|
| 1 | +import { relayClient } from "@/shared/api/relayClient"; |
| 2 | +import type { RelayEvent } from "@/shared/api/types"; |
| 3 | +import { |
| 4 | + KIND_GIT_ISSUE, |
| 5 | + KIND_GIT_PR_UPDATE, |
| 6 | + KIND_GIT_PULL_REQUEST, |
| 7 | + KIND_GIT_STATUS_CLOSED, |
| 8 | + KIND_GIT_STATUS_DRAFT, |
| 9 | + KIND_GIT_STATUS_MERGED, |
| 10 | + KIND_GIT_STATUS_OPEN, |
| 11 | + KIND_TEXT_NOTE, |
| 12 | +} from "@/shared/constants/kinds"; |
| 13 | +import { |
| 14 | + getTag, |
| 15 | + type ProjectIssue, |
| 16 | + projectIssueEventsToIssues, |
| 17 | +} from "./projectIssues.mjs"; |
| 18 | +import { |
| 19 | + type ProjectPullRequest, |
| 20 | + projectPullRequestEventsToPullRequests, |
| 21 | +} from "./projectPullRequests.mjs"; |
| 22 | + |
| 23 | +type ProjectReference = { |
| 24 | + repoAddress: string; |
| 25 | +}; |
| 26 | + |
| 27 | +/** Optional event groups that can fail without discarding root work items. */ |
| 28 | +export type ProjectWorkItemSection = |
| 29 | + | "comments" |
| 30 | + | "pull-request-updates" |
| 31 | + | "statuses"; |
| 32 | + |
| 33 | +/** Aggregate work items plus any optional event groups that failed to load. */ |
| 34 | +export type ProjectsWorkItemsResult<TProject extends ProjectReference> = { |
| 35 | + issues: { |
| 36 | + items: Array<{ project: TProject; issue: ProjectIssue }>; |
| 37 | + failedSections: ProjectWorkItemSection[]; |
| 38 | + }; |
| 39 | + pullRequests: { |
| 40 | + items: Array<{ project: TProject; pullRequest: ProjectPullRequest }>; |
| 41 | + failedSections: ProjectWorkItemSection[]; |
| 42 | + }; |
| 43 | +}; |
| 44 | + |
| 45 | +function groupByRepoAddress(events: RelayEvent[]): Map<string, RelayEvent[]> { |
| 46 | + const grouped = new Map<string, RelayEvent[]>(); |
| 47 | + for (const event of events) { |
| 48 | + const repoAddress = getTag(event, "a"); |
| 49 | + if (!repoAddress) continue; |
| 50 | + const projectEvents = grouped.get(repoAddress) ?? []; |
| 51 | + projectEvents.push(event); |
| 52 | + grouped.set(repoAddress, projectEvents); |
| 53 | + } |
| 54 | + return grouped; |
| 55 | +} |
| 56 | + |
| 57 | +/** Loads aggregate issue and pull-request data with bounded relay fan-out. */ |
| 58 | +export async function fetchProjectsWorkItems<TProject extends ProjectReference>( |
| 59 | + projects: TProject[], |
| 60 | +): Promise<ProjectsWorkItemsResult<TProject>> { |
| 61 | + const repoAddresses = [ |
| 62 | + ...new Set(projects.map((project) => project.repoAddress)), |
| 63 | + ]; |
| 64 | + const [rootResult, updateResult, commentResult, statusResult] = |
| 65 | + await Promise.allSettled([ |
| 66 | + relayClient.fetchEvents({ |
| 67 | + kinds: [KIND_GIT_ISSUE, KIND_GIT_PULL_REQUEST], |
| 68 | + "#a": repoAddresses, |
| 69 | + limit: 2_000, |
| 70 | + }), |
| 71 | + relayClient.fetchEvents({ |
| 72 | + kinds: [KIND_GIT_PR_UPDATE], |
| 73 | + "#a": repoAddresses, |
| 74 | + limit: 2_000, |
| 75 | + }), |
| 76 | + relayClient.fetchEvents({ |
| 77 | + kinds: [KIND_TEXT_NOTE], |
| 78 | + "#a": repoAddresses, |
| 79 | + limit: 2_000, |
| 80 | + }), |
| 81 | + relayClient.fetchEvents({ |
| 82 | + kinds: [ |
| 83 | + KIND_GIT_STATUS_OPEN, |
| 84 | + KIND_GIT_STATUS_MERGED, |
| 85 | + KIND_GIT_STATUS_CLOSED, |
| 86 | + KIND_GIT_STATUS_DRAFT, |
| 87 | + ], |
| 88 | + "#a": repoAddresses, |
| 89 | + limit: 2_000, |
| 90 | + }), |
| 91 | + ]); |
| 92 | + |
| 93 | + if (rootResult.status === "rejected") { |
| 94 | + throw rootResult.reason instanceof Error |
| 95 | + ? rootResult.reason |
| 96 | + : new Error("Could not load project issues and pull requests."); |
| 97 | + } |
| 98 | + |
| 99 | + const updateEvents = |
| 100 | + updateResult.status === "fulfilled" ? updateResult.value : []; |
| 101 | + const commentEvents = |
| 102 | + commentResult.status === "fulfilled" ? commentResult.value : []; |
| 103 | + const statusEvents = |
| 104 | + statusResult.status === "fulfilled" ? statusResult.value : []; |
| 105 | + const rootsByRepo = groupByRepoAddress(rootResult.value); |
| 106 | + const updatesByRepo = groupByRepoAddress(updateEvents); |
| 107 | + const commentsByRepo = groupByRepoAddress(commentEvents); |
| 108 | + const statusesByRepo = groupByRepoAddress(statusEvents); |
| 109 | + |
| 110 | + const pullRequests = projects |
| 111 | + .flatMap((project) => |
| 112 | + projectPullRequestEventsToPullRequests( |
| 113 | + (rootsByRepo.get(project.repoAddress) ?? []).filter( |
| 114 | + (event) => event.kind === KIND_GIT_PULL_REQUEST, |
| 115 | + ), |
| 116 | + updatesByRepo.get(project.repoAddress) ?? [], |
| 117 | + commentsByRepo.get(project.repoAddress) ?? [], |
| 118 | + statusesByRepo.get(project.repoAddress) ?? [], |
| 119 | + ).map((pullRequest) => ({ project, pullRequest })), |
| 120 | + ) |
| 121 | + .sort( |
| 122 | + (left, right) => right.pullRequest.updatedAt - left.pullRequest.updatedAt, |
| 123 | + ); |
| 124 | + const issues = projects |
| 125 | + .flatMap((project) => |
| 126 | + projectIssueEventsToIssues( |
| 127 | + (rootsByRepo.get(project.repoAddress) ?? []).filter( |
| 128 | + (event) => event.kind === KIND_GIT_ISSUE, |
| 129 | + ), |
| 130 | + statusesByRepo.get(project.repoAddress) ?? [], |
| 131 | + commentsByRepo.get(project.repoAddress) ?? [], |
| 132 | + ).map((issue) => ({ project, issue })), |
| 133 | + ) |
| 134 | + .sort((left, right) => right.issue.updatedAt - left.issue.updatedAt); |
| 135 | + const sharedFailedSections: ProjectWorkItemSection[] = []; |
| 136 | + if (commentResult.status === "rejected") { |
| 137 | + sharedFailedSections.push("comments"); |
| 138 | + } |
| 139 | + if (statusResult.status === "rejected") { |
| 140 | + sharedFailedSections.push("statuses"); |
| 141 | + } |
| 142 | + const pullRequestFailedSections = [...sharedFailedSections]; |
| 143 | + if (updateResult.status === "rejected") { |
| 144 | + pullRequestFailedSections.unshift("pull-request-updates"); |
| 145 | + } |
| 146 | + |
| 147 | + return { |
| 148 | + issues: { |
| 149 | + items: issues, |
| 150 | + failedSections: sharedFailedSections, |
| 151 | + }, |
| 152 | + pullRequests: { |
| 153 | + items: pullRequests, |
| 154 | + failedSections: pullRequestFailedSections, |
| 155 | + }, |
| 156 | + }; |
| 157 | +} |
0 commit comments