Skip to content

Commit 1f8caea

Browse files
Adds ref and SHA as inputs, and sarif-id as output
1 parent 708446c commit 1f8caea

File tree

7 files changed

+51
-6
lines changed

7 files changed

+51
-6
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## [UNRELEASED]
44

5-
No user facing changes.
5+
- Add sarif-id as an output for upload-sarif action and analyze action (if uploading)
6+
- Accept ref and hash as inputs to override the ones provided by the runner
67

78
## 1.0.30 - 24 Jan 2022
89

analyze/action.yml

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ inputs:
4545
description: "The path at which the analyzed repository was checked out. Used to relativize any absolute paths in the uploaded SARIF file."
4646
required: false
4747
default: ${{ github.workspace }}
48+
ref:
49+
description: "The ref where results will be uploaded. If not provided, the Action will use the GITHUB_REF environment variable."
50+
required: false
51+
sha:
52+
description: "The hash of the HEAD of the ref where results will be uploaded. If not provided, the Action will use the GITHUB_SHA environment variable."
53+
required: false
4854
category:
4955
description: String used by Code Scanning for matching the analyses
5056
required: false
@@ -63,6 +69,8 @@ inputs:
6369
outputs:
6470
db-locations:
6571
description: A map from language to absolute path for each database created by CodeQL.
72+
sarif-id:
73+
description: The ID of the uploaded sarif file.
6674
runs:
6775
using: "node12"
6876
main: "../lib/analyze-action.js"

src/actions-util.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (
6565
callback.restore();
6666
});
6767

68+
test("getRef() returns ref provided as an input and ignores current HEAD", async (t) => {
69+
const getAdditionalInputStub = sinon.stub(actionsutil, "getOptionalInput");
70+
getAdditionalInputStub.withArgs("ref").resolves("refs/pull/2/merge");
71+
getAdditionalInputStub.withArgs("sha").resolves("b".repeat(40));
72+
73+
// These values are be ignored
74+
process.env["GITHUB_REF"] = "refs/pull/1/merge";
75+
process.env["GITHUB_SHA"] = "a".repeat(40);
76+
77+
const callback = sinon.stub(actionsutil, "getCommitOid");
78+
callback.withArgs("refs/pull/1/merge").resolves("b".repeat(40));
79+
callback.withArgs("HEAD").resolves("b".repeat(40));
80+
81+
const actualRef = await actionsutil.getRef();
82+
t.deepEqual(actualRef, "refs/pull/2/head");
83+
callback.restore();
84+
});
85+
6886
test("computeAutomationID()", async (t) => {
6987
let actualAutomationID = actionsutil.computeAutomationID(
7088
".github/workflows/codeql-analysis.yml:analyze",

src/actions-util.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ export const getCommitOid = async function (ref = "HEAD"): Promise<string> {
8383
return commitOid.trim();
8484
} catch (e) {
8585
core.info(
86-
`Failed to call git to get current commit. Continuing with data from environment: ${e}`
86+
`Failed to call git to get current commit. Continuing with data from environment or input: ${e}`
8787
);
8888
core.info((e as Error).stack || "NO STACK");
89-
return getRequiredEnvParam("GITHUB_SHA");
89+
return getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
9090
}
9191
};
9292

@@ -431,8 +431,15 @@ export function computeAutomationID(
431431
export async function getRef(): Promise<string> {
432432
// Will be in the form "refs/heads/master" on a push event
433433
// or in the form "refs/pull/N/merge" on a pull_request event
434-
const ref = getRequiredEnvParam("GITHUB_REF");
435-
const sha = getRequiredEnvParam("GITHUB_SHA");
434+
const refInput = getOptionalInput("ref");
435+
const ref = refInput || getRequiredEnvParam("GITHUB_REF");
436+
const sha = getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
437+
438+
// If the ref is a user-provided input, we have to skip logic
439+
// and assume that it is really where they want to upload the results.
440+
if (refInput) {
441+
return refInput;
442+
}
436443

437444
// For pull request refs we want to detect whether the workflow
438445
// has run `git checkout HEAD^2` to analyze the 'head' ref rather
@@ -520,7 +527,7 @@ export async function createStatusReportBase(
520527
cause?: string,
521528
exception?: string
522529
): Promise<StatusReportBase> {
523-
const commitOid = process.env["GITHUB_SHA"] || "";
530+
const commitOid = getOptionalInput("sha") || process.env["GITHUB_SHA"] || "";
524531
const ref = await getRef();
525532
const workflowRunIDStr = process.env["GITHUB_RUN_ID"];
526533
let workflowRunID = -1;

src/analyze-action.ts

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ async function run() {
193193
apiDetails,
194194
logger
195195
);
196+
core.setOutput('sarif-id', uploadResult.sarifID);
196197
} else {
197198
logger.info("Not uploading results");
198199
}

src/upload-sarif-action.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async function run() {
6363
apiDetails,
6464
getActionsLogger()
6565
);
66+
core.setOutput('sarif-id', uploadResult.sarifID);
6667
if (actionsUtil.getRequiredInput("wait-for-processing") === "true") {
6768
await upload_lib.waitForProcessing(
6869
parseRepositoryNwo(getRequiredEnvParam("GITHUB_REPOSITORY")),

upload-sarif/action.yml

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ inputs:
1313
description: "The path at which the analyzed repository was checked out. Used to relativize any absolute paths in the uploaded SARIF file."
1414
required: false
1515
default: ${{ github.workspace }}
16+
ref:
17+
description: "The ref where results will be uploaded. If not provided, the Action will use the GITHUB_REF environment variable."
18+
required: false
19+
sha:
20+
description: "The hash of the HEAD of the ref where results will be uploaded. If not provided, the Action will use the GITHUB_SHA environment variable."
21+
required: false
1622
token:
1723
default: ${{ github.token }}
1824
matrix:
@@ -24,6 +30,9 @@ inputs:
2430
description: If true, the Action will wait for the uploaded SARIF to be processed before completing.
2531
required: true
2632
default: "false"
33+
outputs:
34+
sarif-id:
35+
description: The ID of the uploaded sarif file.
2736
runs:
2837
using: 'node12'
2938
main: '../lib/upload-sarif-action.js'

0 commit comments

Comments
 (0)