Switch to 'Job' resolution for 'Check Run' (easier search)

This commit is contained in:
Michael J Mulligan 2021-04-06 21:50:58 +01:00
parent 61a0ce84ec
commit ff77f2c863
3 changed files with 73 additions and 107 deletions

View file

@ -6835,60 +6835,58 @@ const logLintIssues = (issues) => {
}); });
}; };
function resolveCheckRunId() { function resolveCheckRunId() {
var _a, _b; var _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let checkRunId = -1; let jobId = -1;
if (process.env.GITHUB_ACTIONS === `true`) { if (process.env.GITHUB_ACTIONS === `true` && process.env.GITHUB_RUN_ID) {
try { try {
core.info(`Attempting to resolve GitHub Check Run`); core.info(`Attempting to resolve current GitHub Job`);
const ctx = github.context; const ctx = github.context;
const ref = (_a = ctx.payload.after) !== null && _a !== void 0 ? _a : ctx.sha;
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
const { data: checkRunsResponse } = yield octokit.checks const { data: workflowResponse } = yield octokit.actions
.listForRef(Object.assign(Object.assign({}, ctx.repo), { ref, status: `in_progress`, filter: `latest` })) .listJobsForWorkflowRun(Object.assign(Object.assign({}, ctx.repo), { run_id: parseInt(process.env.GITHUB_RUN_ID) }))
.catch((e) => { .catch((e) => {
throw `Unable to fetch Check Run List: ${e}`; throw `Unable to fetch Workflow Job List: ${e}`;
}); });
if (checkRunsResponse.check_runs.length > 0) { if (workflowResponse.jobs.length > 0) {
let checkRuns = checkRunsResponse.check_runs; if (workflowResponse.jobs.length > 1) {
if (checkRuns.length > 1) { workflowResponse.jobs = (_a = workflowResponse.jobs.filter((run) => run.name.includes(ctx.job))) !== null && _a !== void 0 ? _a : workflowResponse.jobs;
checkRuns = (_b = checkRuns.filter((run) => run.name.includes(ctx.job))) !== null && _b !== void 0 ? _b : checkRuns;
} }
if (checkRuns.length > 1) { if (workflowResponse.jobs.length > 1) {
const searchToken = uuid_1.v4(); const searchToken = uuid_1.v4();
core.info(`::warning::[ignore] Resolving GitHub Check Run with Search Token: ${searchToken}`); core.info(`::warning::[ignore] Resolving GitHub Job with Search Token: ${searchToken}`);
for (const run of checkRuns) { for (const job of workflowResponse.jobs) {
try { try {
if ((yield octokit.checks.listAnnotations(Object.assign(Object.assign({}, ctx.repo), { check_run_id: run.id }))).data.findIndex((annotation) => annotation.message.includes(searchToken)) !== -1) { if ((yield octokit.checks.listAnnotations(Object.assign(Object.assign({}, ctx.repo), { check_run_id: job.id }))).data.findIndex((annotation) => annotation.message.includes(searchToken)) !== -1) {
checkRunId = run.id; jobId = job.id;
break; break;
} }
} }
catch (e) { catch (e) {
core.info(`::debug::Error Fetching CheckRun ${run.id}: ${e}`); core.info(`::debug::Error Fetching Job ${job.id}: ${e}`);
} }
} }
} }
else if (checkRuns[0]) { else if (workflowResponse.jobs[0]) {
checkRunId = checkRuns[0].id; jobId = workflowResponse.jobs[0].id;
core.info(`Current Check Run: ${checkRunId}`);
} }
else { else {
throw `Unable to resolve GitHub Check Run`; throw `Unable to resolve GitHub Job`;
} }
core.info(`Current Job: ${jobId}`);
} }
else { else {
throw `Fetching octokit.checks.listForRef(${ref}) returned no results`; throw `Fetching octokit.actions.getWorkflowRun(${process.env.GITHUB_RUN_ID}) returned no results`;
} }
} }
catch (e) { catch (e) {
core.info(`::error::Error resolving GitHub Check Run: ${e}`); core.info(`::error::Unable to resolve GitHub Job: ${e}`);
} }
} }
else { else {
core.info(`::debug::Not in GitHub Action Context, Skipping Check Run Resolution`); core.info(`::debug::Not in GitHub Action Context, Skipping Job Resolution`);
} }
return checkRunId; return jobId;
}); });
} }
function annotateLintIssues(issues, checkRunId) { function annotateLintIssues(issues, checkRunId) {

50
dist/run/index.js vendored
View file

@ -6845,60 +6845,58 @@ const logLintIssues = (issues) => {
}); });
}; };
function resolveCheckRunId() { function resolveCheckRunId() {
var _a, _b; var _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let checkRunId = -1; let jobId = -1;
if (process.env.GITHUB_ACTIONS === `true`) { if (process.env.GITHUB_ACTIONS === `true` && process.env.GITHUB_RUN_ID) {
try { try {
core.info(`Attempting to resolve GitHub Check Run`); core.info(`Attempting to resolve current GitHub Job`);
const ctx = github.context; const ctx = github.context;
const ref = (_a = ctx.payload.after) !== null && _a !== void 0 ? _a : ctx.sha;
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
const { data: checkRunsResponse } = yield octokit.checks const { data: workflowResponse } = yield octokit.actions
.listForRef(Object.assign(Object.assign({}, ctx.repo), { ref, status: `in_progress`, filter: `latest` })) .listJobsForWorkflowRun(Object.assign(Object.assign({}, ctx.repo), { run_id: parseInt(process.env.GITHUB_RUN_ID) }))
.catch((e) => { .catch((e) => {
throw `Unable to fetch Check Run List: ${e}`; throw `Unable to fetch Workflow Job List: ${e}`;
}); });
if (checkRunsResponse.check_runs.length > 0) { if (workflowResponse.jobs.length > 0) {
let checkRuns = checkRunsResponse.check_runs; if (workflowResponse.jobs.length > 1) {
if (checkRuns.length > 1) { workflowResponse.jobs = (_a = workflowResponse.jobs.filter((run) => run.name.includes(ctx.job))) !== null && _a !== void 0 ? _a : workflowResponse.jobs;
checkRuns = (_b = checkRuns.filter((run) => run.name.includes(ctx.job))) !== null && _b !== void 0 ? _b : checkRuns;
} }
if (checkRuns.length > 1) { if (workflowResponse.jobs.length > 1) {
const searchToken = uuid_1.v4(); const searchToken = uuid_1.v4();
core.info(`::warning::[ignore] Resolving GitHub Check Run with Search Token: ${searchToken}`); core.info(`::warning::[ignore] Resolving GitHub Job with Search Token: ${searchToken}`);
for (const run of checkRuns) { for (const job of workflowResponse.jobs) {
try { try {
if ((yield octokit.checks.listAnnotations(Object.assign(Object.assign({}, ctx.repo), { check_run_id: run.id }))).data.findIndex((annotation) => annotation.message.includes(searchToken)) !== -1) { if ((yield octokit.checks.listAnnotations(Object.assign(Object.assign({}, ctx.repo), { check_run_id: job.id }))).data.findIndex((annotation) => annotation.message.includes(searchToken)) !== -1) {
checkRunId = run.id; jobId = job.id;
break; break;
} }
} }
catch (e) { catch (e) {
core.info(`::debug::Error Fetching CheckRun ${run.id}: ${e}`); core.info(`::debug::Error Fetching Job ${job.id}: ${e}`);
} }
} }
} }
else if (checkRuns[0]) { else if (workflowResponse.jobs[0]) {
checkRunId = checkRuns[0].id; jobId = workflowResponse.jobs[0].id;
core.info(`Current Check Run: ${checkRunId}`);
} }
else { else {
throw `Unable to resolve GitHub Check Run`; throw `Unable to resolve GitHub Job`;
} }
core.info(`Current Job: ${jobId}`);
} }
else { else {
throw `Fetching octokit.checks.listForRef(${ref}) returned no results`; throw `Fetching octokit.actions.getWorkflowRun(${process.env.GITHUB_RUN_ID}) returned no results`;
} }
} }
catch (e) { catch (e) {
core.info(`::error::Error resolving GitHub Check Run: ${e}`); core.info(`::error::Unable to resolve GitHub Job: ${e}`);
} }
} }
else { else {
core.info(`::debug::Not in GitHub Action Context, Skipping Check Run Resolution`); core.info(`::debug::Not in GitHub Action Context, Skipping Job Resolution`);
} }
return checkRunId; return jobId;
}); });
} }
function annotateLintIssues(issues, checkRunId) { function annotateLintIssues(issues, checkRunId) {

View file

@ -175,31 +175,6 @@ type GithubAnnotation = {
raw_details?: string raw_details?: string
} }
type CheckRun = {
id: number
node_id: string
head_sha: string
external_id: string
url: string
html_url: string
details_url: string
status: string
conclusion: string | null
started_at: string
completed_at: string | null
output: {
title: string | null
summary: string | null
text: string | null
annotations_count: number
annotations_url: string
}
name: string
check_suite: {
id: number
}
}
type SeverityMap = { type SeverityMap = {
[key: string]: LintSeverityStrings [key: string]: LintSeverityStrings
} }
@ -265,68 +240,63 @@ const logLintIssues = (issues: LintIssue[]): void => {
} }
async function resolveCheckRunId(): Promise<number> { async function resolveCheckRunId(): Promise<number> {
let checkRunId = -1 let jobId = -1
if (process.env.GITHUB_ACTIONS === `true`) { if (process.env.GITHUB_ACTIONS === `true` && process.env.GITHUB_RUN_ID) {
try { try {
core.info(`Attempting to resolve GitHub Check Run`) core.info(`Attempting to resolve current GitHub Job`)
const ctx = github.context const ctx = github.context
const ref = ctx.payload.after ?? ctx.sha
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
const { data: checkRunsResponse } = await octokit.checks const { data: workflowResponse } = await octokit.actions
.listForRef({ .listJobsForWorkflowRun({
...ctx.repo, ...ctx.repo,
ref, run_id: parseInt(process.env.GITHUB_RUN_ID),
status: `in_progress`,
filter: `latest`,
}) })
.catch((e: string) => { .catch((e: string) => {
throw `Unable to fetch Check Run List: ${e}` throw `Unable to fetch Workflow Job List: ${e}`
}) })
if (checkRunsResponse.check_runs.length > 0) { if (workflowResponse.jobs.length > 0) {
let checkRuns: CheckRun[] = checkRunsResponse.check_runs if (workflowResponse.jobs.length > 1) {
if (checkRuns.length > 1) { workflowResponse.jobs = workflowResponse.jobs.filter((run) => run.name.includes(ctx.job)) ?? workflowResponse.jobs
checkRuns = checkRuns.filter((run) => run.name.includes(ctx.job)) ?? checkRuns
} }
if (checkRuns.length > 1) { if (workflowResponse.jobs.length > 1) {
const searchToken = uuidv4() const searchToken = uuidv4()
core.info(`::warning::[ignore] Resolving GitHub Check Run with Search Token: ${searchToken}`) core.info(`::warning::[ignore] Resolving GitHub Job with Search Token: ${searchToken}`)
for (const run of checkRuns) { for (const job of workflowResponse.jobs) {
try { try {
if ( if (
( (
await octokit.checks.listAnnotations({ await octokit.checks.listAnnotations({
...ctx.repo, ...ctx.repo,
check_run_id: run.id, check_run_id: job.id,
}) })
).data.findIndex((annotation: { message: string }) => annotation.message.includes(searchToken)) !== -1 ).data.findIndex((annotation) => annotation.message.includes(searchToken)) !== -1
) { ) {
checkRunId = run.id jobId = job.id
break break
} }
} catch (e) { } catch (e) {
core.info(`::debug::Error Fetching CheckRun ${run.id}: ${e}`) core.info(`::debug::Error Fetching Job ${job.id}: ${e}`)
} }
} }
} else if (checkRuns[0]) { } else if (workflowResponse.jobs[0]) {
checkRunId = checkRuns[0].id jobId = workflowResponse.jobs[0].id
core.info(`Current Check Run: ${checkRunId}`)
} else { } else {
throw `Unable to resolve GitHub Check Run` throw `Unable to resolve GitHub Job`
} }
core.info(`Current Job: ${jobId}`)
} else { } else {
throw `Fetching octokit.checks.listForRef(${ref}) returned no results` throw `Fetching octokit.actions.getWorkflowRun(${process.env.GITHUB_RUN_ID}) returned no results`
} }
} catch (e) { } catch (e) {
core.info(`::error::Error resolving GitHub Check Run: ${e}`) core.info(`::error::Unable to resolve GitHub Job: ${e}`)
} }
} else { } else {
core.info(`::debug::Not in GitHub Action Context, Skipping Check Run Resolution`) core.info(`::debug::Not in GitHub Action Context, Skipping Job Resolution`)
} }
return checkRunId return jobId
} }
async function annotateLintIssues(issues: LintIssue[], checkRunId: number): Promise<void> { async function annotateLintIssues(issues: LintIssue[], checkRunId: number): Promise<void> {