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

Add workload_run_attempt to analysis upload #1658

Merged
merged 5 commits into from Apr 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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
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