Skip to content

Commit a5b959e

Browse files
authored
Merge pull request #3537 from github/henrymercer/overlay-status-record-job
Record the job that published an overlay status
2 parents d1ac77f + 281b265 commit a5b959e

File tree

5 files changed

+89
-11
lines changed

5 files changed

+89
-11
lines changed

init/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ inputs:
159159
description: >-
160160
Explicitly enable or disable caching of project build dependencies.
161161
required: false
162+
check-run-id:
163+
description: >-
164+
[Internal] The ID of the check run, as provided by the Actions runtime environment. Do not set this value manually.
165+
default: ${{ job.check_run_id }}
166+
required: false
162167
outputs:
163168
codeql-path:
164169
description: The path of the CodeQL binary used for analysis

lib/init-action-post.js

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init-action-post-helper.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ test("not uploading failed SARIF when `code-scanning` is not an enabled analysis
316316
test("saves overlay status when overlay-base analysis did not complete successfully", async (t) => {
317317
return await util.withTmpDir(async (tmpDir) => {
318318
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
319+
process.env["GITHUB_RUN_ID"] = "12345";
320+
process.env["GITHUB_RUN_ATTEMPT"] = "1";
321+
process.env["GITHUB_JOB"] = "analyze";
319322
process.env["RUNNER_TEMP"] = tmpDir;
320323
// Ensure analyze did not complete successfully.
321324
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
@@ -370,8 +373,14 @@ test("saves overlay status when overlay-base analysis did not complete successfu
370373
{
371374
attemptedToBuildOverlayBaseDatabase: true,
372375
builtOverlayBaseDatabase: false,
376+
job: {
377+
checkRunId: undefined,
378+
workflowRunId: 12345,
379+
workflowRunAttempt: 1,
380+
name: "analyze",
381+
},
373382
},
374-
"fourth arg should be the overlay status recording an unsuccessful build attempt",
383+
"fourth arg should be the overlay status recording an unsuccessful build attempt with job details",
375384
);
376385
});
377386
});

src/init-action-post-helper.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { EnvVar } from "./environment";
1212
import { Feature, FeatureEnablement } from "./feature-flags";
1313
import { Logger } from "./logging";
1414
import { OverlayDatabaseMode } from "./overlay";
15-
import { OverlayStatus, saveOverlayStatus } from "./overlay/status";
15+
import {
16+
createOverlayStatus,
17+
OverlayStatus,
18+
saveOverlayStatus,
19+
} from "./overlay/status";
1620
import { RepositoryNwo, getRepositoryNwo } from "./repository";
1721
import { JobStatus } from "./status-report";
1822
import * as uploadLib from "./upload-lib";
@@ -270,10 +274,17 @@ async function recordOverlayStatus(
270274
return;
271275
}
272276

273-
const overlayStatus: OverlayStatus = {
274-
attemptedToBuildOverlayBaseDatabase: true,
275-
builtOverlayBaseDatabase: false,
276-
};
277+
const checkRunIdInput = actionsUtil.getOptionalInput("check-run-id");
278+
const checkRunId =
279+
checkRunIdInput !== undefined ? parseInt(checkRunIdInput, 10) : undefined;
280+
281+
const overlayStatus: OverlayStatus = createOverlayStatus(
282+
{
283+
attemptedToBuildOverlayBaseDatabase: true,
284+
builtOverlayBaseDatabase: false,
285+
},
286+
checkRunId !== undefined && checkRunId >= 0 ? checkRunId : undefined,
287+
);
277288

278289
const diskUsage = await checkDiskUsage(logger);
279290
if (diskUsage === undefined) {

src/overlay/status.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import * as path from "path";
1313

1414
import * as actionsCache from "@actions/cache";
1515

16-
import { getTemporaryDirectory } from "../actions-util";
16+
import {
17+
getTemporaryDirectory,
18+
getWorkflowRunAttempt,
19+
getWorkflowRunID,
20+
} from "../actions-util";
1721
import { type CodeQL } from "../codeql";
1822
import { Logger } from "../logging";
1923
import {
2024
DiskUsage,
2125
getErrorMessage,
26+
getRequiredEnvParam,
2227
waitForResultWithTimeLimit,
2328
} from "../util";
2429

@@ -38,12 +43,43 @@ function getStatusFilePath(languages: string[]): string {
3843
);
3944
}
4045

46+
/** Details of the job that recorded an overlay status. */
47+
interface JobInfo {
48+
/** The check run ID. This is optional since it is not always available. */
49+
checkRunId?: number;
50+
/** The workflow run ID. */
51+
workflowRunId: number;
52+
/** The workflow run attempt number. */
53+
workflowRunAttempt: number;
54+
/** The name of the job (from GITHUB_JOB). */
55+
name: string;
56+
}
57+
4158
/** Status of an overlay analysis for a group of languages. */
4259
export interface OverlayStatus {
4360
/** Whether the job attempted to build an overlay base database. */
4461
attemptedToBuildOverlayBaseDatabase: boolean;
4562
/** Whether the job successfully built an overlay base database. */
4663
builtOverlayBaseDatabase: boolean;
64+
/** Details of the job that recorded this status. */
65+
job?: JobInfo;
66+
}
67+
68+
/** Creates an `OverlayStatus` populated with the details of the current job. */
69+
export function createOverlayStatus(
70+
attributes: Omit<OverlayStatus, "job">,
71+
checkRunId?: number,
72+
): OverlayStatus {
73+
const job: JobInfo = {
74+
workflowRunId: getWorkflowRunID(),
75+
workflowRunAttempt: getWorkflowRunAttempt(),
76+
name: getRequiredEnvParam("GITHUB_JOB"),
77+
checkRunId,
78+
};
79+
return {
80+
...attributes,
81+
job,
82+
};
4783
}
4884

4985
/**

0 commit comments

Comments
 (0)