Skip to content

Commit da583b0

Browse files
authored
Add workload_run_attempt to analysis upload (#1658)
* Refactor status report upload logic Previously we had duplicated the logic to check `GITHUB_RUN_ID`. We now call the `getWorkflowRunID()` method for the status report upload method, and move the logic for the run attempt to `getWorkflowRunAttempt()` * Add `workflow_run_attempt` to analysis payload * Stop allowing `undefined` run IDs and attempts Because we already throw an error if the ID or attempt aren't numbers, we don't have to allow `undefined` values into the payload.
1 parent a9648ea commit da583b0

12 files changed

+85
-42
lines changed

lib/actions-util.js

+2-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.test.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/workflow.js

+22-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/workflow.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import {
2121
parseMatrixInput,
2222
UserError,
2323
} from "./util";
24-
import { getWorkflowRelativePath } from "./workflow";
24+
import {
25+
getWorkflowRunID,
26+
getWorkflowRunAttempt,
27+
getWorkflowRelativePath,
28+
} from "./workflow";
2529

2630
// eslint-disable-next-line import/no-commonjs
2731
const pkg = require("../package.json") as JSONSchemaForNPMPackageJsonFiles;
@@ -407,16 +411,8 @@ export async function createStatusReportBase(
407411
): Promise<StatusReportBase> {
408412
const commitOid = getOptionalInput("sha") || process.env["GITHUB_SHA"] || "";
409413
const ref = await getRef();
410-
const workflowRunIDStr = process.env["GITHUB_RUN_ID"];
411-
let workflowRunID = -1;
412-
if (workflowRunIDStr) {
413-
workflowRunID = parseInt(workflowRunIDStr, 10);
414-
}
415-
const workflowRunAttemptStr = process.env["GITHUB_RUN_ATTEMPT"];
416-
let workflowRunAttempt = -1;
417-
if (workflowRunAttemptStr) {
418-
workflowRunAttempt = parseInt(workflowRunAttemptStr, 10);
419-
}
414+
const workflowRunID = getWorkflowRunID();
415+
const workflowRunAttempt = getWorkflowRunAttempt();
420416
const workflowName = process.env["GITHUB_WORKFLOW"] || "";
421417
const jobName = process.env["GITHUB_JOB"] || "";
422418
const analysis_key = await getAnalysisKey();

src/upload-lib.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
3737
"key",
3838
undefined,
3939
"",
40-
undefined,
40+
1234,
41+
1,
4142
"/opt/src",
4243
undefined,
4344
["CodeQL", "eslint"],
@@ -59,7 +60,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
5960
"key",
6061
undefined,
6162
"",
62-
undefined,
63+
1234,
64+
1,
6365
"/opt/src",
6466
undefined,
6567
["CodeQL", "eslint"],
@@ -75,7 +77,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
7577
"key",
7678
undefined,
7779
"",
78-
undefined,
80+
1234,
81+
1,
7982
"/opt/src",
8083
undefined,
8184
["CodeQL", "eslint"],

src/upload-lib.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export async function uploadFromActions(
173173
category,
174174
util.getRequiredEnvParam("GITHUB_WORKFLOW"),
175175
workflow.getWorkflowRunID(),
176+
workflow.getWorkflowRunAttempt(),
176177
checkoutPath,
177178
actionsUtil.getRequiredInput("matrix"),
178179
logger
@@ -255,7 +256,8 @@ export function buildPayload(
255256
analysisKey: string | undefined,
256257
analysisName: string | undefined,
257258
zippedSarif: string,
258-
workflowRunID: number | undefined,
259+
workflowRunID: number,
260+
workflowRunAttempt: number,
259261
checkoutURI: string,
260262
environment: string | undefined,
261263
toolNames: string[],
@@ -268,6 +270,7 @@ export function buildPayload(
268270
analysis_name: analysisName,
269271
sarif: zippedSarif,
270272
workflow_run_id: workflowRunID,
273+
workflow_run_attempt: workflowRunAttempt,
271274
checkout_uri: checkoutURI,
272275
environment,
273276
started_at: process.env[CODEQL_WORKFLOW_STARTED_AT],
@@ -312,7 +315,8 @@ async function uploadFiles(
312315
analysisKey: string,
313316
category: string | undefined,
314317
analysisName: string | undefined,
315-
workflowRunID: number | undefined,
318+
workflowRunID: number,
319+
workflowRunAttempt: number,
316320
sourceRoot: string,
317321
environment: string | undefined,
318322
logger: Logger
@@ -352,6 +356,7 @@ async function uploadFiles(
352356
analysisName,
353357
zippedSarif,
354358
workflowRunID,
359+
workflowRunAttempt,
355360
checkoutURI,
356361
environment,
357362
toolNames,

src/workflow.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,40 @@ export async function getWorkflowRelativePath(): Promise<string> {
312312
* Get the workflow run ID.
313313
*/
314314
export function getWorkflowRunID(): number {
315-
const workflowRunID = parseInt(getRequiredEnvParam("GITHUB_RUN_ID"), 10);
315+
const workflowRunIdString = getRequiredEnvParam("GITHUB_RUN_ID");
316+
const workflowRunID = parseInt(workflowRunIdString, 10);
316317
if (Number.isNaN(workflowRunID)) {
317-
throw new Error("GITHUB_RUN_ID must define a non NaN workflow run ID");
318+
throw new Error(
319+
`GITHUB_RUN_ID must define a non NaN workflow run ID. Current value is ${workflowRunIdString}`
320+
);
321+
}
322+
if (workflowRunID < 0) {
323+
throw new Error(
324+
`GITHUB_RUN_ID must be a non-negative integer. Current value is ${workflowRunIdString}`
325+
);
318326
}
319327
return workflowRunID;
320328
}
321329

330+
/**
331+
* Get the workflow run attempt number.
332+
*/
333+
export function getWorkflowRunAttempt(): number {
334+
const workflowRunAttemptString = getRequiredEnvParam("GITHUB_RUN_ID");
335+
const workflowRunAttempt = parseInt(workflowRunAttemptString, 10);
336+
if (Number.isNaN(workflowRunAttempt)) {
337+
throw new Error(
338+
`GITHUB_RUN_ATTEMPT must define a non NaN workflow run attempt. Current value is ${workflowRunAttemptString}`
339+
);
340+
}
341+
if (workflowRunAttempt <= 0) {
342+
throw new Error(
343+
`GITHUB_RUN_ATTEMPT must be a positive integer. Current value is ${workflowRunAttemptString}`
344+
);
345+
}
346+
return workflowRunAttempt;
347+
}
348+
322349
function getStepsCallingAction(
323350
job: WorkflowJob,
324351
actionName: string

0 commit comments

Comments
 (0)