Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit d87e821

Browse files
authored
Merge pull request #22 from DanHoerst/check_diff
Add checkDiff option
2 parents 60a36be + 433c5d3 commit d87e821

File tree

11 files changed

+306
-54
lines changed

11 files changed

+306
-54
lines changed

__tests__/github-provider.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ test("It lists labels on a PR", async () => {
4949
let provider = new GitHubProvider("token");
5050
await provider.listLabelsOnPR(1);
5151
});
52+
53+
test("It gets the diff of a PR", async () => {
54+
process.env["GITHUB_REPOSITORY"] = "foo/bar";
55+
56+
nock("https://api.github.com")
57+
.persist()
58+
.get("/repos/foo/bar/pulls/1")
59+
.reply(200);
60+
61+
let provider = new GitHubProvider("token");
62+
await provider.getPRDiff(1);
63+
});

__tests__/pull-request.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,26 @@ test("We can list labels", async () => {
4848
let labels = await pullRequest.listLabels();
4949
expect(labels).toStrictEqual(prLabels);
5050
});
51+
52+
test("We can get the diff", async () => {
53+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
54+
index 2f4e8d9..93c2072 100644
55+
--- a/.github/workflows/check-dist.yml
56+
+++ b/.github/workflows/check-dist.yml
57+
@@ -44,7 +44,7 @@ jobs:
58+
id: diff
59+
60+
# If index.js was different than expected, upload the expected version as an artifact
61+
- - uses: actions/upload-artifact@v2
62+
+ - uses: actions/upload-artifact@v3
63+
if: blah
64+
with:
65+
name: dist`;
66+
let provider = new GitHubProvider("token");
67+
let spy = jest.spyOn(provider, "getPRDiff").mockImplementation(() => prDiff);
68+
expect(provider.getPRDiff()).toBe(prDiff);
69+
70+
let pullRequest = new PullRequest(provider);
71+
let diff = await pullRequest.getDiff();
72+
expect(diff).toStrictEqual(prDiff);
73+
});

__tests__/runner.test.js

Lines changed: 136 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ jest.spyOn(core, "debug").mockImplementation(() => {});
1616
jest.spyOn(core, "setFailed").mockImplementation(() => {});
1717
jest.spyOn(core, "setOutput").mockImplementation(() => {});
1818

19+
afterEach(() => {
20+
process.env["INPUT_CHECKCOMMITS"] = "";
21+
process.env["INPUT_CHECKLABELS"] = "";
22+
process.env["INPUT_CHECKDIFF"] = "";
23+
});
24+
1925
describe("processCommits", () => {
2026
test("We process commits successfully", async () => {
2127
let prCommits = [{ author: { login: "robot" } }];
@@ -43,6 +49,53 @@ describe("processCommits", () => {
4349
});
4450
});
4551

52+
describe("processDiff", () => {
53+
test("We process the diff successfully", async () => {
54+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
55+
index 2f4e8d9..93c2072 100644
56+
--- a/.github/workflows/check-dist.yml
57+
+++ b/.github/workflows/check-dist.yml
58+
@@ -44,7 +44,7 @@ jobs:
59+
id: diff
60+
61+
# If index.js was different than expected, upload the expected version as an artifact
62+
- - uses: actions/upload-artifact@v2
63+
if: blah
64+
with:
65+
name: dist`;
66+
let spy = jest
67+
.spyOn(pullRequest, "getDiff")
68+
.mockImplementation(() => prDiff);
69+
expect(pullRequest.getDiff()).toBe(prDiff);
70+
71+
let diff = await runner.processDiff();
72+
expect(diff).toStrictEqual(true);
73+
});
74+
75+
test("We process the diff unsuccessfully", async () => {
76+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
77+
index 2f4e8d9..93c2072 100644
78+
--- a/.github/workflows/check-dist.yml
79+
+++ b/.github/workflows/check-dist.yml
80+
@@ -44,7 +44,7 @@ jobs:
81+
id: diff
82+
83+
# If index.js was different than expected, upload the expected version as an artifact
84+
- - uses: actions/upload-artifact@v2
85+
+ - uses: actions/upload-artifact@v3
86+
if: blah
87+
with:
88+
name: dist`;
89+
let spy = jest
90+
.spyOn(pullRequest, "getDiff")
91+
.mockImplementation(() => prDiff);
92+
expect(pullRequest.getDiff()).toBe(prDiff);
93+
94+
let diff = await runner.processDiff();
95+
expect(diff).toStrictEqual(false);
96+
});
97+
});
98+
4699
describe("labelsEqual", () => {
47100
test("We process labels successfully", async () => {
48101
let prLabels = ["bug", "feature-request"];
@@ -90,8 +143,10 @@ describe("processLabels", () => {
90143
});
91144

92145
describe("processPrivilegedReviewer", () => {
93-
test("We process commits and labels successfully with all options enabled", async () => {
146+
test("We process commits, diff and labels successfully with all options enabled", async () => {
94147
process.env["INPUT_CHECKCOMMITS"] = "true";
148+
process.env["INPUT_CHECKLABELS"] = "true";
149+
process.env["INPUT_CHECKDIFF"] = "true";
95150
let prLabels = [{ name: "bug" }, { name: "feature-request" }];
96151
let spyLabels = jest
97152
.spyOn(pullRequest, "listLabels")
@@ -104,20 +159,31 @@ describe("processPrivilegedReviewer", () => {
104159
.mockImplementation(() => prCommits);
105160
expect(pullRequest.listCommits()).toBe(prCommits);
106161

107-
let commits = await runner.processPrivilegedReviewer("robot", {
162+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
163+
index 2f4e8d9..93c2072 100644
164+
--- a/.github/workflows/check-dist.yml
165+
+++ b/.github/workflows/check-dist.yml
166+
@@ -44,7 +44,7 @@ jobs:
167+
id: diff
168+
169+
# If index.js was different than expected, upload the expected version as an artifact
170+
- - uses: actions/upload-artifact@v2
171+
if: blah
172+
with:
173+
name: dist`;
174+
let spyDiff = jest
175+
.spyOn(pullRequest, "getDiff")
176+
.mockImplementation(() => prDiff);
177+
expect(pullRequest.getDiff()).toBe(prDiff);
178+
179+
let processed = await runner.processPrivilegedReviewer("robot", {
108180
labels: ["bug", "feature-request"],
109181
});
110-
expect(commits).toStrictEqual(true);
111-
process.env["INPUT_CHECKCOMMITS"] = "";
182+
expect(processed).toStrictEqual(true);
112183
});
113184

