Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excluding self check run and same kind check runs when waiting for checks #638

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions dist/getWorkflowRunJobs.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 53 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions lib/getWorkflowRunJobs.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions lib/getWorkflowRunJobs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/getCheckRuns.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Octokit } from "@octokit/core";
import { Api } from "@octokit/plugin-rest-endpoint-methods/dist-types/types";
import { components } from "@octokit/openapi-types/types";
import { context } from "@actions/github";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'context' is defined but never used. @typescript-eslint/no-unused-vars


export async function getCheckRuns(
owner: string,
Expand Down
17 changes: 17 additions & 0 deletions src/getWorkflowRunJobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Octokit } from "@octokit/core";
import { Api } from "@octokit/plugin-rest-endpoint-methods/dist-types/types";
import { components } from "@octokit/openapi-types/types";
import { context } from "@actions/github";

export async function getWorkflowRunJobs(
owner: string,
repo: string,
octokit: Octokit & Api
) {
const response = await octokit.rest.actions.listJobsForWorkflowRun({
owner,
repo,
run_id: context.runId,
});
return response.data.jobs as components["schemas"]["job"][];
}
34 changes: 26 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getPullRequest } from "./getPullRequest";
import { getPullRequestComments } from "./getPullRequestComments";
import { getPullRequestReviewRequests } from "./getPullRequestReviewRequests";
import { getPullRequestReviews } from "./getPullRequestReviews";
import { getWorkflowRunJobs } from "./getWorkflowRunJobs";
import { getCheckRuns } from "./getCheckRuns";
import { checkIfPullRequestMerged, mergePullRequest } from "./mergePullRequest";
import { sleep } from "./sleep";
Expand All @@ -27,9 +28,6 @@ const FORMATTER = new Intl.NumberFormat(LOCALE, {
});

async function run(): Promise<void> {
info(`Current Workflow: ${context.workflow}`);
info(`Current job: ${context.job}`);

const octokit = getOctokit();
const owner = context.repo.owner;
const repo = context.repo.repo;
Expand Down Expand Up @@ -163,9 +161,19 @@ async function run(): Promise<void> {
return;
}

const jobs = await getWorkflowRunJobs(owner, repo, octokit);
info(`Jobs: ${jobs.length}`);
for (const job of jobs) {
info(` Job id: ${job.id} (${job.html_url})`);
info(` Job name: ${job.name}`);
info(` Job run id/attempt: ${job.run_id}-${job.run_attempt}\n\n`);
}
const jobIds = jobs.map((job) => job.id);

const timeout = parseInt(getInput("timeout"), 10);
const interval = parseInt(getInput("checks-watch-interval"), 10);
let checksCompleted = false;
let externalId: string | undefined | null = undefined;
while (!checksCompleted) {
const checkRuns = await getCheckRuns(
owner,
Expand All @@ -175,16 +183,26 @@ async function run(): Promise<void> {
);
info(`Checks:`);
for (const checkRun of checkRuns) {
info(` Check id: ${checkRun.id} (${checkRun.html_url})`);
info(` Check name: ${checkRun.name}`);
info(
` ${checkRun.name}: ${
` Check status/conclusion: ${
checkRun.status === COMPLETED ? checkRun.conclusion : checkRun.status
}`
}\n\n`
);
}
if (externalId === undefined || externalId === null) {
externalId = checkRuns.find((checkRun) =>
jobIds.includes(checkRun.id)
)?.external_id;
}
const incompleteChecks = checkRuns.filter(
(checkRun) => checkRun.status !== COMPLETED
(checkRun) =>
!jobIds.includes(checkRun.id) &&
checkRun.external_id !== externalId &&
checkRun.status !== COMPLETED
);
checksCompleted = incompleteChecks.length <= 1;
checksCompleted = incompleteChecks.length === 0;
if (checksCompleted) {
const failedCheckes = checkRuns.filter(
(checkRun) =>
Expand All @@ -204,7 +222,7 @@ async function run(): Promise<void> {
const executionTime = Math.round(performance.now() / 1000);
if (executionTime <= timeout) {
info(`Execution time: ${FORMATTER.format(executionTime)}`);
info(`Sleeping: ${FORMATTER.format(interval)}`);
info(`Sleeping: ${FORMATTER.format(interval)}\n`);
await sleep(interval * 1000);
} else {
error(`Execution time: ${FORMATTER.format(executionTime)}`);
Expand Down