Skip to content

Commit

Permalink
Showing 10 changed files with 111 additions and 15 deletions.
27 changes: 20 additions & 7 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.

37 changes: 37 additions & 0 deletions lib/actions-util.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/actions-util.test.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/analyze-action.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/analyze-action.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/upload-sarif-action.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-sarif-action.js.map

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

35 changes: 34 additions & 1 deletion src/actions-util.test.ts
@@ -79,8 +79,41 @@ test("getRef() returns ref provided as an input and ignores current HEAD", async
callback.withArgs("HEAD").resolves("b".repeat(40));

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

test("getRef() throws an error if only `ref` is provided as an input", async (t) => {
const getAdditionalInputStub = sinon.stub(actionsutil, "getOptionalInput");
getAdditionalInputStub.withArgs("ref").resolves("refs/pull/1/merge");

await t.throwsAsync(
async () => {
await actionsutil.getRef();
},
{
instanceOf: Error,
message: "Both 'ref' and 'sha' are required if one of them is provided.",
}
);
getAdditionalInputStub.restore();
});

test("getRef() throws an error if only `sha` is provided as an input", async (t) => {
const getAdditionalInputStub = sinon.stub(actionsutil, "getOptionalInput");
getAdditionalInputStub.withArgs("sha").resolves("a".repeat(40));

await t.throwsAsync(
async () => {
await actionsutil.getRef();
},
{
instanceOf: Error,
message: "Both 'ref' and 'sha' are required if one of them is provided.",
}
);
getAdditionalInputStub.restore();
});

test("computeAutomationID()", async (t) => {
17 changes: 14 additions & 3 deletions src/actions-util.ts
@@ -33,10 +33,10 @@ export function getRequiredInput(name: string): string {
* This allows us to get stronger type checking of required/optional inputs
* and make behaviour more consistent between actions and the runner.
*/
export function getOptionalInput(name: string): string | undefined {
export const getOptionalInput = function (name: string): string | undefined {
const value = core.getInput(name);
return value.length > 0 ? value : undefined;
}
};

export function getTemporaryDirectory(): string {
const value = process.env["CODEQL_ACTION_TEMP"];
@@ -432,8 +432,19 @@ 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 refInput = getOptionalInput("ref");
const shaInput = getOptionalInput("sha");

const hasRefInput = !!refInput;
const hasShaInput = !!shaInput;
// If one of 'ref' or 'sha' are provided, both are required
if ((hasRefInput || hasShaInput) && !(hasRefInput && hasShaInput)) {
throw new Error(
"Both 'ref' and 'sha' are required if one of them is provided."
);
}

const ref = refInput || getRequiredEnvParam("GITHUB_REF");
const sha = getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
const sha = shaInput || getRequiredEnvParam("GITHUB_SHA");

// 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.

0 comments on commit 1eaaf07

Please sign in to comment.