Skip to content

Commit

Permalink
Add forceReview flag, fixes hmarr#206
Browse files Browse the repository at this point in the history
When forceReview is true, re-approves the pull request even if current
review state is already approved
  • Loading branch information
vincejv committed Oct 19, 2022
1 parent a740555 commit a71e08b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
review-message:
description: '(optional) The message of the pull request review.'
required: false
force-review:
description: '(optional) Re-approves the PR even if status is already approved.'
required: false
runs:
using: 'node16'
main: 'dist/index.js'
28 changes: 28 additions & 0 deletions src/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,34 @@ test("when a review has already been approved by another user", async () => {
);
});

test("when a review has already been approved by another user and forceReview is set to true", async () => {
nock("https://api.github.com").get("/user").reply(200, { login: "hmarr" });

nock("https://api.github.com")
.get("/repos/hmarr/test/pulls/101")
.reply(200, { head: { sha: "24c5451bbf1fb09caa3ac8024df4788aff4d4974" } });

nock("https://api.github.com")
.get("/repos/hmarr/test/pulls/101/reviews")
.reply(200, [
{
user: { login: "some" },
commit_id: "24c5451bbf1fb09caa3ac8024df4788aff4d4974",
state: "APPROVED",
},
]);

nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(200, { id: 1 });

await approve("gh-tok", new Context(), 101, undefined, true);

expect(core.info).toHaveBeenCalledWith(
expect.stringContaining("Approved pull request #101")
);
});

test("when a review has already been approved by unknown user", async () => {
nock("https://api.github.com").get("/user").reply(200, { login: "hmarr" });

Expand Down
22 changes: 16 additions & 6 deletions src/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export async function approve(
token: string,
context: Context,
prNumber?: number,
reviewMessage?: string
reviewMessage?: string,
forceReview?: boolean,
) {
if (!prNumber) {
prNumber = context.payload.pull_request?.number;
}
if (forceReview == null) {
forceReview = false;
}

if (!prNumber) {
core.setFailed(
Expand Down Expand Up @@ -54,15 +58,21 @@ export async function approve(
review.commit_id == commit &&
review.state == "APPROVED"
) {
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
);
return;
if (forceReview) {
core.info(
`Current user already approved pull request #${prNumber}, but forceReview is set to true, so re-approving anyway`
)
} else {
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
);
return;
}
}
}

core.info(
`Pull request #${prNumber} has not been approved yet, creating approving review`
`Creating approving review for pull request #${prNumber}`
);
await client.rest.pulls.createReview({
owner: context.repo.owner,
Expand Down
34 changes: 31 additions & 3 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ test("passes the review message to approve", async () => {
"tok-xyz",
expect.anything(),
101,
"LGTM"
"LGTM",
expect.anything()
);
});

Expand All @@ -54,7 +55,8 @@ test("calls approve when no PR number is provided", async () => {
"tok-xyz",
expect.anything(),
101,
undefined
undefined,
expect.anything()
);
});

Expand All @@ -65,7 +67,8 @@ test("calls approve when a valid PR number is provided", async () => {
"tok-xyz",
expect.anything(),
456,
undefined
undefined,
expect.anything()
);
});

Expand All @@ -75,6 +78,31 @@ test("errors when an invalid PR number is provided", async () => {
expect(mockedApprove).not.toHaveBeenCalled();
});

test("calls approve when force-review is set to true", async () => {
process.env["INPUT_PULL-REQUEST-NUMBER"] = "456";
process.env["INPUT_FORCE-REVIEW"] = "true";
await run();
expect(mockedApprove).toHaveBeenCalledWith(
"tok-xyz",
expect.anything(),
456,
undefined,
true
);
});

test("calls approve when force-review is set to false", async () => {
process.env["INPUT_PULL-REQUEST-NUMBER"] = "456";
await run();
expect(mockedApprove).toHaveBeenCalledWith(
"tok-xyz",
expect.anything(),
456,
undefined,
false
);
});

function ghContext(): Context {
const ctx = new Context();
ctx.payload = {
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export async function run() {
token,
github.context,
prNumber(),
reviewMessage || undefined
reviewMessage || undefined,
forceReview()
);
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -39,6 +40,13 @@ function prNumber(): number {
return github.context.payload.pull_request.number;
}

function forceReview(): boolean {
if (core.getInput("force-review") === undefined) {
return false;
}
return (/true/i).test(core.getInput("force-review"))
}

if (require.main === module) {
run();
}

0 comments on commit a71e08b

Please sign in to comment.