From 3eeb2710eb7124e82147faffcf009b32b1176255 Mon Sep 17 00:00:00 2001 From: Nicholas Lim <18374483+niclim@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:06:26 -0500 Subject: [PATCH] pass in additional args to diff-all (#8) * pass in additional args to diff-all * write test and build --- action.yml | 3 +++ build/index.js | 18 ++++++++++---- src/__tests__/action.test.ts | 47 ++++++++++++++++++++++++++++++++++-- src/action.ts | 18 +++++++++++--- src/index.ts | 2 ++ 5 files changed, 78 insertions(+), 10 deletions(-) diff --git a/action.yml b/action.yml index 2a10dc1..8e84a48 100644 --- a/action.yml +++ b/action.yml @@ -11,6 +11,9 @@ inputs: description: "Fail the action if a standard fails" required: false default: "true" + additional_args: + description: "Additional arguments to pass into `diff-all`" + required: false runs: using: "node16" main: "build/index.js" diff --git a/build/index.js b/build/index.js index 27eb6ee..b0444fe 100644 --- a/build/index.js +++ b/build/index.js @@ -3999,7 +3999,7 @@ async function execCommand(command, args, options = {}, logError = true) { return false; } } -async function runAction(opticToken, githubToken, standardsFail, eventName, headRef, baseRef, owner, repo, sha) { +async function runAction(opticToken, githubToken, additionalArgs, standardsFail, eventName, headRef, baseRef, owner, repo, sha) { const failOnCheckError = standardsFail === "true"; const valid = verifyInput(opticToken, eventName, owner, repo); if (!valid) { @@ -4038,7 +4038,7 @@ async function runAction(opticToken, githubToken, standardsFail, eventName, head core.error("Unable to determine base for comparison."); return 1; } - const comparisonRun = await diffAll(opticToken, from); + const comparisonRun = await diffAll(opticToken, from, additionalArgs); if (eventName === "pull_request") { const commentResult = await prComment(githubToken, owner || "", repo || "", pr || "", sha || ""); if (!commentResult) { @@ -4096,9 +4096,16 @@ async function deepen() { } return true; } -async function diffAll(token, from) { +async function diffAll(token, from, additionalArgs) { core.info("Running Optic diff-all"); - return execCommand("optic", ["diff-all", "--compare-from", from, "--check", "--upload"], { + return execCommand("optic", [ + "diff-all", + "--compare-from", + from, + "--check", + "--upload", + ...(additionalArgs ? [additionalArgs] : []), + ], { env: Object.assign(Object.assign({}, process.env), { OPTIC_TOKEN: token }), }, false); } @@ -4160,13 +4167,14 @@ const action_1 = __nccwpck_require__(672); const opticToken = core.getInput("optic_token"); const githubToken = core.getInput("github_token"); const standardsFail = core.getInput("standards_fail"); +const additionalArgs = core.getInput("additional_args"); const eventName = process.env.GITHUB_EVENT_NAME; const headRef = process.env.GITHUB_REF; const baseRef = process.env.GITHUB_BASE_REF; const owner = process.env.GITHUB_REPOSITORY_OWNER; const repo = (_a = process.env.GITHUB_REPOSITORY) === null || _a === void 0 ? void 0 : _a.split("/")[1]; const sha = process.env.GITHUB_SHA; -(0, action_1.runAction)(opticToken, githubToken, standardsFail, eventName, headRef, baseRef, owner, repo, sha) +(0, action_1.runAction)(opticToken, githubToken, additionalArgs, standardsFail, eventName, headRef, baseRef, owner, repo, sha) .then((exitCode) => { return process.exit(exitCode); }) diff --git a/src/__tests__/action.test.ts b/src/__tests__/action.test.ts index 94dd960..b548d46 100644 --- a/src/__tests__/action.test.ts +++ b/src/__tests__/action.test.ts @@ -7,6 +7,7 @@ test("invalid input", async () => { const exitCode = await runAction( "optic-token", "github-token", + "", "true", "", "", @@ -24,6 +25,7 @@ test("failed install", async () => { const exitCode = await runAction( "optic-token", "github-token", + "", "true", "push", "refs/heads/main", @@ -45,6 +47,7 @@ test("pull_request event", async () => { const exitCode = await runAction( "optic-token", "github-token", + "", "true", "pull_request", "refs/pulls/1/merge", @@ -68,6 +71,32 @@ test("push event", async () => { const exitCode = await runAction( "optic-token", "github-token", + "", + "true", + "push", + "refs/heads/main", + undefined, + "owner", + "repo", + "abc123" + ); + expect(exitCode).toBe(0); + assertInstall(); + assertDeepen(); + assertDiffAll(); +}); + +test("push event with additional-args", async () => { + const assertInstall = mockInstall(); + const assertDeepen = mockDeepen(); + const assertDiffAll = mockDiffAll("optic-token", "HEAD~1", false, [ + "--fail-on-untracked-openapi", + ]); + + const exitCode = await runAction( + "optic-token", + "github-token", + "--fail-on-untracked-openapi", "true", "push", "refs/heads/main", @@ -90,6 +119,7 @@ test("push event with standards failure and standards_fail set to true", async ( const exitCode = await runAction( "optic-token", "github-token", + "", "true", "push", "refs/heads/main", @@ -112,6 +142,7 @@ test("push event with standards failure but standards_fail set to false", async const exitCode = await runAction( "optic-token", "github-token", + "", "false", "push", "refs/heads/main", @@ -167,7 +198,12 @@ function mockDeepen(): () => void { ); } -function mockDiffAll(token: string, from: string, error = false): () => void { +function mockDiffAll( + token: string, + from: string, + error = false, + additionalArgs: string[] = [] +): () => void { if (error) { jest.mocked(exec.exec).mockRejectedValue(new Error("Something broke")); } else { @@ -177,7 +213,14 @@ function mockDiffAll(token: string, from: string, error = false): () => void { return () => expect(jest.mocked(exec.exec)).toHaveBeenCalledWith( "optic", - ["diff-all", "--compare-from", from, "--check", "--upload"], + [ + "diff-all", + "--compare-from", + from, + "--check", + "--upload", + ...additionalArgs, + ], expect.objectContaining({ env: expect.objectContaining({ OPTIC_TOKEN: "optic-token" }), }) diff --git a/src/action.ts b/src/action.ts index 40ea02b..7efbf5a 100644 --- a/src/action.ts +++ b/src/action.ts @@ -22,6 +22,7 @@ async function execCommand( export async function runAction( opticToken: string, githubToken: string, + additionalArgs: string | undefined, standardsFail: string, eventName: string | undefined, headRef: string | undefined, @@ -75,7 +76,7 @@ export async function runAction( return 1; } - const comparisonRun = await diffAll(opticToken, from); + const comparisonRun = await diffAll(opticToken, from, additionalArgs); if (eventName === "pull_request") { const commentResult = await prComment( @@ -163,12 +164,23 @@ async function deepen(): Promise { return true; } -async function diffAll(token: string, from: string): Promise { +async function diffAll( + token: string, + from: string, + additionalArgs: string | undefined +): Promise { core.info("Running Optic diff-all"); return execCommand( "optic", - ["diff-all", "--compare-from", from, "--check", "--upload"], + [ + "diff-all", + "--compare-from", + from, + "--check", + "--upload", + ...(additionalArgs ? [additionalArgs] : []), + ], { env: { ...process.env, diff --git a/src/index.ts b/src/index.ts index c898625..31a72a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { runAction } from "./action"; const opticToken = core.getInput("optic_token"); const githubToken = core.getInput("github_token"); const standardsFail = core.getInput("standards_fail"); +const additionalArgs = core.getInput("additional_args"); const eventName = process.env.GITHUB_EVENT_NAME; const headRef = process.env.GITHUB_REF; @@ -15,6 +16,7 @@ const sha = process.env.GITHUB_SHA; runAction( opticToken, githubToken, + additionalArgs, standardsFail, eventName, headRef,