Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider the latest review for a user #216

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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