114185
test("We process commits unsuccessfully with the option enabled", async () => {
115186
process.env["INPUT_CHECKCOMMITS"] = "true";
116-
let prLabels = [{ name: "bug" }, { name: "feature-request" }];
117-
let spyLabels = jest
118-
.spyOn(pullRequest, "listLabels")
119-
.mockImplementation(() => prLabels);
120-
expect(pullRequest.listLabels()).toBe(prLabels);
121187

122188
let prCommits = [
123189
{ author: { login: "robot" } },
@@ -128,20 +194,13 @@ describe("processPrivilegedReviewer", () => {
128194
.mockImplementation(() => prCommits);
129195
expect(pullRequest.listCommits()).toBe(prCommits);
130196

131-
let commits = await runner.processPrivilegedReviewer("robot", {
197+
let processed = await runner.processPrivilegedReviewer("robot", {
132198
labels: ["bug", "feature-request"],
133199
});
134-
expect(commits).toStrictEqual(false);
135-
process.env["INPUT_CHECKCOMMITS"] = "";
200+
expect(processed).toStrictEqual(false);
136201
});
137202

138203
test("We allow bad commits when the option to check them is not set", async () => {
139-
let prLabels = [{ name: "bug" }, { name: "feature-request" }];
140-
let spyLabels = jest
141-
.spyOn(pullRequest, "listLabels")
142-
.mockImplementation(() => prLabels);
143-
expect(pullRequest.listLabels()).toBe(prLabels);
144-
145204
let prCommits = [
146205
{ author: { login: "robot" } },
147206
{ author: { login: "malicious" } },
@@ -151,10 +210,10 @@ describe("processPrivilegedReviewer", () => {
151210
.mockImplementation(() => prCommits);
152211
expect(pullRequest.listCommits()).toBe(prCommits);
153212

154-
let commits = await runner.processPrivilegedReviewer("robot", {
155-
labels: ["bug", "feature-request"],
213+
let processed = await runner.processPrivilegedReviewer("robot", {
214+
labels: [],
156215
});
157-
expect(commits).toStrictEqual(true);
216+
expect(processed).toStrictEqual(true);
158217
});
159218

160219
test("We process labels unsuccessfully with the option enabled", async () => {
@@ -165,35 +224,73 @@ describe("processPrivilegedReviewer", () => {
165224
.mockImplementation(() => prLabels);
166225
expect(pullRequest.listLabels()).toBe(prLabels);
167226

168-
let prCommits = [{ author: { login: "robot" } }];
169-
let spyCommits = jest
170-
.spyOn(pullRequest, "listCommits")
171-
.mockImplementation(() => prCommits);
172-
expect(pullRequest.listCommits()).toBe(prCommits);
173-
174-
let commits = await runner.processPrivilegedReviewer("robot", {
227+
let processed = await runner.processPrivilegedReviewer("robot", {
175228
labels: ["new", "feature-request"],
176229
});
177-
expect(commits).toStrictEqual(false);
178-
process.env["INPUT_CHECKLABELS"] = "";
230+
expect(processed).toStrictEqual(false);
179231
});
180232

181-
test("We allow bad commits when the option to check them is not set", async () => {
233+
test("We allow bad labels when the option to check them is not set", async () => {
182234
let prLabels = [{ name: "bug" }, { name: "feature-request" }];
183235
let spyLabels = jest
184236
.spyOn(pullRequest, "listLabels")
185237
.mockImplementation(() => prLabels);
186238
expect(pullRequest.listLabels()).toBe(prLabels);
187239

188-
let prCommits = [{ author: { login: "robot" } }];
189-
let spyCommits = jest
190-
.spyOn(pullRequest, "listCommits")
191-
.mockImplementation(() => prCommits);
192-
expect(pullRequest.listCommits()).toBe(prCommits);
193-
194-
let commits = await runner.processPrivilegedReviewer("robot", {
240+
let processed = await runner.processPrivilegedReviewer("robot", {
195241
labels: ["new", "feature-request"],
196242
});
197-
expect(commits).toStrictEqual(true);
243+
expect(processed).toStrictEqual(true);
244+
});
245+
246+
test("We process the diff unsuccessfully with the option enabled", async () => {
247+
process.env["INPUT_CHECKDIFF"] = "true";
248+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
249+
index 2f4e8d9..93c2072 100644
250+
--- a/.github/workflows/check-dist.yml
251+
+++ b/.github/workflows/check-dist.yml
252+
@@ -44,7 +44,7 @@ jobs:
253+
id: diff
254+
255+
# If index.js was different than expected, upload the expected version as an artifact
256+
- - uses: actions/upload-artifact@v2
257+
+ - uses: actions/upload-artifact@v3
258+
if: blah
259+
with:
260+
name: dist`;
261+
let spyDiff = jest
262+
.spyOn(pullRequest, "getDiff")
263+
.mockImplementation(() => prDiff);
264+
expect(pullRequest.getDiff()).toBe(prDiff);
265+
266+
let processed = await runner.processPrivilegedReviewer("robot", {
267+
labels: [],
268+
});
269+
expect(processed).toStrictEqual(false);
270+
});
271+
272+
test("We allow a bad diff when the option to check them is not set", async () => {
273+
let prDiff = `| diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml
274+
index 2f4e8d9..93c2072 100644
275+
--- a/.github/workflows/check-dist.yml
276+
+++ b/.github/workflows/check-dist.yml
277+
@@ -44,7 +44,7 @@ jobs:
278+
id: diff
279+
280+
# If index.js was different than expected, upload the expected version as an artifact
281+
- - uses: actions/upload-artifact@v2
282+
+ - uses: actions/upload-artifact@v3
283+
if: blah
284+
with:
285+
name: dist`;
286+
let spyDiff = jest
287+
.spyOn(pullRequest, "getDiff")
288+
.mockImplementation(() => prDiff);
289+
expect(pullRequest.getDiff()).toBe(prDiff);
290+
291+
let processed = await runner.processPrivilegedReviewer("robot", {
292+
labels: [],
293+
});
294+
expect(processed).toStrictEqual(true);
198295
});
199296
});

.github/workflows/privileged-requester.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ jobs:
1919
prCreator: ${{ github.event.pull_request.user.login }}
2020
prNumber: ${{ github.event.pull_request.number }}
2121
checkCommits: "true"
22+
checkDiff: "true"
2223
checkLabels: "true"

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ Here are the configuration options for this Action:
5454

5555
## Inputs 📥
5656

57-
| Input | Required? | Default | Description |
58-
|----------------| --------- |---------------------------------------------| ----------- |
59-
| myToken | yes | ${{ github.token }} | The GitHub token used to create an authenticated client - Provided for you by default! |
57+
| Input | Required? | Default | Description |
58+
|-----------| --------- |---------------------------------------------| ----------- |
59+
| myToken | yes | ${{ github.token }} | The GitHub token used to create an authenticated client - Provided for you by default! |
6060
| robotUserToken | no | - | An alternative robot user PAT to be used instead of the built-in Actions token |
61-
| path | yes | config/privileged-requester.yaml | Path where the privileged requester configuration can be found |
62-
| prCreator | yes | ${{ github.event.pull_request.user.login }} | The creator of the PR for this pull request event |
63-
| prNumber | yes | ${{ github.event.pull_request.number }} | The number of the PR for this pull request event |
64-
| checkCommits | yes | "true" | An option to check that every commit in the PR is made from the privileged requester |
65-
| checkLabels | yes | "true" | An option to check that the labels on the PR match those defined in the privileged requester config |
61+
| path | yes | config/privileged-requester.yaml | Path where the privileged requester configuration can be found |
62+
| prCreator | yes | ${{ github.event.pull_request.user.login }} | The creator of the PR for this pull request event |
63+
| prNumber | yes | ${{ github.event.pull_request.number }} | The number of the PR for this pull request event |
64+
| checkCommits | yes | "true" | An option to check that every commit in the PR is made from the privileged requester |
65+
| checkDiff | yes | "true" | An option to check that the PR diff only has a removal diff, with no additions |
66+
| checkLabels | yes | "true" | An option to check that the labels on the PR match those defined in the privileged requester config |
6667

6768
## Outputs 📤
6869

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ inputs:
2727
description: 'An option to check that every commit in the PR is made from the privileged requester'
2828
required: true
2929
default: 'true'
30+
checkDiff:
31+
description: 'An option to check that the PR diff only has a removal diff, with no additions'
32+
required: true
33+
default: 'true'
3034
checkLabels:
3135
description: 'An option to check that the labels on the PR match those defined in the privileged requester config'
3236
required: true

0 commit comments

Comments
 (0)