Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hmarr/auto-approve-action
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.2.0
Choose a base ref
...
head repository: hmarr/auto-approve-action
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.2.1
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Mar 15, 2023

  1. Only consider the latest review for a user (#216)

    Harry Marr authored Mar 15, 2023
    Copy the full SHA
    4488819 View commit details
Showing with 40 additions and 4 deletions.
  1. +8 −1 dist/index.js
  2. +22 −0 src/approve.test.ts
  3. +10 −3 src/approve.ts
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
@@ -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();
13 changes: 10 additions & 3 deletions src/approve.ts
Original file line number Diff line number Diff line change
@@ -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`