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