Skip to content

Commit

Permalink
fix: Add a check in PR's requested_reviewers
Browse files Browse the repository at this point in the history
Add a check in PR's `requested_reviewers` before considering the PR as already approved, fixes the issue wherein action does not approve a PR was approved before but requested a re-review, fixes hmarr#206
  • Loading branch information
vincejv committed Oct 20, 2022
1 parent a740555 commit 0932a83
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
43 changes: 41 additions & 2 deletions src/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,21 @@ test("when a review has already been approved by current user", async () => {

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

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

Expand All @@ -134,6 +140,39 @@ test("when a review has already been approved by current user", async () => {
);
});

test("when a review has been previously approved by user and but requests a re-review", 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" },
requested_reviewers: [
{ login: "hmarr" }
]
});

nock("https://api.github.com")
.get("/repos/hmarr/test/pulls/101/reviews")
.reply(200, [
{
user: null,
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);

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

test("when a review is pending", async () => {
nock("https://api.github.com").get("/user").reply(200, { login: "hmarr" });

Expand Down
16 changes: 10 additions & 6 deletions src/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ export async function approve(
repo: context.repo.repo,
pull_number: prNumber,
});

for (const review of reviews.data) {
if (
review.user?.login == login &&
review.commit_id == commit &&
review.state == "APPROVED"
review.state == "APPROVED" &&
pull_request.data
.requested_reviewers?.filter(
reviewer => reviewer.login == login
).length == 0
) {
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
);
return;
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
);
return;
}
}

Expand Down

0 comments on commit 0932a83

Please sign in to comment.