Merge pull request #128 from github/fix-hasAlreadyApproved-method

fix hasAlreadyApproved() method
This commit is contained in:
Grant Birkinbine 2023-11-28 14:18:03 -07:00 коммит произвёл GitHub
Родитель 5c60fbf967 43298e0a4a
Коммит f8d60d9344
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 29 добавлений и 21 удалений

24
dist/index.js сгенерированный поставляемый
Просмотреть файл

@ -37702,30 +37702,34 @@ class GitHubProvider {
`checking if ${login} has already approved PR #${prNumber} in a previous workflow run`, `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({ const { data: reviews } = await this.octokit.rest.pulls.listReviews({
owner: github.context.repo.owner, owner: github.context.repo.owner,
repo: github.context.repo.repo, repo: github.context.repo.repo,
pull_number: prNumber, pull_number: prNumber,
}); });
// filter out all reviews that are not APPROVED // filter the reviews to only those from the current user and sort them by date descending
const approvedReviews = reviews.filter( const userReviews = reviews
(review) => review.state === "APPROVED", .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 // attempt to get the latest review from the user (if there are no reviews from the user, this will be undefined)
const approvedReviewsByUser = approvedReviews.filter( const latestReview = userReviews[0];
(review) => review.user.login.toLowerCase() === login.toLowerCase(),
);
const approved = approvedReviewsByUser.length > 0; // check if the latest review from the user is an APPROVED review and not undefined
const approved = latestReview && latestReview.state === "APPROVED";
// log the result
if (approved) { if (approved) {
core.info( core.info(
`${login} has already approved PR #${prNumber} in a previous workflow run`, `${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; return approved;
} }

2
dist/index.js.map сгенерированный поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -48,30 +48,34 @@ class GitHubProvider {
`checking if ${login} has already approved PR #${prNumber} in a previous workflow run`, `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({ const { data: reviews } = await this.octokit.rest.pulls.listReviews({
owner: github.context.repo.owner, owner: github.context.repo.owner,
repo: github.context.repo.repo, repo: github.context.repo.repo,
pull_number: prNumber, pull_number: prNumber,
}); });
// filter out all reviews that are not APPROVED // filter the reviews to only those from the current user and sort them by date descending
const approvedReviews = reviews.filter( const userReviews = reviews
(review) => review.state === "APPROVED", .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 // attempt to get the latest review from the user (if there are no reviews from the user, this will be undefined)
const approvedReviewsByUser = approvedReviews.filter( const latestReview = userReviews[0];
(review) => review.user.login.toLowerCase() === login.toLowerCase(),
);
const approved = approvedReviewsByUser.length > 0; // check if the latest review from the user is an APPROVED review and not undefined
const approved = latestReview && latestReview.state === "APPROVED";
// log the result
if (approved) { if (approved) {
core.info( core.info(
`${login} has already approved PR #${prNumber} in a previous workflow run`, `${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; return approved;
} }