From 8588acb3054eba0239edf584923d858226393f1b Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Thu, 3 Aug 2023 15:14:13 +0200 Subject: [PATCH 1/4] test commit --- src/github/pullRequest.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/github/pullRequest.ts b/src/github/pullRequest.ts index bba1e7b..b06fa5c 100644 --- a/src/github/pullRequest.ts +++ b/src/github/pullRequest.ts @@ -55,6 +55,7 @@ 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, ); From 352bbd72fb40463542c1b694bfdf1ee50c480a2d Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Thu, 3 Aug 2023 15:14:59 +0200 Subject: [PATCH 2/4] fixed user's not being taken in order --- src/github/pullRequest.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/github/pullRequest.ts b/src/github/pullRequest.ts index b06fa5c..3cbfcff 100644 --- a/src/github/pullRequest.ts +++ b/src/github/pullRequest.ts @@ -56,7 +56,38 @@ export class PullRequestApi { const reviews = request.data as PullRequestReview[]; this.logger.debug(`List of reviews: ${JSON.stringify(reviews)}`); - const approvals = reviews.filter( + const latestReviewsMap = new Map(); + + for (const review of reviews) { + if ( + review.state.localeCompare("commented", undefined, { sensitivity: "accent" }) === 0 || + review.user === null || + review.user === undefined + ) { + continue; + } + + const prevReview = latestReviewsMap.get(review.user.id); + if ( + prevReview === undefined || + // Newer reviews have a higher id number + prevReview.id < review.id + ) { + 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) => review.state.localeCompare("approved", undefined, { sensitivity: "accent" }) === 0, ); this.usersThatApprovedThePr = approvals.map((approval) => approval.user.login); From 99db02f76614a6d9558f72f116b1ac011f258cab Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Thu, 3 Aug 2023 15:17:56 +0200 Subject: [PATCH 3/4] added more comments --- src/github/pullRequest.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/github/pullRequest.ts b/src/github/pullRequest.ts index 3cbfcff..a84d89d 100644 --- a/src/github/pullRequest.ts +++ b/src/github/pullRequest.ts @@ -61,18 +61,21 @@ export class PullRequestApi { for (const review of reviews) { if ( review.state.localeCompare("commented", undefined, { sensitivity: "accent" }) === 0 || + // 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); } } From 2739895b355855569250ba5e6c2a0d7afc345707 Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Thu, 3 Aug 2023 15:32:28 +0200 Subject: [PATCH 4/4] added a case insensitive method --- src/github/pullRequest.ts | 7 +++---- src/util.ts | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/github/pullRequest.ts b/src/github/pullRequest.ts index a84d89d..0a52700 100644 --- a/src/github/pullRequest.ts +++ b/src/github/pullRequest.ts @@ -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 */ @@ -60,7 +61,7 @@ export class PullRequestApi { for (const review of reviews) { if ( - review.state.localeCompare("commented", undefined, { sensitivity: "accent" }) === 0 || + caseInsensitiveEqual(review.state, "commented") || // the user may have been deleted review.user === null || review.user === undefined @@ -90,9 +91,7 @@ export class PullRequestApi { )}`, ); - const approvals = latestReviews.filter( - (review) => review.state.localeCompare("approved", undefined, { sensitivity: "accent" }) === 0, - ); + 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)}`); diff --git a/src/util.ts b/src/util.ts index 95688d0..dad646b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -13,3 +13,10 @@ export function concatArraysUniquely(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(a: T, b: T): boolean { + return a.localeCompare(b, undefined, { sensitivity: "accent" }) === 0; +}