Skip to content

Adds ref and SHA as inputs, and sarif-id as output #889

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds ref and SHA as inputs, and sarif-id as output
  • Loading branch information
cw-alexcroteau committed Feb 1, 2022
commit 980fd4ed387536df268b54e3e03a4f7146c27171
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [UNRELEASED]

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

## 1.0.31 - 31 Jan 2022

Expand Down
8 changes: 8 additions & 0 deletions analyze/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ inputs:
description: "The path at which the analyzed repository was checked out. Used to relativize any absolute paths in the uploaded SARIF file."
required: false
default: ${{ github.workspace }}
ref:
description: "The ref where results will be uploaded. If not provided, the Action will use the GITHUB_REF environment variable."
required: false
sha:
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."
required: false
category:
description: String used by Code Scanning for matching the analyses
required: false
Expand All @@ -63,6 +69,8 @@ inputs:
outputs:
db-locations:
description: A map from language to absolute path for each database created by CodeQL.
sarif-id:
description: The ID of the uploaded sarif file.
runs:
using: "node12"
main: "../lib/analyze-action.js"
18 changes: 18 additions & 0 deletions src/actions-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (
callback.restore();
});

test("getRef() returns ref provided as an input and ignores current HEAD", async (t) => {
const getAdditionalInputStub = sinon.stub(actionsutil, "getOptionalInput");
getAdditionalInputStub.withArgs("ref").resolves("refs/pull/2/merge");
getAdditionalInputStub.withArgs("sha").resolves("b".repeat(40));

// These values are be ignored
process.env["GITHUB_REF"] = "refs/pull/1/merge";
process.env["GITHUB_SHA"] = "a".repeat(40);

const callback = sinon.stub(actionsutil, "getCommitOid");
callback.withArgs("refs/pull/1/merge").resolves("b".repeat(40));
callback.withArgs("HEAD").resolves("b".repeat(40));

const actualRef = await actionsutil.getRef();
t.deepEqual(actualRef, "refs/pull/2/head");
callback.restore();
});

test("computeAutomationID()", async (t) => {
let actualAutomationID = actionsutil.computeAutomationID(
".github/workflows/codeql-analysis.yml:analyze",
Expand Down
17 changes: 12 additions & 5 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export const getCommitOid = async function (ref = "HEAD"): Promise<string> {
return commitOid.trim();
} catch (e) {
core.info(
`Failed to call git to get current commit. Continuing with data from environment: ${e}`
`Failed to call git to get current commit. Continuing with data from environment or input: ${e}`
);
core.info((e as Error).stack || "NO STACK");
return getRequiredEnvParam("GITHUB_SHA");
return getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
}
};

Expand Down Expand Up @@ -431,8 +431,15 @@ export function computeAutomationID(
export async function getRef(): Promise<string> {
// Will be in the form "refs/heads/master" on a push event
// or in the form "refs/pull/N/merge" on a pull_request event
const ref = getRequiredEnvParam("GITHUB_REF");
const sha = getRequiredEnvParam("GITHUB_SHA");
const refInput = getOptionalInput("ref");
const ref = refInput || getRequiredEnvParam("GITHUB_REF");
const sha = getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably be an error if only one of these inputs are specified.

Also, I'm not sure what would happen if you specify a SHA that is not part of the current branch. Hopefully, code scanning would error out..

Copy link
Contributor Author

@cw-alexcroteau cw-alexcroteau Jan 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I've added a check that throws an error message, and a unit test to confirm it works. Integration test to come with the rest of the integration tests.

  2. I was initially targeting the upload-sarif action when I added this, so I did really see it as two arbitrary values provided by the user, who would ultimately be responsible to confirm their validity.
    I can see two ways to handle this:
    a. I can add an integration test for this specific use case, which would test that the code scanning would error out if an invalid ref is provided. If the error message is meaningful and relevant, we can rely on it.
    b. I can add a check using git branch ${ref} --contains ${sha} in getRef(), with a fallback if the git command can't be called (see line 86). I would of course add the corresponding unit/integration tests.

For 2., I would prefer option b. This way, we don't have to rely on the underlying scanner. What's your preferred solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: Option 1 would not be possible, because I realized integration tests can only be positive with the current setup.

So, I can either leave it as-is, without a test, or use option b.


// If the ref is a user-provided input, we have to skip logic
// and assume that it is really where they want to upload the results.
if (refInput) {
return refInput;
}

// For pull request refs we want to detect whether the workflow
// has run `git checkout HEAD^2` to analyze the 'head' ref rather
Expand Down Expand Up @@ -520,7 +527,7 @@ export async function createStatusReportBase(
cause?: string,
exception?: string
): Promise<StatusReportBase> {
const commitOid = process.env["GITHUB_SHA"] || "";
const commitOid = getOptionalInput("sha") || process.env["GITHUB_SHA"] || "";
const ref = await getRef();
const workflowRunIDStr = process.env["GITHUB_RUN_ID"];
let workflowRunID = -1;
Expand Down
1 change: 1 addition & 0 deletions src/analyze-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ async function run() {
apiDetails,
logger
);
core.setOutput('sarif-id', uploadResult.sarifID);
} else {
logger.info("Not uploading results");
}
Expand Down
1 change: 1 addition & 0 deletions src/upload-sarif-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function run() {
apiDetails,
getActionsLogger()
);
core.setOutput('sarif-id', uploadResult.sarifID);
if (actionsUtil.getRequiredInput("wait-for-processing") === "true") {
await upload_lib.waitForProcessing(
parseRepositoryNwo(getRequiredEnvParam("GITHUB_REPOSITORY")),
Expand Down
9 changes: 9 additions & 0 deletions upload-sarif/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ inputs:
description: "The path at which the analyzed repository was checked out. Used to relativize any absolute paths in the uploaded SARIF file."
required: false
default: ${{ github.workspace }}
ref:
description: "The ref where results will be uploaded. If not provided, the Action will use the GITHUB_REF environment variable."
required: false
sha:
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."
required: false
token:
default: ${{ github.token }}
matrix:
Expand All @@ -24,6 +30,9 @@ inputs:
description: If true, the Action will wait for the uploaded SARIF to be processed before completing.
required: true
default: "false"
outputs:
sarif-id:
description: The ID of the uploaded sarif file.
runs:
using: 'node12'
main: '../lib/upload-sarif-action.js'
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy