From ab8326290007aad26279fd65e21f233cef9eb42c Mon Sep 17 00:00:00 2001 From: tidy-dev <75402236+tidy-dev@users.noreply.github.com> Date: Tue, 17 Oct 2023 11:50:45 -0400 Subject: [PATCH] Narrow results to PRs that have Reviews/Comments --- .../lib/stores/notifications-debug-store.ts | 69 ++++++++++++++++++- .../test-notifications/test-notifications.tsx | 9 ++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/app/src/lib/stores/notifications-debug-store.ts b/app/src/lib/stores/notifications-debug-store.ts index 05c806ca76..cf9a6af09b 100644 --- a/app/src/lib/stores/notifications-debug-store.ts +++ b/app/src/lib/stores/notifications-debug-store.ts @@ -20,6 +20,12 @@ import { PullRequestCoordinator } from './pull-request-coordinator' * notifications. */ export class NotificationsDebugStore { + private cachedComments: Map> = new Map() + private cachedReviews: Map< + number, + ReadonlyArray + > = new Map() + public constructor( private readonly accountsStore: AccountsStore, private readonly notificationsStore: NotificationsStore, @@ -44,8 +50,57 @@ export class NotificationsDebugStore { } /** Fetch all pull requests for the given repository. */ - public async getPullRequests(repository: RepositoryWithGitHubRepository) { - return this.pullRequestCoordinator.getAllPullRequests(repository) + public async getPullRequests( + repository: RepositoryWithGitHubRepository, + options: { filterByComments?: boolean; filterByReviews?: boolean } + ) { + const prs = await this.pullRequestCoordinator.getAllPullRequests(repository) + + if (!options.filterByComments && !options.filterByReviews) { + return prs + } + + const filteredPrs = [] + for (const pr of prs) { + if (options.filterByComments) { + const cachedComments = this.cachedComments.get(pr.pullRequestNumber) + + if (cachedComments && cachedComments.length > 0) { + filteredPrs.push(pr) + } + + const comments = await this.getPullRequestComments( + repository, + pr.pullRequestNumber + ) + this.cachedComments.set(pr.pullRequestNumber, comments) + + if (comments.length > 0) { + filteredPrs.push(pr) + } + } + + if (options.filterByReviews) { + const cachedReviews = this.cachedReviews.get(pr.pullRequestNumber) + + if (cachedReviews && cachedReviews.length > 0) { + filteredPrs.push(pr) + } + + const reviews = await this.getPullRequestReviews( + repository, + pr.pullRequestNumber + ) + + this.cachedReviews.set(pr.pullRequestNumber, reviews) + + if (reviews.length > 0) { + filteredPrs.push(pr) + } + } + } + + return filteredPrs } /** Fetch all reviews for the given pull request. */ @@ -53,6 +108,11 @@ export class NotificationsDebugStore { repository: RepositoryWithGitHubRepository, pullRequestNumber: number ) { + const cachedReviews = this.cachedReviews.get(pullRequestNumber) + if (cachedReviews) { + return cachedReviews + } + const api = await this.getAPIForRepository(repository.gitHubRepository) if (api === null) { return [] @@ -74,6 +134,11 @@ export class NotificationsDebugStore { repository: RepositoryWithGitHubRepository, pullRequestNumber: number ) { + const cachedComments = this.cachedComments.get(pullRequestNumber) + if (cachedComments) { + return cachedComments + } + const api = await this.getAPIForRepository(repository.gitHubRepository) if (api === null) { return [] diff --git a/app/src/ui/test-notifications/test-notifications.tsx b/app/src/ui/test-notifications/test-notifications.tsx index 4105379784..f07e700e17 100644 --- a/app/src/ui/test-notifications/test-notifications.tsx +++ b/app/src/ui/test-notifications/test-notifications.tsx @@ -368,7 +368,14 @@ export class TestNotifications extends React.Component< }) this.props.notificationsDebugStore - .getPullRequests(this.props.repository) + .getPullRequests(this.props.repository, { + filterByComments: + this.state.selectedFlow?.type === + TestNotificationType.PullRequestComment, + filterByReviews: + this.state.selectedFlow?.type === + TestNotificationType.PullRequestReview, + }) .then(pullRequests => { this.setState({ pullRequests,