Skip to content

Commit

Permalink
change approved label to only consider reviews from users with push a…
Browse files Browse the repository at this point in the history
…ccess
  • Loading branch information
matt-fidd committed Aug 5, 2024
1 parent 37d1234 commit b26a1a1
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/classes/PullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,47 +124,61 @@ 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; user: number };
} = {};

for (const r of reviews) {
if (!r?.user?.id || !r.submitted_at) continue;

const user = r.user.id;
const time = new Date(r.submitted_at).getTime();

if (!latestReviewsObj[user]) {
latestReviewsObj[user] = {
state: r.state,
time,
};
const review = {
state: r.state,
time,
user,
};

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';
}

const approvingReviews = latestReviews.filter(
r => r.state === 'APPROVED',
r => r.state === 'APPROVED' && pushUsers.includes(r.user),
).length;
const requiredReviews = await this.getRequiredReviews();

if (requiredReviews > approvingReviews) {
return 'needsMoreApprovals';
}

return 'approved';
if (approvingReviews > 0) {
return 'approved';
}

return 'readyForReview';
}
}

0 comments on commit b26a1a1

Please sign in to comment.