Skip to content

Commit

Permalink
pass in additional args to diff-all (#8)
Browse files Browse the repository at this point in the history
* pass in additional args to diff-all

* write test and build
niclim authored Feb 28, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 402b845 commit 3eeb271
Showing 5 changed files with 78 additions and 10 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 13 additions & 5 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -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);
})
47 changes: 45 additions & 2 deletions src/__tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -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" }),
})
18 changes: 15 additions & 3 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
return true;
}

async function diffAll(token: string, from: string): Promise<boolean> {
async function diffAll(
token: string,
from: string,
additionalArgs: string | undefined
): Promise<boolean> {
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,
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit 3eeb271

Please sign in to comment.