Skip to content

Commit 4488819

Browse files
author
Harry Marr
authored
Only consider the latest review for a user (#216)
1 parent 0902bf8 commit 4488819

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

dist/index.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/approve.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ test("when a review is dismissed", async () => {
145145
expect(createReview.isDone()).toBe(true);
146146
});
147147

148+
test("when a review is dismissed, but an earlier review is approved", async () => {
149+
apiMocks.getUser();
150+
apiMocks.getPull();
151+
apiMocks.getReviews(200, [
152+
{
153+
user: { login: "hmarr" },
154+
commit_id: "6a9ec7556f0a7fa5b49527a1eea4878b8a22d2e0",
155+
state: "APPROVED",
156+
},
157+
{
158+
user: { login: "hmarr" },
159+
commit_id: "24c5451bbf1fb09caa3ac8024df4788aff4d4974",
160+
state: "DISMISSED",
161+
},
162+
]);
163+
const createReview = apiMocks.createReview();
164+
165+
await approve("gh-tok", new Context(), 101);
166+
167+
expect(createReview.isDone()).toBe(true);
168+
});
169+
148170
test("when a review is not approved", async () => {
149171
apiMocks.getUser();
150172
apiMocks.getPull();

src/approve.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ export async function approve(
3939
const prHead = pr.head.sha;
4040
core.info(`Commit SHA is ${prHead}`);
4141

42-
const alreadyReviewed = reviews.some(
43-
({ user, state }) => user?.login === login && state === "APPROVED"
44-
);
42+
// Only the most recent review for a user counts towards the review state
43+
const latestReviewForUser = [...reviews]
44+
.reverse()
45+
.find(({ user }) => user?.login === login);
46+
const alreadyReviewed = latestReviewForUser?.state === "APPROVED";
47+
48+
// If there's an approved review from a user, but there's an outstanding review request,
49+
// we need to create a new review. Review requests mean that existing "APPROVED" reviews
50+
// don't count towards the mergeability of the PR.
4551
const outstandingReviewRequest = pr.requested_reviewers?.some(
4652
(reviewer) => reviewer.login == login
4753
);
54+
4855
if (alreadyReviewed && !outstandingReviewRequest) {
4956
core.info(
5057
`Current user already approved pull request #${prNumber}, nothing to do`

0 commit comments

Comments
 (0)