From df93c0e722e14692c2effc667111147bf68ee0df Mon Sep 17 00:00:00 2001 From: Matt Fiddaman Date: Tue, 13 Aug 2024 02:57:24 +0100 Subject: [PATCH] Change labels to only consider reviews from users with push access (#23) * change approved label to only consider reviews from users with push access * all non-push reviews are ignored by labels --- src/classes/PullRequest.ts | 39 ++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/classes/PullRequest.ts b/src/classes/PullRequest.ts index 4f8c1ca..04e1769 100644 --- a/src/classes/PullRequest.ts +++ b/src/classes/PullRequest.ts @@ -124,8 +124,17 @@ export default class PullRequest { if (reviews.length < 1) return 'readyForReview'; - const latestReviewsObj: { [key: number]: { state: string; time: number } } = - {}; + const pushUsers = ( + await this.octokit.repos.listCollaborators({ + owner: this.owner, + repo: this.repo, + permission: 'push', + }) + ).data.map(u => u.id); + + const latestReviewsObj: { + [key: number]: { state: string; time: number }; + } = {}; for (const r of reviews) { if (!r?.user?.id || !r.submitted_at) continue; @@ -133,25 +142,27 @@ export default class PullRequest { const user = r.user.id; const time = new Date(r.submitted_at).getTime(); - if (!latestReviewsObj[user]) { - latestReviewsObj[user] = { - state: r.state, - time, - }; + if (!pushUsers.includes(user)) continue; + const review = { + state: r.state, + time, + }; + + if (!latestReviewsObj[user]) { + latestReviewsObj[user] = review; continue; } if (latestReviewsObj[user].time < time) { - latestReviewsObj[user] = { - state: r.state, - time, - }; + latestReviewsObj[user] = review; } } const latestReviews = Object.values(latestReviewsObj); + if (!latestReviews.length) return 'readyForReview'; + if (latestReviews.filter(r => r.state === 'CHANGES_REQUESTED').length > 0) { return 'changesRequested'; } @@ -165,6 +176,10 @@ export default class PullRequest { return 'needsMoreApprovals'; } - return 'approved'; + if (approvingReviews > 0) { + return 'approved'; + } + + return 'readyForReview'; } }