Skip to content

Commit

Permalink
Add workload_run_attempt to analysis upload (#1658)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
angelapwen committed Apr 26, 2023
1 parent a9648ea commit da583b0
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 42 deletions.
12 changes: 2 additions & 10 deletions lib/actions-util.js

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

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions lib/upload-lib.js

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

2 changes: 1 addition & 1 deletion lib/upload-lib.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/upload-lib.test.js

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

2 changes: 1 addition & 1 deletion lib/upload-lib.test.js.map

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions lib/workflow.js

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

2 changes: 1 addition & 1 deletion lib/workflow.js.map

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
parseMatrixInput,
UserError,
} from "./util";
import { getWorkflowRelativePath } from "./workflow";
import {
getWorkflowRunID,
getWorkflowRunAttempt,
getWorkflowRelativePath,
} from "./workflow";

// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json") as JSONSchemaForNPMPackageJsonFiles;
Expand Down Expand Up @@ -407,16 +411,8 @@ export async function createStatusReportBase(
): Promise<StatusReportBase> {
const commitOid = getOptionalInput("sha") || process.env["GITHUB_SHA"] || "";
const ref = await getRef();
const workflowRunIDStr = process.env["GITHUB_RUN_ID"];
let workflowRunID = -1;
if (workflowRunIDStr) {
workflowRunID = parseInt(workflowRunIDStr, 10);
}
const workflowRunAttemptStr = process.env["GITHUB_RUN_ATTEMPT"];
let workflowRunAttempt = -1;
if (workflowRunAttemptStr) {
workflowRunAttempt = parseInt(workflowRunAttemptStr, 10);
}
const workflowRunID = getWorkflowRunID();
const workflowRunAttempt = getWorkflowRunAttempt();
const workflowName = process.env["GITHUB_WORKFLOW"] || "";
const jobName = process.env["GITHUB_JOB"] || "";
const analysis_key = await getAnalysisKey();
Expand Down
9 changes: 6 additions & 3 deletions src/upload-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
"key",
undefined,
"",
undefined,
1234,
1,
"/opt/src",
undefined,
["CodeQL", "eslint"],
Expand All @@ -59,7 +60,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
"key",
undefined,
"",
undefined,
1234,
1,
"/opt/src",
undefined,
["CodeQL", "eslint"],
Expand All @@ -75,7 +77,8 @@ test("validate correct payload used for push, PR merge commit, and PR head", asy
"key",
undefined,
"",
undefined,
1234,
1,
"/opt/src",
undefined,
["CodeQL", "eslint"],
Expand Down
9 changes: 7 additions & 2 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export async function uploadFromActions(
category,
util.getRequiredEnvParam("GITHUB_WORKFLOW"),
workflow.getWorkflowRunID(),
workflow.getWorkflowRunAttempt(),
checkoutPath,
actionsUtil.getRequiredInput("matrix"),
logger
Expand Down Expand Up @@ -255,7 +256,8 @@ export function buildPayload(
analysisKey: string | undefined,
analysisName: string | undefined,
zippedSarif: string,
workflowRunID: number | undefined,
workflowRunID: number,
workflowRunAttempt: number,
checkoutURI: string,
environment: string | undefined,
toolNames: string[],
Expand All @@ -268,6 +270,7 @@ export function buildPayload(
analysis_name: analysisName,
sarif: zippedSarif,
workflow_run_id: workflowRunID,
workflow_run_attempt: workflowRunAttempt,
checkout_uri: checkoutURI,
environment,
started_at: process.env[CODEQL_WORKFLOW_STARTED_AT],
Expand Down Expand Up @@ -312,7 +315,8 @@ async function uploadFiles(
analysisKey: string,
category: string | undefined,
analysisName: string | undefined,
workflowRunID: number | undefined,
workflowRunID: number,
workflowRunAttempt: number,
sourceRoot: string,
environment: string | undefined,
logger: Logger
Expand Down Expand Up @@ -352,6 +356,7 @@ async function uploadFiles(
analysisName,
zippedSarif,
workflowRunID,
workflowRunAttempt,
checkoutURI,
environment,
toolNames,
Expand Down
31 changes: 29 additions & 2 deletions src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,40 @@ export async function getWorkflowRelativePath(): Promise<string> {
* Get the workflow run ID.
*/
export function getWorkflowRunID(): number {
const workflowRunID = parseInt(getRequiredEnvParam("GITHUB_RUN_ID"), 10);
const workflowRunIdString = getRequiredEnvParam("GITHUB_RUN_ID");
const workflowRunID = parseInt(workflowRunIdString, 10);
if (Number.isNaN(workflowRunID)) {
throw new Error("GITHUB_RUN_ID must define a non NaN workflow run ID");
throw new Error(
`GITHUB_RUN_ID must define a non NaN workflow run ID. Current value is ${workflowRunIdString}`
);
}
if (workflowRunID < 0) {
throw new Error(
`GITHUB_RUN_ID must be a non-negative integer. Current value is ${workflowRunIdString}`
);
}
return workflowRunID;
}

/**
* Get the workflow run attempt number.
*/
export function getWorkflowRunAttempt(): number {
const workflowRunAttemptString = getRequiredEnvParam("GITHUB_RUN_ID");
const workflowRunAttempt = parseInt(workflowRunAttemptString, 10);
if (Number.isNaN(workflowRunAttempt)) {
throw new Error(
`GITHUB_RUN_ATTEMPT must define a non NaN workflow run attempt. Current value is ${workflowRunAttemptString}`
);
}
if (workflowRunAttempt <= 0) {
throw new Error(
`GITHUB_RUN_ATTEMPT must be a positive integer. Current value is ${workflowRunAttemptString}`
);
}
return workflowRunAttempt;
}

function getStepsCallingAction(
job: WorkflowJob,
actionName: string
Expand Down

0 comments on commit da583b0

Please sign in to comment.