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

AzureDevOpsPullRequestProvider.ts

275 lines10.9 KB

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