Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AzureDevOpsPullRequestProvider.ts

599 lines21.6 KB

1import { resolvePullRequestAuthorFilter, SourceControlProviderKind } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Semaphore from "effect/Semaphore";
4import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3tools/contracts";
5​
6import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
7import {
8 azureDevOpsFilePatch,
9 azureDevOpsUnreadableFilePatch,
10 formatAzureDevOpsDiffCursor,
11 parseAzureDevOpsDiffCursor,
12 MAX_DIFF_SLICE_BYTES,
13 MAX_DIFF_SLICE_EDITS,
14 MAX_DIFF_SLICE_FILES,
15 byteLength,
16 MAX_FILE_DIFF_EDITS,
17 type AzureDevOpsFileTexts,
18} from "./azureDevOpsDiff.ts";
19import {
20 PullRequestProviderError,
21 type PullRequestProviderFailure,
22 type ProviderChangeRequest,
23 type ProviderChangeRequestActivity,
24 type ProviderChangeRequestDetail,
25 type ProviderChangeRequestSummary,
26 type ProviderDiffSlice,
27 type PullRequestProviderApi,
28} from "@t3tools/source-control-core/server/PullRequestProvider";
29import type {
30 AzureDevOpsChangeEntry,
31 AzureDevOpsItemContent,
32 AzureDevOpsIteration,
33 AzureDevOpsPullRequest,
34 AzureDevOpsRepositoryLocation,
35} from "./azureDevOpsPullRequestJson.ts";
36import { definition } from "../shared/definition.ts";
37​
38/** Each file pays for two Python startups. */
39const DIFF_FILE_CONCURRENCY = 4;
40​
41/** Readers share a spawn budget across requests. */
42export const MAX_DIFF_SPAWNS = 2 * DIFF_FILE_CONCURRENCY;
43​
44export const LOCATION_CACHE_CAPACITY = 128;
45​
46const CAPABILITIES: PullRequestCapabilities = {
47 diff: true,
48 comment: false,
49 actions: [...definition.changeRequestActions],
50 // Azure has no rebase strategy.
51 mergeMethods: ["merge", "squash"],
52 // `az repos pr list` has no text filter.
53 search: false,
54 reactions: false,
55 mentions: false,
56 referenceSigils: [],
57 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
58 // Reviewer candidates require separate identity/graph APIs and permissions.
59 reviewers: { request: true, listCandidates: false, searchCandidates: false },
60 edit: { changeRequest: true, comment: false },
61 // Azure's viewed marks use an undocumented endpoint and are invalidated by each push.
62 viewedFiles: "environment",
63};
64​
65const UNAVAILABLE_REQUIREMENT = {
66 missing:
67 "Azure CLI (`az`) with the Azure DevOps extension is required. Install `az`, then run `az extension add --name azure-devops`.",
68 unauthenticated: "Azure CLI is not signed in. Run `az login` and retry.",
69};
70​
71/**
72 * Azure DevOps states no permission anywhere `az repos pr show` or `az repos pr list` reach: the
73 * answer lives in the security namespaces, behind identity descriptors and token paths that would
74 * be several calls per pull request to resolve.
75 */
76const AZURE_DEVOPS_VIEWER_PERMISSIONS: PullRequestViewerPermissions = {
77 actions: CAPABILITIES.actions,
78 comment: CAPABILITIES.comment,
79 resolve: CAPABILITIES.review.resolve,
80 verdicts: CAPABILITIES.review.verdicts,
81 requestReviewers: CAPABILITIES.reviewers.request,
82};
83​
84function azureDevOpsProviderFailure(
85 error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError,
86): PullRequestProviderFailure {
87 if (error._tag === "AzureDevOpsCliUnavailableError") return { reason: "missing-tool" };
88 if (error._tag === "AzureDevOpsCliAuthenticationError") return { reason: "unauthenticated" };
89 if (error._tag === "AzureDevOpsCliRateLimitError") return { reason: "rate-limited" };
90 if (error._tag === "AzureDevOpsPullRequestNotFoundError") return { reason: "not-found" };
91 return { reason: "failed" };
93​
94function toChangeRequest(pullRequest: AzureDevOpsPullRequest): ProviderChangeRequest {
95 return {
96 number: pullRequest.number,
97 title: pullRequest.title,
98 url: pullRequest.url,
99 author: pullRequest.author,
100 headBranch: pullRequest.headBranch,
101 baseBranch: pullRequest.baseBranch,
102 state: pullRequest.state,
103 isDraft: pullRequest.isDraft,
104 mergeability: pullRequest.mergeability,
105 // Azure omits line counts; computing them requires both sides of every file.
106 additions: 0,
107 deletions: 0,
108 createdAt: pullRequest.createdAt,
109 closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
110 mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
111 updatedAt: pullRequest.updatedAt,
112 reviewRequestLogins: pullRequest.reviewRequestLogins,
113 // Azure keeps labels on work items rather than on the pull request.
114 labels: [],
115 };
117​
118function toSummary(pullRequest: AzureDevOpsPullRequest): ProviderChangeRequestSummary {
119 return {
120 number: pullRequest.number,
121 title: pullRequest.title,
122 url: pullRequest.url,
123 author: pullRequest.author,
124 headBranch: pullRequest.headBranch,
125 baseBranch: pullRequest.baseBranch,
126 state: pullRequest.state,
127 isDraft: pullRequest.isDraft,
128 mergeability: pullRequest.mergeability,
129 closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
130 mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
131 updatedAt: pullRequest.updatedAt,
132 };
134​
135export const make = Effect.gen(function* () {
136 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
137 const diffSpawns = yield* Semaphore.make(MAX_DIFF_SPAWNS);
138 const readItemContent = (input: Parameters<typeof cli.readItemContent>[0]) =>
139 diffSpawns.withPermits(1)(cli.readItemContent(input));
140​
141 const fail =
142 (operation: string) => (error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError) =>
143 new PullRequestProviderError({
144 provider: SourceControlProviderKind.make("azure-devops"),
145 operation,
146 ...azureDevOpsProviderFailure(error),
147 detail: error.detail,
148 cause: error,
149 });
150​
151 const unsupported = (operation: string) =>
152 Effect.fail(
153 new PullRequestProviderError({
154 provider: SourceControlProviderKind.make("azure-devops"),
155 operation,
156 reason: "failed",
157 detail: "Azure DevOps reviews cannot be written from here yet.",
158 }),
159 );
160​
161 const EMPTY_DIFF_SLICE: ProviderDiffSlice = { patch: "", truncated: false, nextCursor: null };
162​
163 // PR locations are immutable; caching avoids full PR reads for viewed-file checks.
164 const locations = new Map<string, AzureDevOpsRepositoryLocation>();
165​
166 const locationOf = (input: { readonly cwd: string; readonly number: number }) => {
167 const key = `${input.cwd} ${input.number}`;
168 const held = locations.get(key);
169 if (held !== undefined) {
170 locations.delete(key);
171 locations.set(key, held);
172 return Effect.succeed(held);
173 }
174 return cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
175 Effect.map((pullRequest) => {
176 const location = pullRequest.location;
177 if (location === null) return null;
178 if (locations.size >= LOCATION_CACHE_CAPACITY) {
179 const oldest = locations.keys().next().value;
180 if (oldest !== undefined) locations.delete(oldest);
181 }
182 locations.set(key, location);
183 return location;
184 }),
185 );
186 };
187​
188 const diffScope = Effect.fn("AzureDevOpsPullRequestProvider.diffScope")(function* (input: {
189 readonly cwd: string;
190 readonly number: number;
191 }) {
192 const location = yield* locationOf(input);
193 if (location === null) return null;
194 const iterations = yield* cli.listIterations({
195 cwd: input.cwd,
196 location,
197 number: input.number,
198 });
199 return { location, iterations };
200 });
201​
202 const EMPTY_ITEM: AzureDevOpsItemContent = { contents: "", isBinary: false };
203​
204 /** Azure fails reads of absent files. */
205 const readTexts = (input: {
206 readonly cwd: string;
207 readonly location: AzureDevOpsRepositoryLocation;
208 readonly iteration: AzureDevOpsIteration;
209 readonly change: Pick<AzureDevOpsChangeEntry, "changeKind" | "path" | "oldPath">;
210 }) =>
211 Effect.all(
212 [
213 input.change.changeKind === "new"
214 ? Effect.succeed(EMPTY_ITEM)
215 : readItemContent({
216 cwd: input.cwd,
217 location: input.location,
218 path: input.change.oldPath,
219 commit: input.iteration.mergeBaseCommit,
220 }),
221 input.change.changeKind === "deleted"
222 ? Effect.succeed(EMPTY_ITEM)
223 : readItemContent({
224 cwd: input.cwd,
225 location: input.location,
226 path: input.change.path,
227 commit: input.iteration.headCommit,
228 }),
229 ],
230 { concurrency: 2 },
231 ).pipe(
232 Effect.map(([oldItem, newItem]): AzureDevOpsFileTexts => ({
233 oldContents: oldItem.contents,
234 newContents: newItem.contents,
235 // Encoded bytes cannot identify binaries; trust Azure's metadata.
236 binary: oldItem.isBinary || newItem.isBinary,
237 })),
238 );
239​
240 const listLatestChanges = (input: {
241 readonly cwd: string;
242 readonly location: AzureDevOpsRepositoryLocation;
243 readonly number: number;
244 readonly iterations: ReadonlyArray<AzureDevOpsIteration>;
245 }) => {
246 const latest = input.iterations.at(-1);
247 return latest === undefined
248 ? Effect.succeed({
249 changes: [],
250 truncated: false,
251 } as AzureDevOpsPullRequestCli.AzureDevOpsIterationChanges)
252 : cli.listIterationChanges({
253 cwd: input.cwd,
254 location: input.location,
255 number: input.number,
256 iterationId: latest.id,
257 });
258 };
259​
260 const provider: PullRequestProviderApi = {
261 kind: SourceControlProviderKind.make("azure-devops"),
262 capabilities: CAPABILITIES,
263 unavailableRequirement: UNAVAILABLE_REQUIREMENT,
264 // The bare repository name repeats across an organization; the project path does not.
265 repositoryKey: ({ canonicalKey }) => canonicalKey,
266​
267 getViewer: (input) =>
268 cli.getViewer({ cwd: input.cwd }).pipe(Effect.mapError(fail("getViewer"))),
269​
270 listChangeRequests: (input) =>
271 cli
272 .listPullRequests({
273 cwd: input.cwd,
274 repository: input.repository,
275 state: input.state,
276 involvement: input.involvement,
277 viewer: input.viewer,
278 limit: input.limit,
279 cursor: input.cursor,
280 author:
281 input.filters?.author === undefined
282 ? undefined
283 : resolvePullRequestAuthorFilter(input.filters.author, input.viewer),
284 })
285 .pipe(
286 Effect.mapError(fail("listChangeRequests")),
287 Effect.map((batch) => ({
288 items: batch.items.map(toChangeRequest),
289 truncated: batch.truncated,
290 cursorAdvance: batch.cursorAdvance,
291 // Azure's ordering is stable across offset-based pages.
292 continues: true,
293 })),
294 ),
295​
296 getChangeRequestPreview: (input) =>
297 cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
298 Effect.mapError(fail("getChangeRequestPreview")),
299 Effect.map((pullRequest) => ({
300 number: pullRequest.number,
301 title: pullRequest.title,
302 url: pullRequest.url,
303 author: pullRequest.author,
304 state: pullRequest.state,
305 isDraft: pullRequest.isDraft,
306 createdAt: pullRequest.createdAt,
307 })),
308 ),
309​
310 getChangeRequestSummary: (input) =>
311 cli
312 .getPullRequest({ cwd: input.cwd, number: input.number })
313 .pipe(Effect.mapError(fail("getChangeRequestSummary")), Effect.map(toSummary)),
314​
315 getChangeRequest: Effect.fn("AzureDevOpsPullRequestProvider.getChangeRequest")(
316 function* (input) {
317 const pullRequest = yield* cli.getPullRequest({ cwd: input.cwd, number: input.number });
318 const location = pullRequest.location;
319 const iterations =
320 location === null
321 ? []
322 : yield* cli
323 .listIterations({ cwd: input.cwd, location, number: input.number })
324 .pipe(Effect.orElseSucceed(() => []));
325 // The trial merge's lastMergeSourceCommit can lag behind a new source push.
326 const headSha = iterations.at(-1)?.headCommit;
327 const changedFiles =
328 location === null
329 ? 0
330 : yield* listLatestChanges({
331 cwd: input.cwd,
332 location,
333 number: input.number,
334 iterations,
335 }).pipe(
336 Effect.map((listed) => listed.changes.length),
337 Effect.orElseSucceed(() => 0),
338 );
339 const detail: ProviderChangeRequestDetail = {
340 ...toChangeRequest(pullRequest),
341 body: pullRequest.body,
342 ...(headSha === undefined ? {} : { headSha }),
343 changedFiles,
344 mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
345 closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
346 reviewers: pullRequest.reviewers,
347 checks: [],
348 mergeCapabilities: { merge: true, squash: true, rebase: false },
349 viewerPermissions: AZURE_DEVOPS_VIEWER_PERMISSIONS,
350 autoMergeEnabled: pullRequest.autoMergeEnabled,
351 ...(pullRequest.autoMergeMethod === undefined
352 ? {}
353 : { autoMergeMethod: pullRequest.autoMergeMethod }),
354 };
355 return detail;
356 },
357 Effect.mapError(fail("getChangeRequest")),
358 ),
359​
360 getChangeRequestActivity: (input) =>
361 cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
362 Effect.mapError(fail("getChangeRequestActivity")),
363 Effect.flatMap((pullRequest) =>
364 (pullRequest.location === null
365 ? Effect.succeed({ comments: [], truncated: true })
366 : cli
367 .listThreads({
368 cwd: input.cwd,
369 location: pullRequest.location,
370 number: input.number,
371 })
372 .pipe(
373 Effect.map((comments) => ({ comments, truncated: false })),
374 Effect.orElseSucceed(() => ({ comments: [], truncated: true })),
375 )
376 ).pipe(
377 Effect.map((conversation): ProviderChangeRequestActivity => ({
378 comments: conversation.comments,
379 commentCount: conversation.comments.length,
380 commentsTruncated: conversation.truncated,
381 reviewThreads: [],
382 commits: [],
383 })),
384 ),
385 ),
386 ),
387​
388 getViewerPermissions: () => Effect.succeed(AZURE_DEVOPS_VIEWER_PERMISSIONS),
389​
390 // `input.commit` is deliberately dropped: Azure states no commit list on a pull request, so
391 // the Code tab has nothing to scope itself to and always asks for the whole change.
392 getDiff: Effect.fn("AzureDevOpsPullRequestProvider.getDiff")(
393 function* (input) {
394 const scope = yield* diffScope(input);
395 if (scope === null) return EMPTY_DIFF_SLICE;
396 const cursor = parseAzureDevOpsDiffCursor(input.cursor);
397 const iteration =
398 cursor === null
399 ? scope.iterations.at(-1)
400 : scope.iterations.find((candidate) => candidate.id === cursor.iterationId);
401 if (iteration === undefined) return EMPTY_DIFF_SLICE;
402 const listed = yield* cli.listIterationChanges({
403 cwd: input.cwd,
404 location: scope.location,
405 number: input.number,
406 iterationId: iteration.id,
407 });
408 const changes = listed.changes;
409​
410 const sections: string[] = [];
411 let truncated = listed.truncated;
412 let bytes = 0;
413 let edits = 0;
414 let index = cursor?.fileIndex ?? 0;
415 let full = false;
416 // Prefetched files beyond the slice must be read again.
417 const batchWidth = () => {
418 if (sections.length === 0) return DIFF_FILE_CONCURRENCY;
419 const admits = (left: number, spent: number) =>
420 Math.ceil(left / Math.max(1, spent / sections.length));
421 return Math.max(
422 1,
423 Math.min(
424 DIFF_FILE_CONCURRENCY,
425 MAX_DIFF_SLICE_FILES - sections.length,
426 admits(MAX_DIFF_SLICE_BYTES - bytes, bytes),
427 admits(MAX_DIFF_SLICE_EDITS - MAX_FILE_DIFF_EDITS - edits, edits),
428 ),
429 );
430 };
431 while (!full && index < changes.length) {
432 const batch = changes.slice(index, index + batchWidth());
433 const read = yield* Effect.forEach(
434 batch,
435 (change) =>
436 readTexts({ cwd: input.cwd, location: scope.location, iteration, change }).pipe(
437 // Auth, rate-limit and CLI failures must reach the caller.
438 Effect.catchTags({
439 AzureDevOpsPullRequestNotFoundError: () => Effect.succeed(null),
440 AzureDevOpsCommandFailedError: () => Effect.succeed(null),
441 AzureDevOpsPullRequestReadError: () => Effect.succeed(null),
442 }),
443 Effect.map((texts) => ({ change, texts })),
444 ),
445 { concurrency: DIFF_FILE_CONCURRENCY },
446 );
447 for (const { change, texts } of read) {
448 // Synchronous diffs need yields to let other server work run.
449 yield* Effect.yieldNow;
450 const file =
451 texts === null
452 ? azureDevOpsUnreadableFilePatch(change)
453 : azureDevOpsFilePatch({ change, texts });
454 sections.push(file.section);
455 bytes += byteLength(file.section);
456 edits += file.edits;
457 truncated = truncated || file.truncated;
458 index += 1;
459 // Add a file before checking budgets so oversized files cannot stall the cursor.
460 if (
461 bytes >= MAX_DIFF_SLICE_BYTES ||
462 edits + MAX_FILE_DIFF_EDITS > MAX_DIFF_SLICE_EDITS ||
463 sections.length >= MAX_DIFF_SLICE_FILES ||
464 file.abandoned
465 ) {
466 full = true;
467 break;
468 }
469 }
470 }
471​
472 const slice: ProviderDiffSlice = {
473 patch: sections.join(""),
474 truncated,
475 nextCursor:
476 index >= changes.length
477 ? null
478 : formatAzureDevOpsDiffCursor({ iterationId: iteration.id, fileIndex: index }),
479 };
480 return slice;
481 },
482 Effect.mapError(fail("getDiff")),
483 ),
484​
485 getDiffFileContents: Effect.fn("AzureDevOpsPullRequestProvider.getDiffFileContents")(
486 function* (input) {
487 const scope = yield* diffScope(input);
488 const iteration = scope?.iterations.at(-1);
489 if (scope === null || iteration === undefined) return { oldContents: "", newContents: "" };
490 return yield* readTexts({
491 cwd: input.cwd,
492 location: scope.location,
493 iteration,
494 change: {
495 changeKind: input.changeType,
496 path: input.newPath,
497 oldPath: input.oldPath,
498 },
499 });
500 },
501 Effect.mapError(fail("getDiffFileContents")),
502 ),
503​
504 /**
505 * A path the change does not carry is at the empty revision, which is where a file the pull
506 * request deletes sits. When the change was too long to follow to its end, those paths are left
507 * out instead: reporting them as deleted would clear a file nobody has read.
508 */
509 getFileRevisions: Effect.fn("AzureDevOpsPullRequestProvider.getFileRevisions")(
510 function* (input) {
511 const revisions = new Map<string, string>();
512 if (input.paths.length === 0) return { revisions };
513 const scope = yield* diffScope(input);
514 if (scope === null) return { revisions };
515 const listed = yield* listLatestChanges({
516 ...scope,
517 cwd: input.cwd,
518 number: input.number,
519 });
520 const marked = new Set(input.paths);
521 for (const change of listed.changes) {
522 if (!marked.has(change.path) || change.objectId === null) continue;
523 revisions.set(change.path, change.objectId);
524 }
525 if (!listed.truncated) {
526 for (const path of input.paths) {
527 if (!revisions.has(path)) revisions.set(path, "");
528 }
529 }
530 return { revisions };
531 },
532 Effect.mapError(fail("getFileRevisions")),
533 ),
534​
535 runAction: (input) =>
536 cli
537 .runPullRequestAction({
538 cwd: input.cwd,
539 number: input.number,
540 action: input.action,
541 ...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
542 })
543 .pipe(
544 Effect.mapError(fail("runAction")),
545 Effect.map(
546 (pullRequest) =>
547 pullRequest && {
548 ...toSummary(pullRequest),
549 autoMergeEnabled: pullRequest.autoMergeEnabled,
550 ...(pullRequest.autoMergeMethod === undefined
551 ? {}
552 : { autoMergeMethod: pullRequest.autoMergeMethod }),
553 },
554 ),
555 ),
556​
557 updateChangeRequest: (input) =>
558 cli
559 .updatePullRequest({
560 cwd: input.cwd,
561 number: input.number,
562 title: input.title,
563 body: input.body,
564 })
565 .pipe(Effect.mapError(fail("updateChangeRequest"))),
566​
567 listReviewerCandidates: () =>
568 Effect.fail(
569 new PullRequestProviderError({
570 provider: SourceControlProviderKind.make("azure-devops"),
571 operation: "listReviewerCandidates",
572 reason: "failed",
573 detail: "Azure DevOps cannot say who may review a pull request.",
574 }),
575 ),
576​
577 setReviewerRequest: (input) =>
578 cli
579 .setPullRequestReviewers({
580 cwd: input.cwd,
581 number: input.number,
582 reviewers: input.reviewers.map((reviewer) => reviewer.id),
583 requested: input.requested,
584 })
585 .pipe(Effect.mapError(fail("setReviewerRequest"))),
586​
587 comment: () => unsupported("comment"),
588​
589 submitReview: () => unsupported("submitReview"),
590​
591 replyToThread: () => unsupported("replyToThread"),
592​
593 setThreadResolution: () => unsupported("setThreadResolution"),
594​
595 setReaction: () => unsupported("setReaction"),
596 };
597​
598 return provider;
599});