Created
May 16, 2023 12:25
-
-
Save horimislime/b1d8615cb7d19cf6cf5dcbdf8512b932 to your computer and use it in GitHub Desktop.
List all warning messages on GitHub Actions workflows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Octokit } = require("@octokit/rest"); | |
const octokit = new Octokit({ | |
auth: process.env.GH_TOKEN, | |
}); | |
(async () => { | |
const orgName = process.env.ORG_NAME; | |
// List all repositories excluding archived status | |
const { data: repos } = await octokit.rest.repos.listForOrg({ | |
org: orgName, | |
per_page: 100, | |
sort: "pushed", | |
direction: "desc", | |
}); | |
const activeRepos = repos.filter((repo) => !repo.archived); | |
for (const repo of activeRepos) { | |
const { data: workflows } = await octokit.rest.actions.listRepoWorkflows({ | |
owner: orgName, | |
repo: repo.name, | |
}); | |
if (workflows.total_count === 0) { | |
continue; | |
} | |
// List workflows in use | |
const activeWorkflows = workflows.workflows.filter((workflow) => workflow.state === "active"); | |
console.log(`----- ${repo.name} -----`); | |
for (const workflow of activeWorkflows) { | |
// Get latest successful workflow run | |
const { data: workflowRuns } = await octokit.rest.actions.listWorkflowRuns({ | |
owner: orgName, | |
repo: repo.name, | |
workflow_id: workflow.id, | |
per_page: 1, | |
status: "success", | |
}); | |
if (workflowRuns.total_count === 0) { | |
continue; | |
} | |
// List Check Suites for the workflow run | |
const { data: checkSuites } = await octokit.rest.checks.listForSuite({ | |
owner: orgName, | |
repo: repo.name, | |
check_suite_id: workflowRuns.workflow_runs[0].check_suite_id, | |
}); | |
// Check if any warning messages exist in the Check Suites | |
const warningMessages = []; | |
for (const checkRun of checkSuites.check_runs) { | |
const { data: annotations } = await octokit.rest.checks.listAnnotations({ | |
owner: orgName, | |
repo: repo.name, | |
check_run_id: checkRun.id, | |
}); | |
warningMessages.push(...Array.from(new Set(annotations.map((annotation) => annotation.message)))); | |
} | |
if (warningMessages.length > 0) { | |
console.log(` | |
${workflow.html_url} has following warnings: | |
${warningMessages.join("\n")}`); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment