Skip to content

Commit

Permalink
feat: mark as approved when a reviewer was re requested
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-ae committed Nov 5, 2024
1 parent 19aa267 commit 656ff40
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions src/utils/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,37 @@ export async function getAllPullRequestReviews(context: Context, pullNumber: num
}
}

async function getReviewRequestsTimeline(context: Context, pullNumber: number, owner: string, repo: string) {
const events = [];
let page = 1;
const perPage = 100;
try {
while (true) {
const response = await context.octokit.request(`GET /repos/${owner}/${repo}/issues/${pullNumber}/timeline`, {
owner,
repo,
issue_number: pullNumber,
page,
per_page: perPage,
});

const reviewRequestEvents = response.data.filter(
(event: { event: string }) => event.event === "review_requested" || event.event === "review_request_removed"
);

events.push(...reviewRequestEvents);

if (response.data.length < perPage) break;
page++;
}

return events;
} catch (error) {
console.error("Error fetching review request timeline events:", error);
return [];
}
}

export function getOwnerRepoFromHtmlUrl(url: string) {
const parts = url.split("/");
if (parts.length < 5) {
Expand All @@ -245,21 +276,38 @@ export async function getAvailableOpenedPullRequests(context: Context, username:
const { owner, repo } = getOwnerRepoFromHtmlUrl(openedPullRequest.html_url);
const reviews = await getAllPullRequestReviews(context, openedPullRequest.number, owner, repo);

if (reviews.length > 0) {
const approvedReviews = reviews.find((review) => review.state === "APPROVED");
if (approvedReviews) {
approved.push(openedPullRequest);
}
// Determine the latest review state
const latestReview = reviews[reviews.length - 1];
const latestReviewState = latestReview?.state;

if (latestReviewState === "APPROVED") {
approved.push(openedPullRequest);
continue;
}

if (reviews.length > 0) {
const changesRequested = reviews.find((review) => review.state === "CHANGES_REQUESTED");
if (changesRequested) {
changes.push(openedPullRequest);
if (latestReviewState === "CHANGES_REQUESTED") {
changes.push(openedPullRequest);

// Track the time of the last "CHANGES_REQUESTED"s
const lastChangesRequestedTime = latestReview.submitted_at ? new Date(latestReview.submitted_at).getTime() : null;

// Fetch timeline or comments to check if reviewer has been re-requested
const reviewRequests = await getReviewRequestsTimeline(context, openedPullRequest.number, owner, repo);

// Find if any review request was made after the last changes requested
const isReviewRequestedAfterChanges = lastChangesRequestedTime
? reviewRequests.some((request) => new Date(request.created_at).getTime() > lastChangesRequestedTime)
: false;

// If reviewer was re-requested after changes were requested, mark as completed and increment approved count
if (isReviewRequestedAfterChanges) {
approved.push(openedPullRequest);
changes.pop();
continue;
}
}

if (reviews.length === 0 && new Date().getTime() - new Date(openedPullRequest.created_at).getTime() >= getTimeValue(reviewDelayTolerance)) {
if (reviews.length === 0 && new Date().getTime() - new Date(openedPullRequest.created_at).getTime() >= getTimeValue("1m")) {
approved.push(openedPullRequest);
}
}
Expand Down

0 comments on commit 656ff40

Please sign in to comment.