Skip to content

Commit 3eeb271

Browse files
authored
pass in additional args to diff-all (#8)
* pass in additional args to diff-all * write test and build
1 parent 402b845 commit 3eeb271

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
description: "Fail the action if a standard fails"
1212
required: false
1313
default: "true"
14+
additional_args:
15+
description: "Additional arguments to pass into `diff-all`"
16+
required: false
1417
runs:
1518
using: "node16"
1619
main: "build/index.js"

build/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3999,7 +3999,7 @@ async function execCommand(command, args, options = {}, logError = true) {
39993999
return false;
40004000
}
40014001
}
4002-
async function runAction(opticToken, githubToken, standardsFail, eventName, headRef, baseRef, owner, repo, sha) {
4002+
async function runAction(opticToken, githubToken, additionalArgs, standardsFail, eventName, headRef, baseRef, owner, repo, sha) {
40034003
const failOnCheckError = standardsFail === "true";
40044004
const valid = verifyInput(opticToken, eventName, owner, repo);
40054005
if (!valid) {
@@ -4038,7 +4038,7 @@ async function runAction(opticToken, githubToken, standardsFail, eventName, head
40384038
core.error("Unable to determine base for comparison.");
40394039
return 1;
40404040
}
4041-
const comparisonRun = await diffAll(opticToken, from);
4041+
const comparisonRun = await diffAll(opticToken, from, additionalArgs);
40424042
if (eventName === "pull_request") {
40434043
const commentResult = await prComment(githubToken, owner || "", repo || "", pr || "", sha || "");
40444044
if (!commentResult) {
@@ -4096,9 +4096,16 @@ async function deepen() {
40964096
}
40974097
return true;
40984098
}
4099-
async function diffAll(token, from) {
4099+
async function diffAll(token, from, additionalArgs) {
41004100
core.info("Running Optic diff-all");
4101-
return execCommand("optic", ["diff-all", "--compare-from", from, "--check", "--upload"], {
4101+
return execCommand("optic", [
4102+
"diff-all",
4103+
"--compare-from",
4104+
from,
4105+
"--check",
4106+
"--upload",
4107+
...(additionalArgs ? [additionalArgs] : []),
4108+
], {
41024109
env: Object.assign(Object.assign({}, process.env), { OPTIC_TOKEN: token }),
41034110
}, false);
41044111
}
@@ -4160,13 +4167,14 @@ const action_1 = __nccwpck_require__(672);
41604167
const opticToken = core.getInput("optic_token");
41614168
const githubToken = core.getInput("github_token");
41624169
const standardsFail = core.getInput("standards_fail");
4170+
const additionalArgs = core.getInput("additional_args");
41634171
const eventName = process.env.GITHUB_EVENT_NAME;
41644172
const headRef = process.env.GITHUB_REF;
41654173
const baseRef = process.env.GITHUB_BASE_REF;
41664174
const owner = process.env.GITHUB_REPOSITORY_OWNER;
41674175
const repo = (_a = process.env.GITHUB_REPOSITORY) === null || _a === void 0 ? void 0 : _a.split("/")[1];
41684176
const sha = process.env.GITHUB_SHA;
4169-
(0, action_1.runAction)(opticToken, githubToken, standardsFail, eventName, headRef, baseRef, owner, repo, sha)
4177+
(0, action_1.runAction)(opticToken, githubToken, additionalArgs, standardsFail, eventName, headRef, baseRef, owner, repo, sha)
41704178
.then((exitCode) => {
41714179
return process.exit(exitCode);
41724180
})

src/__tests__/action.test.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test("invalid input", async () => {
77
const exitCode = await runAction(
88
"optic-token",
99
"github-token",
10+
"",
1011
"true",
1112
"",
1213
"",
@@ -24,6 +25,7 @@ test("failed install", async () => {
2425
const exitCode = await runAction(
2526
"optic-token",
2627
"github-token",
28+
"",
2729
"true",
2830
"push",
2931
"refs/heads/main",
@@ -45,6 +47,7 @@ test("pull_request event", async () => {
4547
const exitCode = await runAction(
4648
"optic-token",
4749
"github-token",
50+
"",
4851
"true",
4952
"pull_request",
5053
"refs/pulls/1/merge",
@@ -68,6 +71,32 @@ test("push event", async () => {
6871
const exitCode = await runAction(
6972
"optic-token",
7073
"github-token",
74+
"",
75+
"true",
76+
"push",
77+
"refs/heads/main",
78+
undefined,
79+
"owner",
80+
"repo",
81+
"abc123"
82+
);
83+
expect(exitCode).toBe(0);
84+
assertInstall();
85+
assertDeepen();
86+
assertDiffAll();
87+
});
88+
89+
test("push event with additional-args", async () => {
90+
const assertInstall = mockInstall();
91+
const assertDeepen = mockDeepen();
92+
const assertDiffAll = mockDiffAll("optic-token", "HEAD~1", false, [
93+
"--fail-on-untracked-openapi",
94+
]);
95+
96+
const exitCode = await runAction(
97+
"optic-token",
98+
"github-token",
99+
"--fail-on-untracked-openapi",
71100
"true",
72101
"push",
73102
"refs/heads/main",
@@ -90,6 +119,7 @@ test("push event with standards failure and standards_fail set to true", async (
90119
const exitCode = await runAction(
91120
"optic-token",
92121
"github-token",
122+
"",
93123
"true",
94124
"push",
95125
"refs/heads/main",
@@ -112,6 +142,7 @@ test("push event with standards failure but standards_fail set to false", async
112142
const exitCode = await runAction(
113143
"optic-token",
114144
"github-token",
145+
"",
115146
"false",
116147
"push",
117148
"refs/heads/main",
@@ -167,7 +198,12 @@ function mockDeepen(): () => void {
167198
);
168199
}
169200

170-
function mockDiffAll(token: string, from: string, error = false): () => void {
201+
function mockDiffAll(
202+
token: string,
203+
from: string,
204+
error = false,
205+
additionalArgs: string[] = []
206+
): () => void {
171207
if (error) {
172208
jest.mocked(exec.exec).mockRejectedValue(new Error("Something broke"));
173209
} else {
@@ -177,7 +213,14 @@ function mockDiffAll(token: string, from: string, error = false): () => void {
177213
return () =>
178214
expect(jest.mocked(exec.exec)).toHaveBeenCalledWith(
179215
"optic",
180-
["diff-all", "--compare-from", from, "--check", "--upload"],
216+
[
217+
"diff-all",
218+
"--compare-from",
219+
from,
220+
"--check",
221+
"--upload",
222+
...additionalArgs,
223+
],
181224
expect.objectContaining({
182225
env: expect.objectContaining({ OPTIC_TOKEN: "optic-token" }),
183226
})

src/action.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function execCommand(
2222
export async function runAction(
2323
opticToken: string,
2424
githubToken: string,
25+
additionalArgs: string | undefined,
2526
standardsFail: string,
2627
eventName: string | undefined,
2728
headRef: string | undefined,
@@ -75,7 +76,7 @@ export async function runAction(
7576
return 1;
7677
}
7778

78-
const comparisonRun = await diffAll(opticToken, from);
79+
const comparisonRun = await diffAll(opticToken, from, additionalArgs);
7980

8081
if (eventName === "pull_request") {
8182
const commentResult = await prComment(
@@ -163,12 +164,23 @@ async function deepen(): Promise<boolean> {
163164
return true;
164165
}
165166

166-
async function diffAll(token: string, from: string): Promise<boolean> {
167+
async function diffAll(
168+
token: string,
169+
from: string,
170+
additionalArgs: string | undefined
171+
): Promise<boolean> {
167172
core.info("Running Optic diff-all");
168173

169174
return execCommand(
170175
"optic",
171-
["diff-all", "--compare-from", from, "--check", "--upload"],
176+
[
177+
"diff-all",
178+
"--compare-from",
179+
from,
180+
"--check",
181+
"--upload",
182+
...(additionalArgs ? [additionalArgs] : []),
183+
],
172184
{
173185
env: {
174186
...process.env,

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { runAction } from "./action";
44
const opticToken = core.getInput("optic_token");
55
const githubToken = core.getInput("github_token");
66
const standardsFail = core.getInput("standards_fail");
7+
const additionalArgs = core.getInput("additional_args");
78

89
const eventName = process.env.GITHUB_EVENT_NAME;
910
const headRef = process.env.GITHUB_REF;
@@ -15,6 +16,7 @@ const sha = process.env.GITHUB_SHA;
1516
runAction(
1617
opticToken,
1718
githubToken,
19+
additionalArgs,
1820
standardsFail,
1921
eventName,
2022
headRef,

0 commit comments

Comments
 (0)