From f41a502b532f30530f72eb625610b7f0915b47c8 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Mon, 2 Oct 2023 22:53:55 +0900 Subject: [PATCH] Experimental - Directly filter run id since GraphQL --- dist/index.js | 34 +++++++++------------------------- src/github-api.ts | 45 +++++++++------------------------------------ src/main.ts | 8 ++------ 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/dist/index.js b/dist/index.js index 83d85b1a..9e9a8aae 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7873,6 +7873,9 @@ async function getCheckRunSummaries(octokit, params) { if (!workflow) { return []; } + if (checkSuite.workflowRun?.databaseId === params.triggerRunId) { + return []; + } const runNodes = checkSuite?.checkRuns?.nodes?.flatMap((node) => node ? [node] : []); if (!runNodes) { (0, import_core.error)("Cannot correctly get via GraphQL"); @@ -7893,28 +7896,12 @@ async function getCheckRunSummaries(octokit, params) { }); return summaries.toSorted((a, b) => join(a.workflowPath, a.jobName).localeCompare(join(b.workflowPath, b.jobName))); } -async function fetchJobIDs(octokit, params) { - return new Set( - await octokit.paginate( - octokit.rest.actions.listJobsForWorkflowRun, - { - ...params, - per_page: 100, - filter: "latest" - }, - (resp) => resp.data.map((job) => job.id) - ) - ); -} -async function fetchOtherRunStatus(octokit, params, ownJobIDs) { +async function fetchOtherRunStatus(octokit, params) { const checkRunSummaries = await getCheckRunSummaries(octokit, params); - const otherRelatedRuns = checkRunSummaries.flatMap( - (summary) => summary.runDatabaseId ? ownJobIDs.has(summary.runDatabaseId) ? [] : [summary] : [] - ); - const otherRelatedCompletedRuns = otherRelatedRuns.filter((summary) => summary.runStatus === "COMPLETED"); - const progress = otherRelatedCompletedRuns.length === otherRelatedRuns.length ? "done" : "in_progress"; - const conclusion = otherRelatedCompletedRuns.every((summary) => summary.acceptable) ? "acceptable" : "bad"; - return { progress, conclusion, summaries: otherRelatedRuns }; + const completedRuns = checkRunSummaries.filter((summary) => summary.runStatus === "COMPLETED"); + const progress = completedRuns.length === checkRunSummaries.length ? "done" : "in_progress"; + const conclusion = completedRuns.every((summary) => summary.acceptable) ? "acceptable" : "bad"; + return { progress, conclusion, summaries: checkRunSummaries }; } // src/wait.ts @@ -8032,8 +8019,6 @@ async function run() { return; } (0, import_core2.startGroup)("Get own job_id"); - const ownJobIDs = await fetchJobIDs(octokit, { ...repositoryInfo, run_id: runId }); - (0, import_core2.info)(JSON.stringify({ ownJobIDs: [...ownJobIDs] }, null, 2)); (0, import_core2.endGroup)(); for (; ; ) { attempts += 1; @@ -8047,8 +8032,7 @@ async function run() { (0, import_core2.startGroup)(`Polling ${attempts}: ${(/* @__PURE__ */ new Date()).toISOString()}`); const report = await fetchOtherRunStatus( octokit, - { ...repositoryInfo, ref: commitSha }, - ownJobIDs + { ...repositoryInfo, ref: commitSha, triggerRunId: runId } ); for (const summary of report.summaries) { const { diff --git a/src/github-api.ts b/src/github-api.ts index a916dafa..afc667b8 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -1,5 +1,4 @@ import type { getOctokit } from '@actions/github'; -import type { Endpoints } from '@octokit/types'; import schema from '@octokit/graphql-schema'; import { error } from '@actions/core'; import { join, relative } from 'path'; @@ -22,7 +21,7 @@ interface Summary { export async function getCheckRunSummaries( octokit: Octokit, - params: { 'owner': string; 'repo': string; ref: string }, + params: { owner: string; repo: string; ref: string; triggerRunId: number }, ): Promise> { const { repository: { object: { checkSuites } } } = await octokit.graphql< { repository: { object: { checkSuites: schema.Commit['checkSuites'] } } } @@ -79,6 +78,10 @@ export async function getCheckRunSummaries( return []; } + if (checkSuite.workflowRun?.databaseId === params.triggerRunId) { + return []; + } + const runNodes = checkSuite?.checkRuns?.nodes?.flatMap((node) => node ? [node] : []); if (!runNodes) { error('Cannot correctly get via GraphQL'); @@ -106,15 +109,6 @@ export async function getCheckRunSummaries( type Octokit = ReturnType; -// REST: https://docs.github.com/en/rest/reference/actions#list-jobs-for-a-workflow-run -// GitHub does not provide to get job_id, we should get from the run_id https://github.com/actions/starter-workflows/issues/292#issuecomment-922372823 -const listWorkflowRunsRoute = 'GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs' as const; -type ListWorkflowRunsRoute = typeof listWorkflowRunsRoute; -type ListWorkflowRunsResponse = Endpoints[ListWorkflowRunsRoute]['response']; -type ListWorkflowRunsParams = Endpoints[ListWorkflowRunsRoute]['parameters']; - -type JobID = ListWorkflowRunsResponse['data']['jobs'][number]['id']; - // No need to keep as same as GitHub responses so We can change the definition. interface Report { progress: 'in_progress' | 'done'; @@ -122,40 +116,19 @@ interface Report { summaries: Summary[]; } -export async function fetchJobIDs( - octokit: Octokit, - params: Readonly>, -): Promise> { - return new Set( - await octokit.paginate( - octokit.rest.actions.listJobsForWorkflowRun, - { - ...params, - per_page: 100, - filter: 'latest', - }, - (resp) => resp.data.map((job) => job.id), - ), - ); -} - export async function fetchOtherRunStatus( octokit: Parameters[0], params: Parameters[1], - ownJobIDs: Readonly>, ): Promise { const checkRunSummaries = await getCheckRunSummaries(octokit, params); - const otherRelatedRuns = checkRunSummaries.flatMap((summary) => - summary.runDatabaseId ? (ownJobIDs.has(summary.runDatabaseId) ? [] : [summary]) : [] - ); - const otherRelatedCompletedRuns = otherRelatedRuns.filter((summary) => summary.runStatus === 'COMPLETED'); + const completedRuns = checkRunSummaries.filter((summary) => summary.runStatus === 'COMPLETED'); - const progress: Report['progress'] = otherRelatedCompletedRuns.length === otherRelatedRuns.length + const progress: Report['progress'] = completedRuns.length === checkRunSummaries.length ? 'done' : 'in_progress'; - const conclusion: Report['conclusion'] = otherRelatedCompletedRuns.every((summary) => summary.acceptable) + const conclusion: Report['conclusion'] = completedRuns.every((summary) => summary.acceptable) ? 'acceptable' : 'bad'; - return { progress, conclusion, summaries: otherRelatedRuns }; + return { progress, conclusion, summaries: checkRunSummaries }; } diff --git a/src/main.ts b/src/main.ts index 549f9291..9e806ac1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,7 @@ const errorMessage = (body: string) => (`${styles.red.open}${body}${styles.red.c const succeededMessage = (body: string) => (`${styles.green.open}${body}${styles.green.close}`); const colorize = (body: string, ok: boolean) => (ok ? succeededMessage(body) : errorMessage(body)); -import { fetchJobIDs, fetchOtherRunStatus } from './github-api.js'; +import { fetchOtherRunStatus } from './github-api.js'; import { readableDuration, wait, isRetryMethod, retryMethods, getIdleMilliseconds } from './wait.js'; async function run(): Promise { @@ -98,9 +98,6 @@ async function run(): Promise { startGroup('Get own job_id'); - const ownJobIDs = await fetchJobIDs(octokit, { ...repositoryInfo, run_id: runId }); - info(JSON.stringify({ ownJobIDs: [...ownJobIDs] }, null, 2)); - endGroup(); for (;;) { @@ -116,8 +113,7 @@ async function run(): Promise { const report = await fetchOtherRunStatus( octokit, - { ...repositoryInfo, ref: commitSha }, - ownJobIDs, + { ...repositoryInfo, ref: commitSha, triggerRunId: runId }, ); for (const summary of report.summaries) {