Skip to content

Commit 081b17f

Browse files
committed
check-run: automatically update the conclusion in a post step
In a GitHub Action it is possible to register a `post` Action which will run, if the main Action has been run in a GitHub workflow's job, as an extra step after all the regular steps have been executed (or skipped). This presents a fine opportunity to mark the the Check Run as completed _automatically_, without the need to call the Action _again_. By adding a new input called job status and filling it automatically with -- you guessed it -- the job status, we can even automatically determine the conclusion (I verified manually that this will receive the expected value in the `post` Action). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 327b8a7 commit 081b17f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

check-run/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ inputs:
3838
conclusion:
3939
description: 'If set, the Check Run will be marked as completed'
4040
default: ''
41+
job-status:
42+
description: 'Needed at the end of the job'
43+
default: ${{ job.status }}
4144
outputs:
4245
check-run-id:
4346
description: 'The ID of the created or updated Check Run'
4447
runs:
4548
using: 'node20'
4649
main: './index.js'
50+
post: './post.js'
4751
branding:
4852
icon: 'git-commit'
4953
color: 'orange'

check-run/post.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
async function run() {
2+
const { CIHelper } = await import("../dist/index.js")
3+
4+
try {
5+
const ci = new CIHelper()
6+
ci.setupGitHubAction({ createOrUpdateCheckRun: "post" })
7+
} catch (e) {
8+
console.error(e)
9+
process.exitCode = 1
10+
}
11+
}
12+
13+
run()

lib/ci-helper.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class CIHelper {
102102
needsMailingListMirror?: boolean;
103103
needsUpstreamBranches?: boolean;
104104
needsMailToCommitNotes?: boolean;
105-
createOrUpdateCheckRun?: boolean;
105+
createOrUpdateCheckRun?: boolean | "post";
106106
}): Promise<void> {
107107
// configure the Git committer information
108108
process.env.GIT_CONFIG_PARAMETERS = [
@@ -135,7 +135,9 @@ export class CIHelper {
135135
// Ignore, for now
136136
}
137137

138-
if (setupOptions?.createOrUpdateCheckRun) return await this.createOrUpdateCheckRun();
138+
if (setupOptions?.createOrUpdateCheckRun) {
139+
return await this.createOrUpdateCheckRun(setupOptions.createOrUpdateCheckRun === "post");
140+
}
139141

140142
// help dugite realize where `git` is...
141143
const gitExecutable = os.type() === "Windows_NT" ? "git.exe" : "git";
@@ -285,7 +287,7 @@ export class CIHelper {
285287
}
286288
}
287289

288-
protected async createOrUpdateCheckRun(): Promise<void> {
290+
protected async createOrUpdateCheckRun(runPost: boolean): Promise<void> {
289291
type CheckRunParameters = {
290292
owner: string;
291293
repo: string;
@@ -299,6 +301,7 @@ export class CIHelper {
299301
};
300302
details_url?: string;
301303
conclusion?: ConclusionType;
304+
job_status?: ConclusionType;
302305
};
303306
const params = JSON.parse(core.getState("check-run") || "{}") as CheckRunParameters;
304307

@@ -314,7 +317,7 @@ export class CIHelper {
314317
};
315318
if (Object.keys(params).length) validateCheckRunParameters();
316319

317-
["pr-url", "check-run-id", "name", "title", "summary", "text", "details-url", "conclusion"]
320+
["pr-url", "check-run-id", "name", "title", "summary", "text", "details-url", "conclusion", "job-status"]
318321
.map((name) => [name.replaceAll("-", "_"), core.getInput(name)] as const)
319322
.forEach(([key, value]) => {
320323
if (!value) return;
@@ -327,6 +330,17 @@ export class CIHelper {
327330
});
328331
validateCheckRunParameters();
329332

333+
if (runPost) {
334+
if (!params.check_run_id) {
335+
core.info("No Check Run ID found in state; doing nothing");
336+
return;
337+
}
338+
if (!params.conclusion) {
339+
Object.assign(params, { conclusion: params.job_status });
340+
validateCheckRunParameters();
341+
}
342+
}
343+
330344
if (params.check_run_id === undefined) {
331345
({ id: params.check_run_id } = await this.github.createCheckRun(params));
332346
core.setOutput("check-run-id", params.check_run_id);

0 commit comments

Comments
 (0)