Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed user's review not being taken in order #39

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/github/pullRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PullRequest, PullRequestReview } from "@octokit/webhooks-types";

import { caseInsensitiveEqual } from "../util";
import { ActionLogger, GitHubClient } from "./types";

/** API class that uses the default token to access the data from the pull request and the repository */
Expand Down Expand Up @@ -55,9 +56,42 @@ export class PullRequestApi {
const request = await this.api.rest.pulls.listReviews({ ...this.repoInfo, pull_number: this.number });
const reviews = request.data as PullRequestReview[];
this.logger.debug(`List of reviews: ${JSON.stringify(reviews)}`);
const approvals = reviews.filter(
(review) => review.state.localeCompare("approved", undefined, { sensitivity: "accent" }) === 0,

const latestReviewsMap = new Map<number, PullRequestReview>();

for (const review of reviews) {
if (
caseInsensitiveEqual(review.state, "commented") ||
// the user may have been deleted
review.user === null ||
review.user === undefined
) {
continue;
}

// we check if there is already a review from this user
const prevReview = latestReviewsMap.get(review.user.id);
if (
prevReview === undefined ||
// Newer reviews have a higher id number
prevReview.id < review.id
) {
// if the review is more modern (and not a comment) we replace the one in our map
latestReviewsMap.set(review.user.id, review);
}
}

const latestReviews = Array.from(latestReviewsMap.values());

this.logger.info(
`Latest reviews are ${JSON.stringify(
latestReviews.map((r) => {
return { user: r.user.login, state: r.state };
}),
)}`,
);

const approvals = latestReviews.filter((review) => caseInsensitiveEqual(review.state, "approved"));
this.usersThatApprovedThePr = approvals.map((approval) => approval.user.login);
}
this.logger.debug(`PR approvals are ${JSON.stringify(this.usersThatApprovedThePr)}`);
Expand Down
7 changes: 7 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export function concatArraysUniquely<T>(arr1?: T[], arr2?: T[]): T[] {
// We remove the duplicated values and return the array
return concatedArray.filter((item, pos) => concatedArray.indexOf(item) === pos);
}

/** Case insentive comparison of two strings
* @example caseInsensitiveEqual("hi", "HI") === true
*/
export function caseInsensitiveEqual<T extends string>(a: T, b: T): boolean {
return a.localeCompare(b, undefined, { sensitivity: "accent" }) === 0;
}