Open History to see the last commit
1import * as Effect from "effect/Effect";
2import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3tools/contracts";
3
4import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
5import {
6 PullRequestProviderError,
7 type ProviderChangeRequest,
8 type ProviderChangeRequestActivity,
9 type ProviderChangeRequestDetail,
10 type PullRequestProviderApi,
11} from "./PullRequestProvider.ts";
12import type { AzureDevOpsPullRequest } from "./azureDevOpsPullRequestJson.ts";
13
14const CAPABILITIES: PullRequestCapabilities = {
15 // `az repos pr` has no diff command, and the REST route reports changed files without their
16 // contents, so there is no patch to show. The Code tab is hidden rather than empty.
17 diff: false,
18 // Reading a conversation is a plain REST read, but posting one is not something this can
19 // claim without having run it, so the composer stays hidden.
20 comment: false,
21 actions: ["merge", "ready", "draft", "close", "reopen"],
22 // Azure squashes as a completion option; it has no rebase strategy of its own.
23 mergeMethods: ["merge", "squash"],
24 // `az repos pr list` filters by status, creator, reviewer and branch, and by no text at all.
25 search: false,
26 // With no patch to show there are no lines to write against, so nothing here is offered.
27 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
28 // `az repos pr reviewer add` and `remove` name identities, and nothing anywhere in `az repos`
29 // lists the ones this repository could name — that lives behind the identity and graph APIs, a
30 // different service with its own permissions. So the page takes a name here rather than being
31 // handed a menu built out of a guess.
32 reviewers: { request: true, listCandidates: false },
33};
34
35/**
36 * Everything this host offers, granted to whoever is signed in. Azure DevOps states no permission
37 * anywhere `az repos pr show` or `az repos pr list` reach: the answer lives in the security
38 * namespaces, behind identity descriptors and token paths that would be several calls per pull
39 * request to resolve.
40 *
41 * So the actions stay live and a viewer who may not take one is told so by Azure, at the moment
42 * they try. That is the safer half of an unknown: hiding a control from someone entitled to it
43 * leaves them no way through and no reason given.
44 */
45export const AZURE_DEVOPS_VIEWER_PERMISSIONS: PullRequestViewerPermissions = {
46 actions: CAPABILITIES.actions,
47 comment: CAPABILITIES.comment,
48 resolve: CAPABILITIES.review.resolve,
49 verdicts: CAPABILITIES.review.verdicts,
50 requestReviewers: CAPABILITIES.reviewers.request,
51};
52
53/** The CLI tags that mean the tool itself is unusable, rather than one request failing. */
54function reasonFor(
55 error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError,
56): PullRequestProviderError["reason"] {
57 if (error._tag === "AzureDevOpsCliUnavailableError") return "missing-tool";
58 if (error._tag === "AzureDevOpsCliAuthenticationError") return "unauthenticated";
59 return "failed";
60}
61
62function toChangeRequest(pullRequest: AzureDevOpsPullRequest): ProviderChangeRequest {
63 return {
64 number: pullRequest.number,
65 title: pullRequest.title,
66 url: pullRequest.url,
67 author: pullRequest.author,
68 headBranch: pullRequest.headBranch,
69 baseBranch: pullRequest.baseBranch,
70 state: pullRequest.state,
71 isDraft: pullRequest.isDraft,
72 mergeability: pullRequest.mergeability,
73 // Azure reports no line counts on a pull request, and with no patch to read there is
74 // nothing to count them from either.
75 additions: 0,
76 deletions: 0,
77 createdAt: pullRequest.createdAt,
78 updatedAt: pullRequest.updatedAt,
79 reviewRequestLogins: pullRequest.reviewRequestLogins,
80 // Azure keeps labels on work items rather than on the pull request.
81 labels: [],
82 };
83}
84
85export const make = Effect.gen(function* () {
86 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
87
88 const fail =
89 (operation: string) => (error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError) =>
90 new PullRequestProviderError({
91 provider: "azure-devops",
92 operation,
93 reason: reasonFor(error),
94 detail: error.detail,
95 cause: error,
96 });
97
98 /** Refuses what the capabilities already say this host cannot do. */
99 const unsupported = (operation: string) =>
100 Effect.fail(
101 new PullRequestProviderError({
102 provider: "azure-devops",
103 operation,
104 reason: "failed",
105 detail: "Azure DevOps reviews cannot be written from here yet.",
106 }),
107 );
108
109 const provider: PullRequestProviderApi = {
110 kind: "azure-devops",
111 capabilities: CAPABILITIES,
112
113 getViewer: (input) =>
114 cli.getViewer({ cwd: input.cwd }).pipe(Effect.mapError(fail("getViewer"))),
115
116 // `input.query` is deliberately dropped: `az repos pr list` filters by status, creator,
117 // reviewer and branch, and has nothing that matches text. Sending it as one of those would
118 // narrow by the wrong thing, so the page comes back unnarrowed and the caller filters it.
119 listChangeRequests: (input) =>
120 cli
121 .listPullRequests({
122 cwd: input.cwd,
123 repository: input.repository,
124 state: input.state,
125 involvement: input.involvement,
126 viewer: input.viewer,
127 limit: input.limit,
128 cursor: input.cursor,
129 })
130 .pipe(
131 Effect.mapError(fail("listChangeRequests")),
132 Effect.map((batch) => ({
133 items: batch.items.map(toChangeRequest),
134 truncated: batch.truncated,
135 cursorAdvance: batch.cursorAdvance,
136 // Azure answers in one order whether or not it is being carried on from, so a slice
137 // can always be stepped past — by counting, which is all Azure offers.
138 continues: true,
139 })),
140 ),
141
142 getChangeRequest: (input) =>
143 cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
144 Effect.mapError(fail("getChangeRequest")),
145 Effect.map(
146 (pullRequest): ProviderChangeRequestDetail => ({
147 ...toChangeRequest(pullRequest),
148 body: pullRequest.body,
149 changedFiles: 0,
150 mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
151 closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
152 reviewers: pullRequest.reviewers,
153 checks: [],
154 mergeCapabilities: { merge: true, squash: true, rebase: false },
155 viewerPermissions: AZURE_DEVOPS_VIEWER_PERMISSIONS,
156 }),
157 ),
158 ),
159
160 getChangeRequestActivity: (input) =>
161 cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
162 Effect.mapError(fail("getChangeRequestActivity")),
163 Effect.flatMap((pullRequest) =>
164 (pullRequest.threadsUrl === null
165 ? Effect.succeed({ comments: [], truncated: true })
166 : cli.listThreads({ cwd: input.cwd, threadsUrl: pullRequest.threadsUrl }).pipe(
167 Effect.map((comments) => ({ comments, truncated: false })),
168 Effect.orElseSucceed(() => ({ comments: [], truncated: true })),
169 )
170 ).pipe(
171 Effect.map(
172 (conversation): ProviderChangeRequestActivity => ({
173 comments: conversation.comments,
174 commentCount: conversation.comments.length,
175 commentsTruncated: conversation.truncated,
176 reviewThreads: [],
177 commits: [],
178 }),
179 ),
180 ),
181 ),
182 ),
183
184 // No request at all: Azure has nothing to say about the viewer that a pull request read can
185 // reach, so the answer is the same constant the detail carries.
186 getViewerPermissions: () => Effect.succeed(AZURE_DEVOPS_VIEWER_PERMISSIONS),
187
188 // Never called: `capabilities.diff` is false, and the service refuses a diff without it.
189 getDiff: () =>
190 Effect.fail(
191 new PullRequestProviderError({
192 provider: "azure-devops",
193 operation: "getDiff",
194 reason: "failed",
195 detail: "Azure DevOps cannot produce a patch for a pull request.",
196 }),
197 ),
198
199 runAction: (input) =>
200 cli
201 .runPullRequestAction({
202 cwd: input.cwd,
203 number: input.number,
204 action: input.action,
205 ...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
206 })
207 .pipe(Effect.mapError(fail("runAction"))),
208
209 // Never called: `capabilities.reviewers.listCandidates` is false, and the service refuses the
210 // list without it.
211 listReviewerCandidates: () =>
212 Effect.fail(
213 new PullRequestProviderError({
214 provider: "azure-devops",
215 operation: "listReviewerCandidates",
216 reason: "failed",
217 detail: "Azure DevOps cannot say who may review a pull request.",
218 }),
219 ),
220
221 setReviewerRequest: (input) =>
222 cli
223 .setPullRequestReviewers({
224 cwd: input.cwd,
225 number: input.number,
226 // Azure names an identity by an email address or a guid, and has no team to ask, so a
227 // candidate's id is the whole of what it takes.
228 reviewers: input.reviewers.map((reviewer) => reviewer.id),
229 requested: input.requested,
230 })
231 .pipe(Effect.mapError(fail("setReviewerRequest"))),
232
233 // Never called: `capabilities.comment` is false, and the service refuses a comment without it.
234 comment: () => unsupported("comment"),
235
236 // Declared unsupported above, so the service refuses these before a provider is reached.
237 // They exist because every provider answers the whole port.
238 submitReview: () => unsupported("submitReview"),
239
240 replyToThread: () => unsupported("replyToThread"),
241
242 setThreadResolution: () => unsupported("setThreadResolution"),
243 };
244
245 return provider;
246});