The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { resolvePullRequestAuthorFilter } from "@t3tools/contracts"; |
| 5 | import type { |
| 6 | EnvironmentId, |
| 7 | PullRequestListEntry, |
| 8 | PullRequestListProjectError, |
| 9 | PullRequestListResult, |
| 10 | ProjectId, |
| 11 | RepositoryIdentity, |
| 12 | PullRequestAction, |
| 13 | PullRequestActor, |
| 14 | PullRequestDiffStat, |
| 15 | PullRequestInvolvement, |
| 16 | PullRequestLabel, |
| 17 | PullRequestListCursors, |
| 18 | PullRequestListFilters, |
| 19 | PullRequestListState, |
| 20 | PullRequestState, |
| 21 | PullRequestSummary, |
| 22 | } from "@t3tools/contracts"; |
| 23 | |
| 24 | import { isStalePullRequestReply } from "@t3tools/client-runtime/pull-request-reply"; |
| 25 | import { toSortableTimestamp } from "@t3tools/client-runtime/state/thread-sort"; |
| 26 | import type { PullRequestListSort } from "./pullRequestListPreferences"; |
| 27 | |
| 28 | export interface EnvironmentPullRequestEntry extends PullRequestListEntry { |
| 29 | readonly environmentId: EnvironmentId; |
| 30 | } |
| 31 | |
| 32 | export interface EnvironmentPullRequestStat extends PullRequestDiffStat { |
| 33 | readonly environmentId: EnvironmentId; |
| 34 | } |
| 35 | |
| 36 | export interface EnvironmentPullRequestError extends PullRequestListProjectError { |
| 37 | readonly environmentId: EnvironmentId; |
| 38 | } |
| 39 | |
| 40 | export type PullRequestGroupKey = "reviewRequested" | "authored" | "others"; |
| 41 | |
| 42 | export interface PullRequestGroup<Entry extends PullRequestListEntry = PullRequestListEntry> { |
| 43 | readonly key: PullRequestGroupKey; |
| 44 | readonly label: string; |
| 45 | readonly entries: ReadonlyArray<Entry>; |
| 46 | } |
| 47 | |
| 48 | export interface PullRequestAuthorFacet { |
| 49 | readonly actor: PullRequestActor; |
| 50 | readonly count: number; |
| 51 | readonly mergedCount: number; |
| 52 | } |
| 53 | |
| 54 | export interface PullRequestLabelFacet extends PullRequestLabel { |
| 55 | readonly count: number; |
| 56 | } |
| 57 | |
| 58 | export type PullRequestViewers = PullRequestListResult["viewers"]; |
| 59 | |
| 60 | type ScopedEntry = PullRequestListEntry & { readonly environmentId?: string }; |
| 61 | |
| 62 | const pullRequestViewerKey = (entry: ScopedEntry): string => |
| 63 | `${entry.environmentId ?? ""} ${entry.host}`; |
| 64 | |
| 65 | const GROUP_LABELS: Record<PullRequestGroupKey, string> = { |
| 66 | reviewRequested: msg("Review requested"), |
| 67 | authored: msg("Authored"), |
| 68 | others: msg("Others"), |
| 69 | }; |
| 70 | |
| 71 | function normalize(value: string | null | undefined): string | null { |
| 72 | const trimmed = value?.trim().toLowerCase() ?? ""; |
| 73 | return trimmed.length > 0 ? trimmed : null; |
| 74 | } |
| 75 | |
| 76 | export function pullRequestLabelColor(color: string | null): string | null { |
| 77 | const hex = color?.trim().replace(/^#/, "") ?? ""; |
| 78 | return /^[0-9a-fA-F]{6}$/.test(hex) ? `#${hex}` : null; |
| 79 | } |
| 80 | |
| 81 | export function collectPullRequestListFacets( |
| 82 | entries: ReadonlyArray<PullRequestListEntry>, |
| 83 | state: PullRequestListState, |
| 84 | ) { |
| 85 | const authors = new Map<string, PullRequestAuthorFacet>(); |
| 86 | const labels = new Map<string, PullRequestLabelFacet>(); |
| 87 | const uniqueEntries = new Map(entries.map((entry) => [pullRequestEntryKey(entry), entry])); |
| 88 | for (const entry of uniqueEntries.values()) { |
| 89 | const inState = state === "all" || entry.state === state; |
| 90 | if (entry.author !== null) { |
| 91 | const key = normalize(entry.author.login); |
| 92 | if (key !== null) { |
| 93 | const held = authors.get(key); |
| 94 | authors.set(key, { |
| 95 | actor: held?.actor ?? entry.author, |
| 96 | count: (held?.count ?? 0) + Number(inState), |
| 97 | mergedCount: (held?.mergedCount ?? 0) + Number(entry.state === "merged"), |
| 98 | }); |
| 99 | } |
| 100 | } |
| 101 | if (!inState) continue; |
| 102 | for (const label of entry.labels) { |
| 103 | const key = normalize(label.name); |
| 104 | if (key === null) continue; |
| 105 | const held = labels.get(key); |
| 106 | labels.set(key, { |
| 107 | ...label, |
| 108 | name: held?.name ?? label.name, |
| 109 | color: held?.color ?? label.color, |
| 110 | count: (held?.count ?? 0) + 1, |
| 111 | }); |
| 112 | } |
| 113 | } |
| 114 | return { |
| 115 | authors: [...authors.values()] |
| 116 | .filter((author) => author.count > 0) |
| 117 | .toSorted( |
| 118 | (left, right) => |
| 119 | right.mergedCount - left.mergedCount || |
| 120 | right.count - left.count || |
| 121 | left.actor.login.localeCompare(right.actor.login), |
| 122 | ), |
| 123 | labels: [...labels.values()].toSorted( |
| 124 | (left, right) => right.count - left.count || left.name.localeCompare(right.name), |
| 125 | ), |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * What a filter picker offers for `query`: the loaded rows that match keep their counts and lead, |
| 131 | * then whatever the host found that no loaded row carries, so a name nobody has loaded yet is |
| 132 | * still one search away. |
| 133 | */ |
| 134 | /** How many loaded matches come before the host's answers. */ |
| 135 | const HOSTED_CHOICE_ROOM = 15; |
| 136 | |
| 137 | export function pullRequestAuthorChoices( |
| 138 | facets: ReadonlyArray<PullRequestAuthorFacet>, |
| 139 | hosted: ReadonlyArray<PullRequestActor>, |
| 140 | query: string, |
| 141 | ): ReadonlyArray<PullRequestAuthorFacet> { |
| 142 | const needle = normalize(query); |
| 143 | const loaded = facets.filter( |
| 144 | (facet) => |
| 145 | needle === null || |
| 146 | facet.actor.login.toLowerCase().includes(needle) || |
| 147 | facet.actor.name?.toLowerCase().includes(needle), |
| 148 | ); |
| 149 | const known = new Map(facets.map((facet) => [normalize(facet.actor.login), facet])); |
| 150 | const seen = new Set(loaded.map((facet) => normalize(facet.actor.login))); |
| 151 | const added: PullRequestAuthorFacet[] = []; |
| 152 | for (const actor of hosted) { |
| 153 | const key = normalize(actor.login); |
| 154 | if (seen.has(key)) continue; |
| 155 | seen.add(key); |
| 156 | added.push(known.get(key) ?? { actor, count: 0, mergedCount: 0 }); |
| 157 | } |
| 158 | // The picker shows a short list, and loaded matches alone could fill it. |
| 159 | return [...loaded.slice(0, HOSTED_CHOICE_ROOM), ...added, ...loaded.slice(HOSTED_CHOICE_ROOM)]; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * The author a filter names, as a choice: an author picked from the host's answer is on no |
| 164 | * loaded row, so it stays shown after the search that found it clears. |
| 165 | */ |
| 166 | export function selectedPullRequestAuthor( |
| 167 | facets: ReadonlyArray<PullRequestAuthorFacet>, |
| 168 | hosted: ReadonlyArray<PullRequestActor>, |
| 169 | value: string | undefined, |
| 170 | ): PullRequestAuthorFacet | undefined { |
| 171 | if (value === undefined) return undefined; |
| 172 | const key = normalize(value); |
| 173 | return ( |
| 174 | facets.find((facet) => normalize(facet.actor.login) === key) ?? { |
| 175 | actor: hosted.find((actor) => normalize(actor.login) === key) ?? { |
| 176 | login: value, |
| 177 | name: null, |
| 178 | avatarUrl: null, |
| 179 | }, |
| 180 | count: 0, |
| 181 | mergedCount: 0, |
| 182 | } |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | export function pullRequestLabelChoices( |
| 187 | facets: ReadonlyArray<PullRequestLabelFacet>, |
| 188 | hosted: ReadonlyArray<PullRequestLabel>, |
| 189 | query: string, |
| 190 | ): ReadonlyArray<PullRequestLabelFacet> { |
| 191 | const needle = normalize(query); |
| 192 | const loaded = facets.filter( |
| 193 | (facet) => needle === null || facet.name.toLowerCase().includes(needle), |
| 194 | ); |
| 195 | const known = new Map(facets.map((facet) => [normalize(facet.name), facet])); |
| 196 | const seen = new Set(loaded.map((facet) => normalize(facet.name))); |
| 197 | const added: PullRequestLabelFacet[] = []; |
| 198 | for (const label of hosted) { |
| 199 | const key = normalize(label.name); |
| 200 | if (seen.has(key)) continue; |
| 201 | seen.add(key); |
| 202 | added.push(known.get(key) ?? { ...label, count: 0 }); |
| 203 | } |
| 204 | return [...loaded, ...added]; |
| 205 | } |
| 206 | |
| 207 | export function pullRequestEntryViewer( |
| 208 | entry: ScopedEntry, |
| 209 | viewers: PullRequestViewers, |
| 210 | ): string | null { |
| 211 | return normalize(viewers[pullRequestViewerKey(entry)] ?? viewers[entry.host]); |
| 212 | } |
| 213 | |
| 214 | function isAuthoredByViewer(entry: ScopedEntry, viewers: PullRequestViewers): boolean { |
| 215 | const viewer = pullRequestEntryViewer(entry, viewers); |
| 216 | return viewer !== null && normalize(entry.author?.login) === viewer; |
| 217 | } |
| 218 | |
| 219 | export function matchesPullRequestQuery(entry: PullRequestListEntry, query: string): boolean { |
| 220 | const normalizedQuery = query.trim().toLowerCase(); |
| 221 | if (normalizedQuery.length === 0) return true; |
| 222 | return `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch} ${entry.author?.login ?? ""}` |
| 223 | .toLowerCase() |
| 224 | .includes(normalizedQuery); |
| 225 | } |
| 226 | |
| 227 | export function filterPullRequestsByInvolvement<Entry extends ScopedEntry>( |
| 228 | entries: ReadonlyArray<Entry>, |
| 229 | viewers: PullRequestViewers, |
| 230 | involvement: PullRequestInvolvement, |
| 231 | ): ReadonlyArray<Entry> { |
| 232 | if (involvement === "reviewing") { |
| 233 | return entries.filter((entry) => entry.viewerReviewRequested); |
| 234 | } |
| 235 | if (involvement === "authored") { |
| 236 | return entries.filter((entry) => isAuthoredByViewer(entry, viewers)); |
| 237 | } |
| 238 | return entries; |
| 239 | } |
| 240 | |
| 241 | export function narrowPullRequestsToFilters<Entry extends PullRequestListEntry>( |
| 242 | entries: ReadonlyArray<Entry>, |
| 243 | filters: { |
| 244 | readonly state: PullRequestListState; |
| 245 | readonly projectId: string | undefined; |
| 246 | readonly host: string | undefined; |
| 247 | }, |
| 248 | ): ReadonlyArray<Entry> { |
| 249 | return entries.filter( |
| 250 | (entry) => |
| 251 | (filters.state === "all" || entry.state === filters.state) && |
| 252 | (filters.projectId === undefined || entry.projectId === filters.projectId) && |
| 253 | (filters.host === undefined || entry.host === filters.host), |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | export function matchesPullRequestFilters( |
| 258 | entry: PullRequestListEntry, |
| 259 | filters: PullRequestListFilters, |
| 260 | viewer?: string | null, |
| 261 | ): boolean { |
| 262 | const labels = entry.labels.map((label) => label.name.trim().toLowerCase()); |
| 263 | const holds = (label: string) => labels.includes(label.trim().toLowerCase()); |
| 264 | return ( |
| 265 | (filters.draft === undefined || entry.isDraft === (filters.draft === "only")) && |
| 266 | (filters.review === undefined || |
| 267 | (filters.review === "none" |
| 268 | ? entry.reviewDecision === undefined |
| 269 | : entry.reviewDecision === filters.review)) && |
| 270 | (filters.labels === undefined || filters.labels.every((group) => group.some(holds))) && |
| 271 | (filters.excludedLabels === undefined || !filters.excludedLabels.some(holds)) && |
| 272 | (filters.author === undefined || |
| 273 | entry.author?.login.toLowerCase() === |
| 274 | resolvePullRequestAuthorFilter(filters.author, viewer).toLowerCase()) |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | export function groupPullRequestsByInvolvement<Entry extends ScopedEntry>( |
| 279 | entries: ReadonlyArray<Entry>, |
| 280 | viewers: PullRequestViewers, |
| 281 | ): ReadonlyArray<PullRequestGroup<Entry>> { |
| 282 | const buckets: Record<PullRequestGroupKey, Entry[]> = { |
| 283 | reviewRequested: [], |
| 284 | authored: [], |
| 285 | others: [], |
| 286 | }; |
| 287 | for (const entry of entries) { |
| 288 | if (isAuthoredByViewer(entry, viewers)) { |
| 289 | buckets.authored.push(entry); |
| 290 | } else if (entry.viewerReviewRequested) { |
| 291 | buckets.reviewRequested.push(entry); |
| 292 | } else { |
| 293 | buckets.others.push(entry); |
| 294 | } |
| 295 | } |
| 296 | return (["authored", "reviewRequested", "others"] as const) |
| 297 | .filter((key) => buckets[key].length > 0) |
| 298 | .map((key) => ({ key, label: GROUP_LABELS[key], entries: buckets[key] })); |
| 299 | } |
| 300 | |
| 301 | export function pullRequestEntryKey(entry: ScopedEntry): string { |
| 302 | const scope = entry.environmentId === undefined ? "" : `${entry.environmentId}:`; |
| 303 | return `${scope}${entry.host}:${entry.repository}#${entry.number}`; |
| 304 | } |
| 305 | |
| 306 | export interface PullRequestStatsTarget { |
| 307 | readonly environmentId: EnvironmentId; |
| 308 | readonly input: { |
| 309 | readonly refs: ReadonlyArray<{ |
| 310 | readonly projectId: ProjectId; |
| 311 | readonly repository: string; |
| 312 | readonly number: number; |
| 313 | }>; |
| 314 | }; |
| 315 | } |
| 316 | |
| 317 | export interface PullRequestStatsBatch extends PullRequestStatsTarget { |
| 318 | readonly keys: ReadonlySet<string>; |
| 319 | } |
| 320 | |
| 321 | export type PullRequestStatsPolicy = "visible" | "eager"; |
| 322 | |
| 323 | export interface PullRequestStatsScope { |
| 324 | readonly key: string; |
| 325 | readonly policy: PullRequestStatsPolicy; |
| 326 | } |
| 327 | |
| 328 | const MAX_PULL_REQUEST_STATS_REFS = 500; |
| 329 | |
| 330 | export function pullRequestStatsKeysToRequest( |
| 331 | entriesByKey: ReadonlyMap<string, EnvironmentPullRequestEntry>, |
| 332 | enteredKeys: ReadonlySet<string>, |
| 333 | batches: ReadonlyArray<PullRequestStatsBatch>, |
| 334 | statsByRow: ReadonlyMap<string, unknown>, |
| 335 | ): ReadonlySet<string> { |
| 336 | const requested = new Set(batches.flatMap((batch) => [...batch.keys])); |
| 337 | return new Set( |
| 338 | [...enteredKeys].filter((key) => { |
| 339 | const entry = entriesByKey.get(key); |
| 340 | return ( |
| 341 | entry !== undefined && |
| 342 | entry.additions === 0 && |
| 343 | entry.deletions === 0 && |
| 344 | !requested.has(key) && |
| 345 | !statsByRow.has(pullRequestDiffStatKey(entry)) |
| 346 | ); |
| 347 | }), |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | export function pullRequestStatsBatches( |
| 352 | entriesByKey: ReadonlyMap<string, EnvironmentPullRequestEntry>, |
| 353 | keys: ReadonlySet<string>, |
| 354 | ): ReadonlyArray<PullRequestStatsBatch> { |
| 355 | const byEnvironment = new Map< |
| 356 | EnvironmentId, |
| 357 | Array<{ |
| 358 | readonly key: string; |
| 359 | readonly ref: PullRequestStatsTarget["input"]["refs"][number]; |
| 360 | }> |
| 361 | >(); |
| 362 | for (const key of keys) { |
| 363 | const entry = entriesByKey.get(key); |
| 364 | if (entry === undefined) continue; |
| 365 | const rows = byEnvironment.get(entry.environmentId) ?? []; |
| 366 | rows.push({ |
| 367 | key, |
| 368 | ref: { |
| 369 | projectId: entry.projectId, |
| 370 | repository: entry.repository, |
| 371 | number: entry.number, |
| 372 | }, |
| 373 | }); |
| 374 | byEnvironment.set(entry.environmentId, rows); |
| 375 | } |
| 376 | return [...byEnvironment].flatMap(([environmentId, rows]) => { |
| 377 | const batches: PullRequestStatsBatch[] = []; |
| 378 | for (let index = 0; index < rows.length; index += MAX_PULL_REQUEST_STATS_REFS) { |
| 379 | const batch = rows.slice(index, index + MAX_PULL_REQUEST_STATS_REFS); |
| 380 | batches.push({ |
| 381 | environmentId, |
| 382 | input: { refs: batch.map((row) => row.ref) }, |
| 383 | keys: new Set(batch.map((row) => row.key)), |
| 384 | }); |
| 385 | } |
| 386 | return batches; |
| 387 | }); |
| 388 | } |
| 389 | |
| 390 | /** Size sorting needs all loaded rows' stats; other modes need only nearby rows. */ |
| 391 | export function pullRequestStatsRequestBatches({ |
| 392 | entriesByKey, |
| 393 | candidateKeys, |
| 394 | policy, |
| 395 | activeBatches, |
| 396 | statsByRow, |
| 397 | refresh = false, |
| 398 | }: { |
| 399 | readonly entriesByKey: ReadonlyMap<string, EnvironmentPullRequestEntry>; |
| 400 | readonly candidateKeys: ReadonlySet<string>; |
| 401 | readonly policy: PullRequestStatsPolicy; |
| 402 | readonly activeBatches: ReadonlyArray<PullRequestStatsBatch>; |
| 403 | readonly statsByRow: ReadonlyMap<string, unknown>; |
| 404 | readonly refresh?: boolean; |
| 405 | }): ReadonlyArray<PullRequestStatsBatch> { |
| 406 | const requestedKeys = policy === "eager" ? new Set(entriesByKey.keys()) : candidateKeys; |
| 407 | const keys = refresh |
| 408 | ? requestedKeys |
| 409 | : pullRequestStatsKeysToRequest(entriesByKey, requestedKeys, activeBatches, statsByRow); |
| 410 | return pullRequestStatsBatches(entriesByKey, keys); |
| 411 | } |
| 412 | |
| 413 | export function pullRequestStatsRefreshBatches({ |
| 414 | requestedScope, |
| 415 | currentScope, |
| 416 | entriesByKey, |
| 417 | candidateKeys, |
| 418 | statsByRow, |
| 419 | }: { |
| 420 | readonly requestedScope: PullRequestStatsScope; |
| 421 | readonly currentScope: PullRequestStatsScope; |
| 422 | readonly entriesByKey: ReadonlyMap<string, EnvironmentPullRequestEntry>; |
| 423 | readonly candidateKeys: ReadonlySet<string>; |
| 424 | readonly statsByRow: ReadonlyMap<string, unknown>; |
| 425 | }): ReadonlyArray<PullRequestStatsBatch> | null { |
| 426 | if (requestedScope.key !== currentScope.key || requestedScope.policy !== currentScope.policy) { |
| 427 | return null; |
| 428 | } |
| 429 | return pullRequestStatsRequestBatches({ |
| 430 | entriesByKey, |
| 431 | candidateKeys, |
| 432 | policy: requestedScope.policy, |
| 433 | activeBatches: [], |
| 434 | statsByRow, |
| 435 | refresh: true, |
| 436 | }); |
| 437 | } |
| 438 | |
| 439 | export function retainVisiblePullRequestStatsBatches( |
| 440 | batches: ReadonlyArray<PullRequestStatsBatch>, |
| 441 | visibleKeys: ReadonlySet<string>, |
| 442 | ): ReadonlyArray<PullRequestStatsBatch> { |
| 443 | return batches.filter((batch) => { |
| 444 | for (const key of batch.keys) { |
| 445 | if (visibleKeys.has(key)) return true; |
| 446 | } |
| 447 | return false; |
| 448 | }); |
| 449 | } |
| 450 | |
| 451 | /** Recency pagination can introduce older priority rows; grouping pages would reorder read rows. */ |
| 452 | export function partitionPullRequestsWithPriority<Entry extends PullRequestListEntry>( |
| 453 | entries: ReadonlyArray<Entry>, |
| 454 | authored: ReadonlyArray<Entry>, |
| 455 | reviewRequested: ReadonlyArray<Entry>, |
| 456 | ): ReadonlyArray<PullRequestGroup<Entry>> { |
| 457 | // Matched by URL: each list may hold the pull request under a different environment. |
| 458 | const authoredByKey = new Map(authored.map((entry) => [entry.url, entry])); |
| 459 | const reviewByKey = new Map( |
| 460 | reviewRequested.flatMap((entry) => |
| 461 | authoredByKey.has(entry.url) ? [] : [[entry.url, entry] as const], |
| 462 | ), |
| 463 | ); |
| 464 | const others: Entry[] = []; |
| 465 | for (const entry of entries) { |
| 466 | const key = entry.url; |
| 467 | if (authoredByKey.has(key)) { |
| 468 | authoredByKey.set(key, entry); |
| 469 | } else if (reviewByKey.has(key)) { |
| 470 | reviewByKey.set(key, entry); |
| 471 | } else { |
| 472 | others.push(entry); |
| 473 | } |
| 474 | } |
| 475 | return ( |
| 476 | [ |
| 477 | { |
| 478 | key: "authored", |
| 479 | entries: [...authoredByKey.values()].toSorted(comparePullRequestsByRecency), |
| 480 | }, |
| 481 | { |
| 482 | key: "reviewRequested", |
| 483 | entries: [...reviewByKey.values()].toSorted(comparePullRequestsByRecency), |
| 484 | }, |
| 485 | { key: "others", entries: others }, |
| 486 | ] as const |
| 487 | ) |
| 488 | .filter((group) => group.entries.length > 0) |
| 489 | .map((group) => ({ ...group, label: GROUP_LABELS[group.key] })); |
| 490 | } |
| 491 | |
| 492 | export type PullRequestDiffStats = ReadonlyMap< |
| 493 | string, |
| 494 | { readonly additions: number; readonly deletions: number } |
| 495 | >; |
| 496 | |
| 497 | /** New stats queries must not blank existing counts. */ |
| 498 | export function mergePullRequestDiffStats( |
| 499 | previous: PullRequestDiffStats, |
| 500 | stats: ReadonlyArray<{ |
| 501 | readonly environmentId: string; |
| 502 | readonly projectId: string; |
| 503 | readonly number: number; |
| 504 | readonly additions: number; |
| 505 | readonly deletions: number; |
| 506 | }>, |
| 507 | ): PullRequestDiffStats { |
| 508 | if (stats.length === 0) return previous; |
| 509 | const next = new Map(previous); |
| 510 | for (const stat of stats) { |
| 511 | next.set(pullRequestDiffStatKey(stat), { |
| 512 | additions: stat.additions, |
| 513 | deletions: stat.deletions, |
| 514 | }); |
| 515 | } |
| 516 | return next; |
| 517 | } |
| 518 | |
| 519 | export const pullRequestDiffStatKey = (row: { |
| 520 | readonly environmentId: string; |
| 521 | readonly projectId: string; |
| 522 | readonly number: number; |
| 523 | }) => `${row.environmentId} ${row.projectId} ${row.number}`; |
| 524 | |
| 525 | export interface MergedPullRequestList { |
| 526 | readonly viewers: PullRequestViewers; |
| 527 | readonly providers: PullRequestListResult["providers"]; |
| 528 | readonly entries: ReadonlyArray<EnvironmentPullRequestEntry>; |
| 529 | readonly errors: ReadonlyArray<EnvironmentPullRequestError>; |
| 530 | readonly truncated: boolean; |
| 531 | readonly nextCursors: Readonly<Record<string, PullRequestListCursors>>; |
| 532 | readonly truncatedEnvironments: ReadonlyArray<string>; |
| 533 | } |
| 534 | |
| 535 | export function mergePullRequestLists( |
| 536 | answers: ReadonlyArray<readonly [EnvironmentId, PullRequestListResult]>, |
| 537 | ): MergedPullRequestList | null { |
| 538 | if (answers.length === 0) return null; |
| 539 | const viewers: Record<string, string> = {}; |
| 540 | const truncatedEnvironments = new Set<string>(); |
| 541 | const providers = new Map<string, PullRequestListResult["providers"][number]>(); |
| 542 | const entries = new Map<string, EnvironmentPullRequestEntry>(); |
| 543 | const errors: EnvironmentPullRequestError[] = []; |
| 544 | const nextCursors: Record<string, PullRequestListCursors> = {}; |
| 545 | let truncated = false; |
| 546 | for (const [environmentId, answer] of answers) { |
| 547 | for (const [host, login] of Object.entries(answer.viewers)) { |
| 548 | viewers[`${environmentId} ${host}`] = login; |
| 549 | } |
| 550 | for (const provider of answer.providers) { |
| 551 | const held = providers.get(provider.host); |
| 552 | providers.set( |
| 553 | provider.host, |
| 554 | held === undefined |
| 555 | ? provider |
| 556 | : { |
| 557 | ...(held.configured ? held : provider), |
| 558 | projectCount: held.projectCount + provider.projectCount, |
| 559 | searchesOnHost: held.searchesOnHost && provider.searchesOnHost, |
| 560 | configured: held.configured || provider.configured, |
| 561 | }, |
| 562 | ); |
| 563 | } |
| 564 | for (const entry of answer.entries) { |
| 565 | const held = entries.get(entry.url); |
| 566 | // The first environment to answer keeps a pull request that several of them read. |
| 567 | if ( |
| 568 | held === undefined || |
| 569 | (held.environmentId === environmentId && entry.updatedAt > held.updatedAt) |
| 570 | ) { |
| 571 | entries.set(entry.url, { ...entry, environmentId }); |
| 572 | } |
| 573 | } |
| 574 | errors.push(...answer.errors.map((error) => ({ ...error, environmentId }))); |
| 575 | truncated ||= answer.truncated; |
| 576 | if (answer.truncated) truncatedEnvironments.add(environmentId); |
| 577 | if (Object.keys(answer.nextCursors).length > 0) { |
| 578 | nextCursors[environmentId] = { ...nextCursors[environmentId], ...answer.nextCursors }; |
| 579 | } |
| 580 | } |
| 581 | return { |
| 582 | viewers, |
| 583 | providers: [...providers.values()], |
| 584 | entries: [...entries.values()].toSorted(comparePullRequestsByRecency), |
| 585 | errors, |
| 586 | truncated, |
| 587 | nextCursors, |
| 588 | truncatedEnvironments: [...truncatedEnvironments], |
| 589 | }; |
| 590 | } |
| 591 | |
| 592 | export const pullRequestEnvironmentSetKey = (environmentIds: ReadonlyArray<string>): string => |
| 593 | [...environmentIds].sort((left, right) => left.localeCompare(right)).join(","); |
| 594 | |
| 595 | export interface PullRequestPartitionsSnapshot { |
| 596 | readonly authored: ReadonlyArray<EnvironmentPullRequestEntry>; |
| 597 | readonly reviewing: ReadonlyArray<EnvironmentPullRequestEntry>; |
| 598 | } |
| 599 | |
| 600 | /** Retain project scope during reconnect until projects arrive. */ |
| 601 | export function resolveProjectScope<Id extends string>( |
| 602 | projectId: Id | undefined, |
| 603 | projects: ReadonlyArray<{ readonly id: string }>, |
| 604 | projectsKnown: boolean, |
| 605 | ): Id | undefined { |
| 606 | if (projectId === undefined || !projectsKnown) return projectId; |
| 607 | return projects.some((project) => project.id === projectId) ? projectId : undefined; |
| 608 | } |
| 609 | |
| 610 | export function findScopedProject< |
| 611 | Project extends { readonly id: string; readonly environmentId: string }, |
| 612 | >( |
| 613 | projects: ReadonlyArray<Project>, |
| 614 | environmentId: string | null | undefined, |
| 615 | projectId: string | undefined, |
| 616 | ): Project | undefined { |
| 617 | if (projectId === undefined) return undefined; |
| 618 | const matches = projects.filter((project) => project.id === projectId); |
| 619 | if (environmentId === null || environmentId === undefined) { |
| 620 | return matches.length === 1 ? matches[0] : undefined; |
| 621 | } |
| 622 | return matches.find((project) => project.environmentId === environmentId); |
| 623 | } |
| 624 | |
| 625 | export function resolveQueryEnvironmentIds<Id extends string>( |
| 626 | environmentIds: ReadonlyArray<Id>, |
| 627 | projects: ReadonlyArray<{ readonly id: string; readonly environmentId: Id }>, |
| 628 | scopedProject: { readonly environmentId: Id } | undefined, |
| 629 | scopedProjectId: string | undefined, |
| 630 | projectsKnown: boolean, |
| 631 | ): ReadonlyArray<Id> { |
| 632 | if (scopedProject !== undefined) { |
| 633 | return environmentIds.filter((environmentId) => environmentId === scopedProject.environmentId); |
| 634 | } |
| 635 | if (scopedProjectId === undefined || !projectsKnown) return environmentIds; |
| 636 | const holders = new Set( |
| 637 | projects |
| 638 | .filter((project) => project.id === scopedProjectId) |
| 639 | .map((project) => project.environmentId), |
| 640 | ); |
| 641 | return environmentIds.filter((environmentId) => holders.has(environmentId)); |
| 642 | } |
| 643 | |
| 644 | /** Retain connecting environments to avoid matching another server's project id. */ |
| 645 | export function resolveSelectedEnvironmentId<Id extends string>( |
| 646 | namedEnvironmentId: Id | undefined, |
| 647 | knownEnvironmentIds: ReadonlySet<Id>, |
| 648 | fallbackEnvironmentId: Id | null, |
| 649 | ): Id | null { |
| 650 | if (namedEnvironmentId === undefined) return fallbackEnvironmentId; |
| 651 | return knownEnvironmentIds.has(namedEnvironmentId) ? namedEnvironmentId : fallbackEnvironmentId; |
| 652 | } |
| 653 | |
| 654 | /** Host search also matches bodies and commits absent from visible row fields. */ |
| 655 | export function scorePullRequestMatch(entry: PullRequestListEntry, query: string): number { |
| 656 | const needle = query.trim().toLowerCase(); |
| 657 | if (needle.length === 0) return 0; |
| 658 | const number = needle.replace(/^#/u, ""); |
| 659 | if (String(entry.number) === number) return 100; |
| 660 | |
| 661 | const title = entry.title.toLowerCase(); |
| 662 | const terms = needle.split(/\s+/u).filter((term) => term.length > 0); |
| 663 | if (title === needle) return 90; |
| 664 | if (title.includes(needle)) return 80; |
| 665 | if (terms.length > 1 && terms.every((term) => title.includes(term))) return 70; |
| 666 | if (entry.headBranch.toLowerCase().includes(needle)) return 60; |
| 667 | if ((entry.author?.login ?? "").toLowerCase().includes(needle)) return 50; |
| 668 | if (entry.repository.toLowerCase().includes(needle)) return 40; |
| 669 | if (terms.some((term) => title.includes(term))) return 30; |
| 670 | return 10; |
| 671 | } |
| 672 | |
| 673 | export function rankPullRequestMatches<Entry extends PullRequestListEntry>( |
| 674 | entries: ReadonlyArray<Entry>, |
| 675 | query: string, |
| 676 | ): ReadonlyArray<Entry> { |
| 677 | if (query.trim().length === 0) return entries; |
| 678 | return entries.toSorted((left, right) => { |
| 679 | const byScore = scorePullRequestMatch(right, query) - scorePullRequestMatch(left, query); |
| 680 | return byScore !== 0 ? byScore : comparePullRequestsByRecency(left, right); |
| 681 | }); |
| 682 | } |
| 683 | |
| 684 | export function rankPullRequestsByMergeReadiness<Entry extends PullRequestListEntry>( |
| 685 | entries: ReadonlyArray<Entry>, |
| 686 | hasMeasuredSize: (entry: Entry) => boolean = (entry) => entry.additions + entry.deletions > 0, |
| 687 | ): ReadonlyArray<Entry> { |
| 688 | const tier = (entry: Entry) => { |
| 689 | if (entry.mergeability === "conflicting") return 4; |
| 690 | if (entry.state !== "open") return 3; |
| 691 | if (entry.isDraft) return 2; |
| 692 | if (entry.checksState === "passing" && entry.reviewDecision === "approved") return 0; |
| 693 | if (entry.checksState === "passing") return 1; |
| 694 | return 2; |
| 695 | }; |
| 696 | return entries.toSorted((left, right) => { |
| 697 | const byTier = tier(left) - tier(right); |
| 698 | if (byTier !== 0) return byTier; |
| 699 | const measured = Number(hasMeasuredSize(right)) - Number(hasMeasuredSize(left)); |
| 700 | const sized = left.additions + left.deletions - (right.additions + right.deletions); |
| 701 | return measured || sized || comparePullRequestsByRecency(left, right); |
| 702 | }); |
| 703 | } |
| 704 | |
| 705 | export function comparePullRequestsByRecency( |
| 706 | left: PullRequestListEntry, |
| 707 | right: PullRequestListEntry, |
| 708 | ) { |
| 709 | const leftUpdated = toSortableTimestamp(left.updatedAt); |
| 710 | const rightUpdated = toSortableTimestamp(right.updatedAt); |
| 711 | const measured = Number(leftUpdated === null) - Number(rightUpdated === null); |
| 712 | if (measured !== 0) return measured; |
| 713 | if (leftUpdated === null || rightUpdated === null) return 0; |
| 714 | return rightUpdated - leftUpdated; |
| 715 | } |
| 716 | |
| 717 | function rankByTierThenRecency<Entry extends PullRequestListEntry>( |
| 718 | entries: ReadonlyArray<Entry>, |
| 719 | tier: (entry: Entry) => number, |
| 720 | ): ReadonlyArray<Entry> { |
| 721 | return entries.toSorted((left, right) => { |
| 722 | const byTier = tier(left) - tier(right); |
| 723 | return byTier !== 0 ? byTier : comparePullRequestsByRecency(left, right); |
| 724 | }); |
| 725 | } |
| 726 | |
| 727 | export function rankPullRequestsBlockedOnAuthor<Entry extends PullRequestListEntry>( |
| 728 | entries: ReadonlyArray<Entry>, |
| 729 | ): ReadonlyArray<Entry> { |
| 730 | return rankByTierThenRecency(entries, (entry) => { |
| 731 | if (entry.state !== "open") return 6; |
| 732 | if (entry.mergeability === "conflicting") return 0; |
| 733 | if (entry.reviewDecision === "changes-requested") return 1; |
| 734 | if (entry.checksState === "failing") return 2; |
| 735 | if (entry.isDraft) return 3; |
| 736 | if (entry.checksState === "passing" && entry.reviewDecision === "approved") return 5; |
| 737 | return 4; |
| 738 | }); |
| 739 | } |
| 740 | |
| 741 | function rankPullRequestsBlockedOnReviewer<Entry extends PullRequestListEntry>( |
| 742 | entries: ReadonlyArray<Entry>, |
| 743 | ): ReadonlyArray<Entry> { |
| 744 | return rankByTierThenRecency(entries, (entry) => (entry.state === "open" ? 0 : 1)); |
| 745 | } |
| 746 | |
| 747 | export function sortPullRequestGroups<Entry extends PullRequestListEntry>( |
| 748 | groups: ReadonlyArray<PullRequestGroup<Entry>>, |
| 749 | sort: PullRequestListSort, |
| 750 | searchText: string, |
| 751 | hasMeasuredSize: (entry: Entry) => boolean = (entry) => entry.additions + entry.deletions > 0, |
| 752 | involvement: PullRequestInvolvement = "all", |
| 753 | ): ReadonlyArray<PullRequestGroup<Entry>> { |
| 754 | const sortWithinGroups = (rank: (entries: ReadonlyArray<Entry>) => ReadonlyArray<Entry>) => |
| 755 | groups.map((group) => ({ ...group, entries: rank(group.entries) })); |
| 756 | |
| 757 | if (sort === "ready") { |
| 758 | return searchText.trim().length === 0 |
| 759 | ? sortWithinGroups((entries) => rankPullRequestsByMergeReadiness(entries, hasMeasuredSize)) |
| 760 | : groups; |
| 761 | } |
| 762 | if (sort === "blocked") { |
| 763 | if (searchText.trim().length > 0) return groups; |
| 764 | const role = (key: PullRequestGroupKey) => |
| 765 | key === "others" ? involvement : key === "authored" ? "authored" : "reviewing"; |
| 766 | return groups.map((group) => { |
| 767 | const groupRole = role(group.key); |
| 768 | if (groupRole === "all") return group; |
| 769 | const rank = |
| 770 | groupRole === "authored" |
| 771 | ? rankPullRequestsBlockedOnAuthor |
| 772 | : rankPullRequestsBlockedOnReviewer; |
| 773 | return { ...group, entries: rank(group.entries) }; |
| 774 | }); |
| 775 | } |
| 776 | if (sort === "updated") return groups; |
| 777 | |
| 778 | const timestamp = (entry: Entry) => |
| 779 | toSortableTimestamp(entry.updatedAt) ?? toSortableTimestamp(entry.createdAt) ?? 0; |
| 780 | return sortWithinGroups((entries) => |
| 781 | entries.toSorted((left, right) => { |
| 782 | if (sort === "newest" || sort === "oldest") { |
| 783 | const leftCreated = toSortableTimestamp(left.createdAt); |
| 784 | const rightCreated = toSortableTimestamp(right.createdAt); |
| 785 | const measured = Number(rightCreated !== null) - Number(leftCreated !== null); |
| 786 | const dated = (leftCreated ?? 0) - (rightCreated ?? 0); |
| 787 | return ( |
| 788 | measured || (sort === "newest" ? -dated : dated) || timestamp(right) - timestamp(left) |
| 789 | ); |
| 790 | } |
| 791 | const measured = Number(hasMeasuredSize(right)) - Number(hasMeasuredSize(left)); |
| 792 | const sized = left.additions + left.deletions - (right.additions + right.deletions); |
| 793 | return ( |
| 794 | measured || (sort === "largest" ? -sized : sized) || timestamp(right) - timestamp(left) |
| 795 | ); |
| 796 | }), |
| 797 | ); |
| 798 | } |
| 799 | |
| 800 | export function withDiffStat< |
| 801 | Entry extends PullRequestListEntry & { readonly environmentId: string }, |
| 802 | >( |
| 803 | entry: Entry, |
| 804 | statsByRow: ReadonlyMap<string, { readonly additions: number; readonly deletions: number }>, |
| 805 | ): Entry { |
| 806 | if (entry.additions !== 0 || entry.deletions !== 0) return entry; |
| 807 | const stat = statsByRow.get(pullRequestDiffStatKey(entry)); |
| 808 | return stat === undefined ? entry : { ...entry, ...stat }; |
| 809 | } |
| 810 | |
| 811 | export interface PullRequestListOverride { |
| 812 | readonly state: PullRequestState; |
| 813 | readonly isDraft?: boolean; |
| 814 | readonly updatedAt: string; |
| 815 | readonly token: number; |
| 816 | readonly at: number; |
| 817 | /** Set when the host reported this state, so a newer row can outrank it. */ |
| 818 | readonly fromReply?: true; |
| 819 | readonly observedAt?: number; |
| 820 | } |
| 821 | |
| 822 | export function pullRequestOverrideAfterAction( |
| 823 | entry: Pick<PullRequestListEntry, "state" | "isDraft">, |
| 824 | action: PullRequestAction, |
| 825 | now: Date, |
| 826 | token: number, |
| 827 | reply?: PullRequestSummary, |
| 828 | ): PullRequestListOverride | null { |
| 829 | if (reply !== undefined) { |
| 830 | const { state, isDraft, updatedAt, observedAt } = reply; |
| 831 | return { |
| 832 | state, |
| 833 | ...(isDraft === undefined ? {} : { isDraft }), |
| 834 | updatedAt, |
| 835 | ...(observedAt === undefined ? {} : { observedAt }), |
| 836 | fromReply: true, |
| 837 | token, |
| 838 | at: now.getTime(), |
| 839 | }; |
| 840 | } |
| 841 | const stamp = { updatedAt: now.toISOString(), token, at: now.getTime() }; |
| 842 | switch (action) { |
| 843 | case "close": |
| 844 | return { state: "closed", ...stamp }; |
| 845 | case "reopen": |
| 846 | return { state: "open", ...stamp }; |
| 847 | case "merge": |
| 848 | return { state: "merged", ...stamp }; |
| 849 | case "draft": |
| 850 | return { state: entry.state, isDraft: true, ...stamp }; |
| 851 | case "ready": |
| 852 | return { state: entry.state, isDraft: false, ...stamp }; |
| 853 | default: |
| 854 | return null; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | export function applyPullRequestOverrides<Entry extends PullRequestListEntry>( |
| 859 | entries: ReadonlyArray<Entry>, |
| 860 | overrides: ReadonlyMap<string, PullRequestListOverride>, |
| 861 | keyOf: (entry: Entry) => string, |
| 862 | state: PullRequestListState, |
| 863 | ): ReadonlyArray<Entry> { |
| 864 | if (overrides.size === 0 && state === "all") return entries; |
| 865 | const out: Entry[] = []; |
| 866 | for (const entry of entries) { |
| 867 | const held = overrides.get(keyOf(entry)); |
| 868 | const override = |
| 869 | held?.fromReply === true && isStalePullRequestReply(entry, held) ? undefined : held; |
| 870 | if (state !== "all" && (override?.state ?? entry.state) !== state) continue; |
| 871 | out.push(override === undefined ? entry : { ...entry, ...override }); |
| 872 | } |
| 873 | return overrides.size === 0 && out.length === entries.length ? entries : out; |
| 874 | } |
| 875 | |
| 876 | export function reusePullRequestEntries<Entry extends PullRequestListEntry>( |
| 877 | previous: ReadonlyArray<Entry>, |
| 878 | next: ReadonlyArray<Entry>, |
| 879 | keyOf: (entry: Entry) => string, |
| 880 | ): ReadonlyArray<Entry> { |
| 881 | if (previous.length === 0) return next; |
| 882 | const held = new Map(previous.map((entry) => [keyOf(entry), entry])); |
| 883 | let reused = 0; |
| 884 | const out = next.map((entry) => { |
| 885 | const before = held.get(keyOf(entry)); |
| 886 | if (before !== undefined && JSON.stringify(before) === JSON.stringify(entry)) { |
| 887 | reused += 1; |
| 888 | return before; |
| 889 | } |
| 890 | return entry; |
| 891 | }); |
| 892 | return reused === next.length && |
| 893 | previous.length === next.length && |
| 894 | previous.every((entry, index) => keyOf(entry) === keyOf(next[index]!)) |
| 895 | ? previous |
| 896 | : out; |
| 897 | } |
| 898 | |
| 899 | const PULL_REQUEST_OVERRIDE_TRUST_MS = 60_000; |
| 900 | |
| 901 | /** Retire overrides only when returned rows agree; partial-feed absence proves nothing. */ |
| 902 | export function settlePullRequestOverrides<Entry extends PullRequestListEntry>( |
| 903 | overrides: ReadonlyMap<string, PullRequestListOverride>, |
| 904 | answered: ReadonlyArray<Entry>, |
| 905 | keyOf: (entry: Entry) => string, |
| 906 | now: number, |
| 907 | ): ReadonlyMap<string, PullRequestListOverride> { |
| 908 | if (overrides.size === 0) return overrides; |
| 909 | const byKey = new Map(answered.map((entry) => [keyOf(entry), entry])); |
| 910 | const kept = new Map<string, PullRequestListOverride>(); |
| 911 | for (const [key, override] of overrides) { |
| 912 | const row = byKey.get(key); |
| 913 | if (row === undefined) { |
| 914 | kept.set(key, override); |
| 915 | continue; |
| 916 | } |
| 917 | const agrees = |
| 918 | row.state === override.state && |
| 919 | (override.isDraft === undefined || row.isDraft === override.isDraft); |
| 920 | const outranked = now - override.at > PULL_REQUEST_OVERRIDE_TRUST_MS; |
| 921 | if (!agrees && !outranked) kept.set(key, override); |
| 922 | } |
| 923 | return kept.size === overrides.size ? overrides : kept; |
| 924 | } |
| 925 | |
| 926 | export type PullRequestQuickAction = "close" | "merge" | "ready" | "reopen"; |
| 927 | |
| 928 | export function pullRequestHostActions( |
| 929 | providers: ReadonlyArray<PullRequestListResult["providers"][number]>, |
| 930 | ): ( |
| 931 | entry: Pick<EnvironmentPullRequestEntry, "host" | "provider">, |
| 932 | ) => ReadonlySet<PullRequestAction> { |
| 933 | const byHost = new Map( |
| 934 | providers.flatMap((provider) => |
| 935 | provider.actions === undefined |
| 936 | ? [] |
| 937 | : [[provider.host.toLowerCase(), new Set(provider.actions)] as const], |
| 938 | ), |
| 939 | ); |
| 940 | const none = new Set<PullRequestAction>(); |
| 941 | return (entry) => |
| 942 | byHost.get(entry.host.toLowerCase()) ?? |
| 943 | sourceControlClients.find(entry.provider)?.changeRequestActionsWhenUnreported ?? |
| 944 | none; |
| 945 | } |
| 946 | |
| 947 | export function pullRequestQuickActions( |
| 948 | entry: Pick<EnvironmentPullRequestEntry, "state" | "isDraft">, |
| 949 | hostActions: ReadonlySet<PullRequestAction>, |
| 950 | ): ReadonlyArray<PullRequestQuickAction> { |
| 951 | const forState: ReadonlyArray<PullRequestQuickAction> = |
| 952 | entry.state === "merged" |
| 953 | ? [] |
| 954 | : entry.state === "closed" |
| 955 | ? ["reopen"] |
| 956 | : entry.isDraft |
| 957 | ? ["close", "ready"] |
| 958 | : ["close", "merge"]; |
| 959 | return forState.filter((action) => hostActions.has(action)); |
| 960 | } |
| 961 | |
| 962 | export function largestPullRequestReadCount( |
| 963 | entries: ReadonlyArray<EnvironmentPullRequestEntry>, |
| 964 | ): number { |
| 965 | const counts = new Map<string, number>(); |
| 966 | for (const entry of entries) { |
| 967 | const read = `${entry.environmentId} ${entry.host.toLowerCase()} ${ |
| 968 | sourceControlClients.find(entry.provider)?.changeRequestListScope === "host" |
| 969 | ? "" |
| 970 | : entry.repository.toLowerCase() |
| 971 | }`; |
| 972 | counts.set(read, (counts.get(read) ?? 0) + 1); |
| 973 | } |
| 974 | return Math.max(0, ...counts.values()); |
| 975 | } |
| 976 | |
| 977 | export function findProjectForRepository< |
| 978 | Project extends { |
| 979 | readonly id: string; |
| 980 | readonly environmentId: string; |
| 981 | readonly repositoryIdentity?: RepositoryIdentity | null | undefined; |
| 982 | }, |
| 983 | >( |
| 984 | projects: ReadonlyArray<Project>, |
| 985 | input: { |
| 986 | readonly repository: string; |
| 987 | readonly host?: string | undefined; |
| 988 | readonly selectedEnvironmentId?: string | null | undefined; |
| 989 | readonly scopedProjectId?: string | undefined; |
| 990 | readonly scopedEnvironmentId?: string | null | undefined; |
| 991 | }, |
| 992 | ): Project | undefined { |
| 993 | const repository = input.repository.toLowerCase(); |
| 994 | const host = input.host?.toLowerCase(); |
| 995 | const candidates = projects.filter((project) => { |
| 996 | if ( |
| 997 | input.selectedEnvironmentId != null && |
| 998 | project.environmentId !== input.selectedEnvironmentId |
| 999 | ) { |
| 1000 | return false; |
| 1001 | } |
| 1002 | const identity = project.repositoryIdentity; |
| 1003 | if ( |
| 1004 | !identity || |
| 1005 | sourceControlClients.repositorySelector(identity)?.toLowerCase() !== repository |
| 1006 | ) { |
| 1007 | return false; |
| 1008 | } |
| 1009 | return ( |
| 1010 | host === undefined || |
| 1011 | sourceControlClients.changeRequestHost(identity, identity.provider) === host |
| 1012 | ); |
| 1013 | }); |
| 1014 | // Azure repository names can collide across projects on the same host. |
| 1015 | const scoped = findScopedProject(candidates, input.scopedEnvironmentId, input.scopedProjectId); |
| 1016 | return scoped ?? candidates[0]; |
| 1017 | } |