Skip to content

Commit

Permalink
fix hasAlreadyApproved() method
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Nov 28, 2023
1 parent 5c60fbf commit 43298e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
24 changes: 14 additions & 10 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/github-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,34 @@ class GitHubProvider {
`checking if ${login} has already approved PR #${prNumber} in a previous workflow run`,
);

// get all reviews on the PR
const { data: reviews } = await this.octokit.rest.pulls.listReviews({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber,
});

// filter out all reviews that are not APPROVED
const approvedReviews = reviews.filter(
(review) => review.state === "APPROVED",
);
// filter the reviews to only those from the current user and sort them by date descending
const userReviews = reviews
.filter(
(review) => review.user.login.toLowerCase() === login.toLowerCase(),
)
.sort((a, b) => new Date(b.submitted_at) - new Date(a.submitted_at));

// filter out all reviews that are not by the current authenticated user via login
const approvedReviewsByUser = approvedReviews.filter(
(review) => review.user.login.toLowerCase() === login.toLowerCase(),
);
// attempt to get the latest review from the user (if there are no reviews from the user, this will be undefined)
const latestReview = userReviews[0];

// check if the latest review from the user is an APPROVED review and not undefined
const approved = latestReview && latestReview.state === "APPROVED";

const approved = approvedReviewsByUser.length > 0;
// log the result
if (approved) {
core.info(
`${login} has already approved PR #${prNumber} in a previous workflow run`,
);
}

// if there are any reviews left, then login (this Action) has already approved the PR and we should not approve it again
// return the result (true if the user has an active APPROVED review, false otherwise)
return approved;
}

Expand Down

0 comments on commit 43298e0

Please sign in to comment.