Skip to content

Commit

Permalink
Experimental - Directly filter run id since GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Oct 2, 2023
1 parent 252ec56 commit 22a1606
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 69 deletions.
36 changes: 10 additions & 26 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -8010,7 +7997,7 @@ async function run() {
(0, import_core2.info)(JSON.stringify(
{
triggeredCommitSha: commitSha,
ownRunId: runId,
runId,
repositoryInfo,
minIntervalSeconds,
retryMethod,
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
45 changes: 9 additions & 36 deletions src/github-api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Array<Summary>> {
const { repository: { object: { checkSuites } } } = await octokit.graphql<
{ repository: { object: { checkSuites: schema.Commit['checkSuites'] } } }
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -106,56 +109,26 @@ export async function getCheckRunSummaries(

type Octokit = ReturnType<typeof getOctokit>;

// 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';
conclusion: 'acceptable' | 'bad';
summaries: Summary[];
}

export async function fetchJobIDs(
octokit: Octokit,
params: Readonly<Pick<ListWorkflowRunsParams, 'owner' | 'repo' | 'run_id'>>,
): Promise<Set<JobID>> {
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<typeof getCheckRunSummaries>[0],
params: Parameters<typeof getCheckRunSummaries>[1],
ownJobIDs: Readonly<Set<JobID>>,
): Promise<Report> {
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 };
}
10 changes: 3 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down Expand Up @@ -69,7 +69,7 @@ async function run(): Promise<void> {
info(JSON.stringify(
{
triggeredCommitSha: commitSha,
ownRunId: runId,
runId,
repositoryInfo,
minIntervalSeconds,
retryMethod,
Expand Down Expand Up @@ -98,9 +98,6 @@ async function run(): Promise<void> {

startGroup('Get own job_id');

const ownJobIDs = await fetchJobIDs(octokit, { ...repositoryInfo, run_id: runId });
info(JSON.stringify({ ownJobIDs: [...ownJobIDs] }, null, 2));

endGroup();

for (;;) {
Expand All @@ -116,8 +113,7 @@ async function run(): Promise<void> {

const report = await fetchOtherRunStatus(
octokit,
{ ...repositoryInfo, ref: commitSha },
ownJobIDs,
{ ...repositoryInfo, ref: commitSha, triggerRunId: runId },
);

for (const summary of report.summaries) {
Expand Down

0 comments on commit 22a1606

Please sign in to comment.