Merge pull request #128 from github/fix-hasAlreadyApproved-method
fix hasAlreadyApproved() method
This commit is contained in:
Коммит
f8d60d9344
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче