Skip to content

Commit

Permalink
Only consider the latest review for a user (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Marr committed Mar 15, 2023
1 parent 0902bf8 commit 4488819
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
9 changes: 8 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/approve.test.ts
Expand Up @@ -145,6 +145,28 @@ test("when a review is dismissed", async () => {
expect(createReview.isDone()).toBe(true);
});

test("when a review is dismissed, but an earlier review is approved", async () => {
apiMocks.getUser();
apiMocks.getPull();
apiMocks.getReviews(200, [
{
user: { login: "hmarr" },
commit_id: "6a9ec7556f0a7fa5b49527a1eea4878b8a22d2e0",
state: "APPROVED",
},
{
user: { login: "hmarr" },
commit_id: "24c5451bbf1fb09caa3ac8024df4788aff4d4974",
state: "DISMISSED",
},
]);
const createReview = apiMocks.createReview();

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

expect(createReview.isDone()).toBe(true);
});

test("when a review is not approved", async () => {
apiMocks.getUser();
apiMocks.getPull();
Expand Down
13 changes: 10 additions & 3 deletions src/approve.ts
Expand Up @@ -39,12 +39,19 @@ export async function approve(
const prHead = pr.head.sha;
core.info(`Commit SHA is ${prHead}`);

const alreadyReviewed = reviews.some(
({ user, state }) => user?.login === login && state === "APPROVED"
);
// Only the most recent review for a user counts towards the review state
const latestReviewForUser = [...reviews]
.reverse()
.find(({ user }) => user?.login === login);
const alreadyReviewed = latestReviewForUser?.state === "APPROVED";

// If there's an approved review from a user, but there's an outstanding review request,
// we need to create a new review. Review requests mean that existing "APPROVED" reviews
// don't count towards the mergeability of the PR.
const outstandingReviewRequest = pr.requested_reviewers?.some(
(reviewer) => reviewer.login == login
);

if (alreadyReviewed && !outstandingReviewRequest) {
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
Expand Down

0 comments on commit 4488819

Please sign in to comment.