Repository navigation
Expand file tree
/
Copy pathPullRequestService.ts
More file actions
3251 lines (3151 loc) · 135 KB
/
Copy pathPullRequestService.ts
File metadata and controls
3251 lines (3151 loc) · 135 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
canonicalRepositoryKey,
isSshRemoteUrl,
sourceControlRepositorySelector,
} from "@t3tools/shared/sourceControl";
import { normalizeGitRemoteUrl } from "@t3tools/shared/git";
import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as PubSub from "effect/PubSub";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import type * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
import {
PullRequestOperationError,
PullRequestUnavailableError,
pullRequestHostOf,
pullRequestProviderRequirement,
resolvePullRequestAuthorFilter,
type OrchestrationProjectShell,
type ProjectId,
type PullRequestAction,
type PullRequestActionInput,
type PullRequestActivity,
type PullRequestCommentInput,
type PullRequestCommentUpdateInput,
type PullRequestDetail,
type PullRequestPreview,
type PullRequestDiffFileContentsInput,
type PullRequestDiffFileContentsResult,
type PullRequestDiffStat,
type PullRequestDiffInput,
type PullRequestFilesViewedResult,
type PullRequestDiffResult,
type PullRequestInvalidateInput,
type PullRequestListEntry,
type PullRequestListFilters,
type PullRequestListInput,
type PullRequestListProjectError,
type PullRequestListResult,
type PullRequestListStatsInput,
type PullRequestListStatsResult,
type PullRequestProviderSummary,
type PullRequestReactionInput,
type PullRequestRef,
type PullRequestRoutingResult,
type PullRequestRoutingIdentityInput,
type PullRequestRoutingIdentityResult,
type PullRequestReviewVerdict,
type PullRequestReviewerCandidateList,
type PullRequestReviewerRequestInput,
type PullRequestLabelCandidateList,
type PullRequestLabelChangeInput,
type PullRequestSetFilesViewedInput,
type PullRequestSubmitReviewInput,
PullRequestStack,
PullRequestSummary,
type PullRequestThreadReplyInput,
type PullRequestThreadResolutionInput,
type PullRequestThreadCommentsInput,
type PullRequestThreadCommentsResult,
type PullRequestUpdateInput,
type SourceControlProviderInfo,
type SourceControlProviderKind,
} from "@t3tools/contracts";
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
import { AllowGitHubReserve } from "../sourceControl/GitHubCli.ts";
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
import * as PullRequestFilesViewed from "../persistence/PullRequestFilesViewed.ts";
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
import * as SourceControlRateLimit from "../sourceControl/SourceControlRateLimit.ts";
import {
type ProviderChangeRequest,
type ProviderListCursor,
type PullRequestProviderApi,
PullRequestProviderError,
} from "./PullRequestProvider.ts";
import * as PullRequestReadCache from "./PullRequestReadCache.ts";
import { PullRequestProviderRegistry } from "./PullRequestProviderRegistry.ts";
import * as ViewedFiles from "./pullRequestViewedFiles.ts";
export interface PullRequestMergeEvent extends PullRequestRef {
readonly mergedAt: string;
}
/**
* Rows per repository when the client does not ask for a page size, and rows per slice when a
* listing is carried on from a cursor.
*
* 99 and not 100, because every provider asks its host for one row over this to probe for a next
* page: 99 requests 100, which is exactly what a page of GitHub's API serves — GraphQL refuses
* `first` over 100 with EXCESSIVE_PAGINATION and REST clamps `per_page` to it — and what GitLab
* caps `per_page` at. Asking for 100 here would request 101 and buy a whole second round trip for
* one row (measured: `gh pr list --limit 100` makes 1 HTTP request, `--limit 101` makes 2).
*/
const DEFAULT_REPOSITORY_LIST_LIMIT = 99;
/**
* Repositories read at once. Each one is a CLI process that spends nearly all its wall clock
* waiting on the host, so the useful ceiling is far above the core count; measured over 12
* repositories on this listing's own command, 4 took ~12.7s, 8 ~8.9s and 12 ~4.9s, with 16 and 24
* no faster because 12 already reads every repository in one wave.
*/
const REPOSITORY_CONCURRENCY = 12;
/**
* Repositories named in one read across a host. Measured against GitHub's search: six hundred
* `repo:` qualifiers in one query — 14.7KB of it — were all still honoured, and the answer took
* the same three to six seconds at twelve repositories as at four hundred. A hundred is well
* inside that and past the size of a workspace anyone opens, so a larger one reads in a handful
* of searches rather than in a request per repository.
*/
const REPOSITORY_SEARCH_CHUNK = 100;
/**
* Every read leaves the process — a CLI per repository, against hosts whose limits are low
* (GitHub's search API allows ~30 requests a minute) — so answers are shared for a short
* while and concurrent identical reads share one request. The windows sit near the clients'
* own stale times: long enough that two people opening the same page cost one round trip,
* short enough that "cached" and "fresh" never need telling apart on screen. Reads that
* must not share — the refresh button, a client reloading after its own action — go through
* `invalidate` rather than a flag on the read, so an ordinary read can never opt out.
*/
const LIST_CACHE_TTL = Duration.seconds(30);
const DETAIL_CACHE_TTL = Duration.seconds(15);
const DIFF_CACHE_TTL = Duration.seconds(60);
/** A commit is content-addressed, so its own diff cannot change under its key. */
const COMMIT_DIFF_CACHE_TTL = Duration.minutes(10);
/** Sized like the client's own stale time; a row's counts move only when somebody pushes. */
const LIST_STATS_CACHE_TTL = Duration.seconds(60);
/**
* Short, and with no stale window behind it: this is the reader's own bookkeeping, and the
* press that changes it is the same press the page is already showing optimistically. Held at
* all only so opening a change request on two devices costs one read.
*/
const FILES_VIEWED_CACHE_TTL = Duration.seconds(15);
/** A diff can stay interactive while its next cached value is fetched off the critical path. */
const DIFF_STALE_WINDOW = Duration.minutes(10);
/** How long one host's signed-in login is believed without asking its CLI again. */
const VIEWER_CACHE_TTL = Duration.minutes(10);
const SEARCH_VISIBILITY_TTL = Duration.minutes(10);
const STALE_DETAIL_WINDOW = Duration.minutes(10);
const isPullRequestProviderError = Schema.is(PullRequestProviderError);
const LIST_CACHE_CAPACITY = 64;
const LIST_STATS_CACHE_CAPACITY = 32;
const DETAIL_CACHE_CAPACITY = 128;
const DIFF_CACHE_CAPACITY = 128;
// Each diff cache can retain at most 64 MiB of patch text, counting UTF-16 storage.
const MAX_CACHED_DIFF_PATCH_BYTES = 512 * 1024;
const canCacheDiff = (value: PullRequestDiffResult) =>
value.patch.length * 2 <= MAX_CACHED_DIFF_PATCH_BYTES;
const FILES_VIEWED_CACHE_CAPACITY = 128;
const VIEWER_CACHE_CAPACITY = 32;
export type PullRequestError = PullRequestUnavailableError | PullRequestOperationError;
const routingCredential = Context.Reference<{
readonly credentialFingerprint: string;
readonly viewer: string;
} | null>("t3/PullRequestService/routingCredential", { defaultValue: () => null });
// Internal only: the client cannot choose its cache's credential namespace.
const credentialNamespace = Symbol("pullRequestCredentialNamespace");
type CredentialRef = PullRequestRef & { readonly [credentialNamespace]?: string };
export class PullRequestService extends Context.Service<
PullRequestService,
{
readonly list: (
input: PullRequestListInput,
) => Effect.Effect<PullRequestListResult, PullRequestError>;
readonly listStats: (
input: PullRequestListStatsInput,
) => Effect.Effect<PullRequestListStatsResult, PullRequestError>;
readonly routing: (
input: PullRequestRef,
) => Effect.Effect<PullRequestRoutingResult, PullRequestError>;
readonly routingIdentity: (
input: PullRequestRoutingIdentityInput,
) => Effect.Effect<PullRequestRoutingIdentityResult, PullRequestError>;
readonly withRoutingCredential: <A, E, R>(
input: PullRequestRef,
operation: Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | PullRequestError, R>;
readonly summary: (
input: PullRequestRef,
options?: { readonly recoverTransientFailure?: boolean },
) => Effect.Effect<PullRequestSummary, PullRequestError>;
/**
* The host-native stack the pull request belongs to, or null when it is not in one or the
* host keeps no such object. Cached like a summary; a stack changes about as often.
*/
readonly stack: (
input: PullRequestRef,
options?: { readonly includeDetails?: boolean },
) => Effect.Effect<PullRequestStack | null, PullRequestError>;
readonly subscribeMerges: Effect.Effect<
Stream.Stream<PullRequestMergeEvent>,
never,
Scope.Scope
>;
readonly subscribeRefreshes: Stream.Stream<number>;
readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect<void>;
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
readonly preview: (
input: PullRequestRef,
) => Effect.Effect<PullRequestPreview, PullRequestError>;
readonly activity: (
input: PullRequestRef,
) => Effect.Effect<PullRequestActivity, PullRequestError>;
readonly threadComments: (
input: PullRequestThreadCommentsInput,
) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestError>;
readonly diff: (
input: PullRequestDiffInput,
) => Effect.Effect<PullRequestDiffResult, PullRequestError>;
readonly diffFileContents: (
input: PullRequestDiffFileContentsInput,
) => Effect.Effect<PullRequestDiffFileContentsResult, PullRequestError>;
readonly filesViewed: (
input: PullRequestRef,
) => Effect.Effect<PullRequestFilesViewedResult, PullRequestError>;
readonly setFilesViewed: (
input: PullRequestSetFilesViewedInput,
) => Effect.Effect<void, PullRequestError>;
readonly runAction: (input: PullRequestActionInput) => Effect.Effect<void, PullRequestError>;
readonly update: (input: PullRequestUpdateInput) => Effect.Effect<void, PullRequestError>;
readonly comment: (input: PullRequestCommentInput) => Effect.Effect<void, PullRequestError>;
readonly updateComment: (
input: PullRequestCommentUpdateInput,
) => Effect.Effect<void, PullRequestError>;
readonly submitReview: (
input: PullRequestSubmitReviewInput,
) => Effect.Effect<void, PullRequestError>;
readonly replyToThread: (
input: PullRequestThreadReplyInput,
) => Effect.Effect<void, PullRequestError>;
readonly setThreadResolution: (
input: PullRequestThreadResolutionInput,
) => Effect.Effect<void, PullRequestError>;
readonly setReaction: (
input: PullRequestReactionInput,
) => Effect.Effect<void, PullRequestError>;
readonly reviewerCandidates: (
input: PullRequestRef,
) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestError>;
readonly requestReviewers: (
input: PullRequestReviewerRequestInput,
) => Effect.Effect<void, PullRequestError>;
readonly labelCandidates: (
input: PullRequestRef,
) => Effect.Effect<PullRequestLabelCandidateList, PullRequestError>;
readonly setLabels: (
input: PullRequestLabelChangeInput,
) => Effect.Effect<void, PullRequestError>;
readonly invalidate: (
input: PullRequestInvalidateInput,
options?: { readonly notifyReaders?: boolean },
) => Effect.Effect<void>;
}
>()("t3/pullRequest/PullRequestService") {}
/** What a verdict is called when refusing it, so the sentence reads as an action. */
const VERDICT_LABELS: Record<PullRequestReviewVerdict, string> = {
comment: "review",
approve: "approve",
"request-changes": "request changes on",
};
/**
* Why an action is refused to this viewer, said as the access it would take rather than as the
* refusal the host would have answered with. Merging is the one that needs write and nothing
* else; the other four are also the author's to take, whatever access they have.
*/
const ACTION_ACCESS_REFUSALS: Record<PullRequestAction, string> = {
merge: "You need write access on this repository to merge.",
ready:
"You need write access on this repository, or to have opened this change request, to mark it ready for review.",
draft:
"You need write access on this repository, or to have opened this change request, to return it to a draft.",
close:
"You need write access on this repository, or to have opened this change request, to close it.",
"update-branch":
"You need write access on this repository, or to have opened this change request, to update its branch.",
reopen:
"You need write access on this repository, or to have opened this change request, to reopen it.",
"enable-auto-merge":
"You need write access on this repository to have it merged for you once it is ready.",
"disable-auto-merge":
"You need write access on this repository to stop it being merged for you once it is ready.",
revert: "You need write access on this repository to open a revert pull request.",
"approve-workflows":
"You need write access on this repository to approve workflows from a fork pull request.",
};
/**
* Why asking for a review is refused, and why the menu behind it is too. Write access is what the
* hosts that state anything about this want; the ones that state nothing grant it, so this
* sentence is only ever the answer where a host said no.
*/
const REVIEWER_REQUEST_REFUSAL = "You need write access on this repository to ask for a review.";
const LABEL_CHANGE_REFUSAL = "You need triage access on this repository to change its labels.";
/** A project this page can read: its remote is on a host with an implementation. */
export interface SupportedProject {
readonly cursorKey: string;
readonly project: OrchestrationProjectShell;
readonly api: PullRequestProviderApi;
readonly repository: string;
/** The host the repository lives on, which is the account boundary rather than the kind. */
readonly host: string;
/**
* The identity's canonical key, which is what this environment's own records are keyed by.
* Unique where `repository` is not: Azure's is a bare name that repeats across an organisation.
*/
readonly remote: string;
}
/**
* What the workspace has, split by whether this build can read it. Hosts with no
* implementation are counted rather than dropped, so their projects are explained in the
* provider list instead of quietly missing from the page.
*/
interface WorkspaceProjects {
readonly supported: ReadonlyArray<SupportedProject>;
/** Keyed by host, as the readable ones are: an unimplemented host is its own switcher entry. */
readonly unimplemented: ReadonlyMap<
string,
{ readonly kind: SourceControlProviderKind; readonly projectCount: number }
>;
/**
* Every checkout on a host, including the ones the listing de-duplicated away. Asking who is
* signed in is a question about the host rather than about a repository, and any checkout can
* answer it — so a broken worktree is not allowed to take the host down with it just because
* it happened to be the one the listing kept.
*/
readonly viewerRoots: ReadonlyMap<string, ReadonlyArray<string>>;
}
interface RepositoryBatch {
/** Which repository this slice came from, which is what a cursor for it is filed under. */
readonly key: string;
readonly entries: ReadonlyArray<PullRequestListEntry>;
readonly errors: ReadonlyArray<PullRequestListProjectError>;
readonly truncated: boolean;
readonly nextCursor: string | null;
}
/** What the providers are told, plus the part only the service acts on. */
interface ListCursor extends ProviderListCursor {
/**
* The rows already handed over at exactly `updatedBefore`. The next read asks for that instant
* inclusively, so these are what keeps it from sending them a second time.
*/
readonly seenAt: ReadonlyArray<number>;
}
/**
* A continuation as it travels through the page and back. Written out rather than encoded because
* it comes back from a client and has to be believed or refused on sight: everything a host is
* given is either a timestamp of this shape or a number of this length, which is what lets a
* provider drop it into a filter without checking it again.
*/
const LIST_CURSOR_PATTERN =
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2}))\|(\d{1,9})\|(\d{1,9}(?:,\d{1,9})*)?$/;
function parseListCursor(raw: string): ListCursor | null {
const match = LIST_CURSOR_PATTERN.exec(raw);
if (match === null) return null;
const seenAt = match[3];
return {
updatedBefore: match[1]!,
delivered: Number(match[2]),
seenAt: seenAt === undefined ? [] : seenAt.split(",").map(Number),
};
}
/**
* How a listing tells two repositories apart. The host is part of it because the same
* `owner/repo` exists on github.com and on an Enterprise install, and they are two repositories.
*/
function listCursorKey(host: string, repository: string): string {
return `${host} ${repository.toLowerCase()}`;
}
/**
* Where a repository carries on, worked out from the slice just handed over. The boundary is the
* instant of the oldest row in it: the next read asks for that instant and everything before it,
* and names the rows already sent at it so none of them arrives twice.
*
* The names carry over when the boundary has not moved. A slice that ends on the same instant it
* began on has to keep the earlier rows excluded as well as its own, or the read after it would
* hand them over again.
*/
function nextListCursor(
previous: ListCursor | undefined,
/** What the host handed over, before the rows already sent were dropped from it. */
fetched: ReadonlyArray<ProviderChangeRequest>,
/** What is being sent on, which is what the count of delivered rows is about. */
delivered: ReadonlyArray<ProviderChangeRequest>,
/** A provider may consume malformed offset-paged rows that never appear in `delivered`. */
cursorAdvance = delivered.length,
): string | null {
// The host had nothing at all, so there is no row to carry on from — and repeating the cursor
// that produced the empty slice would ask the same question forever.
if (fetched.length === 0) return null;
// Taken from what the host answered rather than from what survived de-duplication: a slice can
// be entirely rows already sent — a hundred change requests touched in the same second is one
// repository's boring afternoon — and reading "nothing new" as "nothing left" would end the
// walk on the instant it was stuck on, with everything older unreachable for good.
const oldest = fetched.reduce((left, right) => (right.updatedAt < left.updatedAt ? right : left));
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance);
}
/**
* The same cursor against a boundary chosen elsewhere, which is what a slice read across several
* repositories at once needs: every repository in it is read up to the oldest row of the whole
* slice, including the ones that contributed nothing to it — their rows are simply all older, and
* a repository that carried on from its own oldest row would be right about where it stopped and
* silent about the ones that never appeared.
*/
function listCursorAt(
previous: ListCursor | undefined,
boundary: string,
/** This repository's own rows in the slice, before the ones already sent were dropped. */
fetched: ReadonlyArray<ProviderChangeRequest>,
deliveredCount: number,
): string {
const seenAt = [
...(previous?.updatedBefore === boundary ? previous.seenAt : []),
...fetched.filter((item) => item.updatedAt === boundary).map((item) => item.number),
];
return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${seenAt.join(",")}`;
}
/** A host that cannot be read at all, as opposed to one request that failed. */
function isProviderUnusable(error: PullRequestProviderError): boolean {
return error.reason === "missing-tool" || error.reason === "unauthenticated";
}
/**
* Why a host is not readable, told as the thing to do about it. A host that is simply not set up
* says so in the same words the whole-page state uses, rather than repeating whatever its tool
* printed — "HTTP 401" names the symptom, not the fix.
*/
function providerDetail(error: PullRequestProviderError): string {
if (!isProviderUnusable(error)) return error.detail;
return (
pullRequestProviderRequirement(
error.provider,
error.reason === "missing-tool" ? "cli-missing" : "cli-unauthenticated",
) ?? error.detail
);
}
function toUnavailableError(error: PullRequestProviderError): PullRequestUnavailableError {
return new PullRequestUnavailableError({
reason: error.reason === "missing-tool" ? "cli-missing" : "cli-unauthenticated",
provider: error.provider,
cause: error,
});
}
function toPullRequestError(
operation: string,
): (error: PullRequestProviderError) => PullRequestError {
return (error) =>
isProviderUnusable(error)
? toUnavailableError(error)
: new PullRequestOperationError({ operation, detail: error.detail, cause: error });
}
function withRateLimitBackoff(
api: PullRequestProviderApi,
host: string,
limits: SourceControlRateLimit.SourceControlRateLimit["Service"],
options?: { readonly viewerAllowsPause: boolean },
): PullRequestProviderApi {
const key = { provider: api.kind, host };
const protect = <A>(
operation: string,
effect: Effect.Effect<A, PullRequestProviderError>,
allowPaused: boolean,
) =>
limits.check(key, allowPaused ? { allowPaused: true } : undefined).pipe(
Effect.mapError(
(error) =>
new PullRequestProviderError({
provider: api.kind,
operation,
reason: "rate-limited",
detail: error.detail,
retryAt: error.retryAt,
cause: error,
}),
),
Effect.flatMap((lease) =>
effect.pipe(
Effect.provideService(AllowGitHubReserve, allowPaused),
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
Effect.tapError((error) =>
error.reason === "rate-limited"
? limits.recordRateLimit({
...key,
lease,
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
})
: Effect.void,
),
),
),
);
const wrap =
<Args extends ReadonlyArray<unknown>, A>(
operation: string,
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
allowPaused = false,
) =>
(...args: Args) =>
protect(operation, call(...args), allowPaused);
const interactive = <Args extends ReadonlyArray<unknown>, A>(
operation: string,
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
) => wrap(operation, call, true);
const wrapped = {
kind: api.kind,
capabilities: api.capabilities,
// Refused during a pause like any other read, except for the caller that asks for the
// bypass: a lookup that failed is not held, so letting every background read through would
// spawn this host's CLI on each of them and re-extend the pause it was already in.
getViewer:
options?.viewerAllowsPause === true
? interactive("getViewer", api.getViewer)
: wrap("getViewer", api.getViewer),
...(api.getRoutingIdentity === undefined ? {} : { getRoutingIdentity: api.getRoutingIdentity }),
...(api.withVerifiedCredential === undefined
? {}
: { withVerifiedCredential: api.withVerifiedCredential }),
listChangeRequests: wrap("listChangeRequests", api.listChangeRequests),
...(api.listChangeRequestsAcross === undefined
? {}
: {
listChangeRequestsAcross: wrap("listChangeRequestsAcross", api.listChangeRequestsAcross),
}),
...(api.listChangeRequestStats === undefined
? {}
: {
listChangeRequestStats: wrap("listChangeRequestStats", api.listChangeRequestStats),
}),
getChangeRequest: wrap("getChangeRequest", api.getChangeRequest),
...(api.getChangeRequestPreview === undefined
? {}
: { getChangeRequestPreview: wrap("getChangeRequestPreview", api.getChangeRequestPreview) }),
...(api.getChangeRequestSummary === undefined
? {}
: {
getChangeRequestSummary: wrap("getChangeRequestSummary", api.getChangeRequestSummary),
}),
...(api.getChangeRequestStack === undefined
? {}
: { getChangeRequestStack: wrap("getChangeRequestStack", api.getChangeRequestStack) }),
getChangeRequestActivity: wrap("getChangeRequestActivity", api.getChangeRequestActivity),
...(api.getReviewThreadComments === undefined
? {}
: {
getReviewThreadComments: wrap("getReviewThreadComments", api.getReviewThreadComments),
}),
getViewerPermissions: interactive("getViewerPermissions", api.getViewerPermissions),
getDiff: wrap("getDiff", api.getDiff),
...(api.getDiffFileContents === undefined
? {}
: { getDiffFileContents: wrap("getDiffFileContents", api.getDiffFileContents) }),
...(api.getFilesViewed === undefined
? {}
: { getFilesViewed: wrap("getFilesViewed", api.getFilesViewed) }),
...(api.setFilesViewed === undefined
? {}
: { setFilesViewed: interactive("setFilesViewed", api.setFilesViewed) }),
...(api.getFileRevisions === undefined
? {}
: { getFileRevisions: wrap("getFileRevisions", api.getFileRevisions) }),
runAction: interactive("runAction", api.runAction),
...(api.updateChangeRequest === undefined
? {}
: {
updateChangeRequest: interactive("updateChangeRequest", api.updateChangeRequest),
}),
comment: interactive("comment", api.comment),
...(api.updateComment === undefined
? {}
: { updateComment: interactive("updateComment", api.updateComment) }),
submitReview: interactive("submitReview", api.submitReview),
listReviewerCandidates: interactive("listReviewerCandidates", api.listReviewerCandidates),
setReviewerRequest: interactive("setReviewerRequest", api.setReviewerRequest),
...(api.listLabelCandidates === undefined
? {}
: { listLabelCandidates: interactive("listLabelCandidates", api.listLabelCandidates) }),
...(api.setLabels === undefined ? {} : { setLabels: interactive("setLabels", api.setLabels) }),
replyToThread: interactive("replyToThread", api.replyToThread),
setReaction: interactive("setReaction", api.setReaction),
setThreadResolution: interactive("setThreadResolution", api.setThreadResolution),
};
// Optional provider methods must be forwarded too; returning the interface alone permits omissions.
return wrapped satisfies PullRequestProviderApi &
Record<Exclude<keyof PullRequestProviderApi, keyof typeof wrapped>, never>;
}
// Capture before the provider read so a slow response keeps its original freshness through caches.
const observeRead = Effect.fnUntraced(function* <A, E, R>(read: Effect.Effect<A, E, R>) {
const observedAt = yield* Clock.currentTimeMillis;
return { value: yield* read, observedAt };
});
export const make = Effect.gen(function* () {
const mergedPullRequests = yield* PubSub.sliding<PullRequestMergeEvent>(64);
const pullRequestRefreshes = yield* SubscriptionRef.make(0);
const registry = yield* PullRequestProviderRegistry;
const projections = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
const sourceControlProviders = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
const rateLimits = yield* SourceControlRateLimit.SourceControlRateLimit;
const filesViewedStore = yield* PullRequestFilesViewed.PullRequestFilesViewedRepository;
const readCache = yield* PullRequestReadCache.PullRequestReadCache;
const refineUnknownProjectKinds = (
projects: ReadonlyArray<OrchestrationProjectShell>,
filter: Pick<PullRequestListInput, "projectId" | "host">,
) => {
type RefinementCandidate = {
readonly project: OrchestrationProjectShell;
readonly provider: SourceControlProviderInfo;
readonly remoteName: string;
readonly remoteUrl: string;
};
const refinements = new Map<string, RefinementCandidate[]>();
for (const project of projects) {
if (filter.projectId !== undefined && project.id !== filter.projectId) continue;
const identity = project.repositoryIdentity;
if (
(identity?.provider !== "unknown" &&
!(identity?.provider === "forgejo" && isSshRemoteUrl(identity.locator.remoteUrl))) ||
sourceControlRepositorySelector(project.repositoryIdentity) === null
)
continue;
const host = pullRequestHostOf(identity, "unknown");
// A legacy identity has no canonical host until its provider is refined, so it must reach
// the refinement before a host filter can decide whether it belongs in the result.
if (
filter.host !== undefined &&
host !== "unknown" &&
host !== filter.host.toLowerCase() &&
pullRequestHostOf(identity, "forgejo") !== filter.host.toLowerCase() &&
!isSshRemoteUrl(identity.locator.remoteUrl)
) {
continue;
}
const { remoteName, remoteUrl } = identity.locator;
const provider = detectSourceControlProviderFromRemoteUrl(remoteUrl);
if (provider !== null) {
const candidates = refinements.get(provider.baseUrl);
const candidate = { project, provider, remoteName, remoteUrl };
if (candidates === undefined) refinements.set(provider.baseUrl, [candidate]);
else candidates.push(candidate);
}
}
return Effect.forEach(
refinements,
([baseUrl, candidates]) =>
Effect.firstSuccessOf(
candidates.map(({ project, provider, remoteName, remoteUrl }) =>
Effect.suspend(() =>
sourceControlProviders.resolveHandle({
cwd: project.workspaceRoot,
context: {
provider:
provider.kind === "forgejo" ? { ...provider, kind: "unknown" } : provider,
remoteName,
remoteUrl,
...(filter.host !== undefined && isSshRemoteUrl(remoteUrl)
? { requestedHost: filter.host }
: {}),
},
}),
).pipe(
Effect.flatMap((handle) => {
const refined = handle.context?.provider;
return refined === undefined || refined.kind === "unknown"
? Effect.fail(undefined)
: Effect.succeed(refined);
}),
),
),
).pipe(
Effect.map((provider) => [baseUrl, provider] as const),
Effect.orElseSucceed(() => [baseUrl, null] as const),
),
{ concurrency: REPOSITORY_CONCURRENCY },
).pipe(Effect.map((resolved) => new Map(resolved)));
};
const listWorkspaceProjects = (
filter: Pick<PullRequestListInput, "projectId" | "projectIds" | "host">,
): Effect.Effect<WorkspaceProjects, PullRequestError> =>
(filter.projectId === undefined
? projections.getProjectShells(filter.projectIds)
: projections.getProjectShellById(filter.projectId).pipe(Effect.map(Option.toArray))
).pipe(
Effect.mapError(
(error) =>
new PullRequestOperationError({
operation: "listProjects",
detail: "The project list could not be read.",
cause: error,
}),
),
Effect.flatMap((projects) =>
refineUnknownProjectKinds(projects, filter).pipe(
Effect.map((refinedProviders) => ({ refinedProviders, projects })),
),
),
Effect.map(({ refinedProviders, projects }) => {
const supported: SupportedProject[] = [];
const unimplemented = new Map<
string,
{ kind: SourceControlProviderKind; projectCount: number }
>();
const viewerRoots = new Map<string, string[]>();
const seen = new Set<string>();
for (const project of projects) {
if (filter.projectId !== undefined && project.id !== filter.projectId) continue;
if (filter.projectIds !== undefined && !filter.projectIds.includes(project.id)) continue;
const identity = project.repositoryIdentity;
let kind = identity?.provider as SourceControlProviderKind | undefined;
const repository = sourceControlRepositorySelector(project.repositoryIdentity);
if (!identity || kind === undefined || repository === null) continue;
// Worktrees of one repository are separate projects; reading the remote once keeps
// the page from repeating every change request per local checkout. The host is part
// of the key, so the same `owner/repo` on two hosts stays two repositories.
let refinedProvider: SourceControlProviderInfo | null | undefined;
if (
kind === "unknown" ||
(kind === "forgejo" && isSshRemoteUrl(identity.locator.remoteUrl))
) {
const provider = detectSourceControlProviderFromRemoteUrl(identity.locator.remoteUrl);
refinedProvider = provider === null ? null : refinedProviders.get(provider.baseUrl);
kind = refinedProvider?.kind ?? kind;
}
const host =
refinedProvider?.kind === "forgejo"
? new URL(refinedProvider.baseUrl).host.toLowerCase()
: pullRequestHostOf(identity, kind);
if (filter.host !== undefined && host !== filter.host.toLowerCase()) {
continue;
}
const api = registry.get(kind);
// Recorded before the de-duplication below, so the viewer lookup keeps the alternates
// the listing is about to drop.
if (api !== null) {
const roots = viewerRoots.get(host);
if (roots === undefined) viewerRoots.set(host, [project.workspaceRoot]);
else if (!roots.includes(project.workspaceRoot)) roots.push(project.workspaceRoot);
}
const key = listCursorKey(
host,
kind === "azure-devops" ? identity.canonicalKey : repository,
);
if (seen.has(key)) continue;
seen.add(key);
if (api === null) {
const counted = unimplemented.get(host);
if (counted === undefined) unimplemented.set(host, { kind, projectCount: 1 });
else counted.projectCount += 1;
continue;
}
supported.push({
cursorKey: key,
project,
api: withRateLimitBackoff(api, host, rateLimits),
repository,
host,
remote:
kind === "azure-devops"
? identity.canonicalKey
: normalizeGitRemoteUrl(`https://${host}/${repository}`),
});
}
return { supported, unimplemented, viewerRoots };
}),
);
/**
* The project whose checkout and credentials serve a reference. The project's own
* repository is the default; a reference that names a `host` may instead point at any
* repository on that host. Prefer its own checkout; providers with explicit repository
* targeting can fall back to another checkout on the host. Azure derives its organization
* from the checkout, so it requires a matching repository.
*/
const requireProject = (ref: PullRequestRef): Effect.Effect<SupportedProject, PullRequestError> =>
listWorkspaceProjects({ projectId: ref.projectId }).pipe(
Effect.flatMap(({ supported }): Effect.Effect<SupportedProject, PullRequestError> => {
const own = supported[0];
const repository = ref.repository.trim();
const host = ref.host?.trim().toLowerCase();
if (own !== undefined && own.repository.toLowerCase() === repository.toLowerCase()) {
// Hostless references only ever meant the project's own repository, and a hosted one
// naming it still is; either way the project serves itself.
if (host === undefined || host === own.host) return Effect.succeed(own);
}
if (host === undefined) {
if (own === undefined) {
return Effect.fail(new PullRequestUnavailableError({ reason: "provider-unsupported" }));
}
// The repository travels through the client, so it is checked against the project's
// own remote rather than being handed to a provider verbatim.
return Effect.fail(
new PullRequestOperationError({
operation: "resolveRepository",
detail: "The change request does not belong to the selected project.",
}),
);
}
const repositoryKey = canonicalRepositoryKey(`${host}/${repository}`.toLowerCase());
// Azure SSH and legacy clone hosts differ from the browser URL's host. Compare
// the complete repository identity before narrowing those checkouts by host.
return listWorkspaceProjects(
repositoryKey.startsWith("dev.azure.com/") ? {} : { host },
).pipe(
Effect.flatMap(({ supported }) => {
const onHost = supported.filter((candidate) => candidate.host === host);
const route =
supported.find(
(candidate) =>
candidate.api.kind === "azure-devops" &&
candidate.project.repositoryIdentity != null &&
canonicalRepositoryKey(
candidate.project.repositoryIdentity.canonicalKey.toLowerCase(),
) === repositoryKey,
) ??
onHost.find(
(candidate) =>
candidate.api.kind !== "azure-devops" &&
candidate.repository.toLowerCase() === repository.toLowerCase(),
) ??
onHost.find((candidate) => candidate.api.kind !== "azure-devops");
if (route === undefined) {
return Effect.fail(
new PullRequestUnavailableError({ reason: "provider-unsupported" }),
);
}
return Effect.succeed(
route.api.kind === "azure-devops" ||
route.repository.toLowerCase() === repository.toLowerCase()
? route
: {
...route,
repository,
remote: normalizeGitRemoteUrl(`https://${host}/${repository}`),
},
);
}),
);
}),
);
const canonicalRef = Effect.fn("PullRequestService.canonicalRef")(function* <
I extends PullRequestRef,
>(input: I) {
const project = yield* requireProject(input);
return {
...input,
projectId: project.project.id,
host: project.host,
repository: project.repository,
};
});
/**
* What the signed-in account may do with this change request, asked of the host itself. Every
* write goes through it: the page hides what a viewer may not do, and a request that arrived
* without passing through the page — or after the access behind it was withdrawn — must not be
* handed to a provider on the client's word. Read freshly for that reason, rather than taken
* from whatever the detail said when the page loaded.
*/
const viewerPermissionsOf = (
project: SupportedProject,
ref: PullRequestRef,
operation: string,
includeUpdateBranch = false,
) =>
project.api
.getViewerPermissions({
cwd: project.project.workspaceRoot,
repository: project.repository,
host: project.host,
number: ref.number,
includeUpdateBranch,
})
.pipe(Effect.mapError(toPullRequestError(operation)));
/**
* The cursors the page sent back, read once before any host is asked anything. Null where the
* page sent none, which is the listing read from its newest row.
*/
const decodeCursors = (
cursors: PullRequestListInput["cursors"],
): Effect.Effect<ReadonlyMap<string, ListCursor> | null, PullRequestError> => {
if (cursors === undefined) return Effect.succeed(null);
const decoded = new Map<string, ListCursor>();
for (const [key, raw] of Object.entries(cursors)) {
const cursor = parseListCursor(raw);
if (cursor === null) {
return Effect.fail(
new PullRequestOperationError({
operation: "list",
detail: "The list could not be carried on from where it left off.",
}),
);
}
decoded.set(key, cursor);
}
return Effect.succeed(decoded);
};
/**
* One viewer lookup per host, tried across that host's workspaces so a single broken checkout
* cannot hide every healthy repository on it. Per host and not per provider kind: two GitHub
* hosts are two accounts, and the wrong login would misattribute every review request.
*
* Its failure doubles as the answer to "is this host set up", which is what the provider
* switcher shows.
*/
type ResolvedViewer = {
readonly host: string;
readonly kind: SourceControlProviderKind;
readonly viewer: string | null;
readonly error: PullRequestProviderError | null;
};
// Who is signed in moves on the timescale of `gh auth login`, not of a page visit, yet every
// list read was asking each host's CLI again — a subprocess and a network round trip per host
// per read, three reads per page. Only a success is believed for a while: a failure is the
// "is this host set up" answer the provider switcher shows, and holding it would keep saying
// signed-out after the reader has signed in.
const viewersByHost = new Map<string, { readonly at: number; readonly result: ResolvedViewer }>();
const viewerFlights = yield* Cache.makeWith(
(key: string): Effect.Effect<ResolvedViewer> => {
const [host, kind, roots] = JSON.parse(key) as [
string,
SourceControlProviderKind,
ReadonlyArray<string>,
];
const registered = registry.get(kind);
if (registered === null) {
return Effect.die(new Error(`Missing pull request provider: ${kind}`));
}
// Let through a pause: a press the reader is waiting on has to be answered, and the
// callers that are not that press are held back at the gate below instead, before they
// reach this lookup at all.
const api = withRateLimitBackoff(registered, host, rateLimits, { viewerAllowsPause: true });
return Effect.firstSuccessOf(roots.map((cwd) => api.getViewer({ cwd, host }))).pipe(
Effect.map((viewer) => ({
host,
kind,
viewer: viewer as string | null,
error: null as PullRequestProviderError | null,
})),
Effect.tap((result) =>
Effect.map(Clock.currentTimeMillis, (at) => viewersByHost.set(host, { at, result })),
),
Effect.catch((error) =>
Effect.succeed({
host,
kind,
viewer: null,
error,
}),
),
);
},
{
capacity: VIEWER_CACHE_CAPACITY,
// The host-wide success map holds the real ten-minute answer. This short entry exists to
// keep simultaneous cold page reads on one in-flight lookup; failures remain retryable.
timeToLive: (exit) =>
Exit.isSuccess(exit) && exit.value.error === null ? Duration.seconds(1) : Duration.zero,
},
);
const resolveViewers = (
projects: ReadonlyArray<SupportedProject>,
viewerRoots: WorkspaceProjects["viewerRoots"],
options?: { readonly allowPaused: boolean },
) =>
Effect.forEach(
[...new Set(projects.map(({ host }) => host))],