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

GitLabPullRequestProvider.ts

219 lines7.8 KB

1import * as Effect from "effect/Effect";
2import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3tools/contracts";
3​
4import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
5import {
6 PullRequestProviderError,
7 type ProviderChangeRequestActivity,
8 type ProviderChangeRequestDetail,
9 type PullRequestProviderApi,
10} from "./PullRequestProvider.ts";
11​
12const CAPABILITIES: PullRequestCapabilities = {
13 diff: true,
14 comment: true,
15 actions: ["merge", "ready", "draft", "close", "reopen"],
16 // GitLab offers all three, though a project settles on one; `mergeCapabilities` narrows it.
17 mergeMethods: ["merge", "squash", "rebase"],
18 search: true,
19 review: {
20 inlineComment: true,
21 reply: true,
22 resolve: true,
23 // No "changes requested": GitLab has approval and unresolved discussions, and nothing that
24 // says a merge request has been reviewed and rejected.
25 verdicts: ["comment", "approve"],
26 },
27 reviewers: { request: true, listCandidates: true },
28};
29​
30/**
31 * What the signed-in account may do here. GitLab answers exactly one of these questions per
32 * viewer, on the merge request itself: `user.can_merge`, which is why merging is the only thing
33 * narrowed.
34 *
35 * The rest stay granted. GitLab's REST API reports the viewer's role on the project but never
36 * whether they opened this merge request — and its author may close it, reopen it and move it in
37 * and out of draft whatever their role, just as the author of a note may resolve the discussion
38 * it started. Withholding those controls from the one person entitled to them is the worse of the
39 * two mistakes, so they are offered and GitLab explains any refusal itself.
40 *
41 * Asking for a review is granted for the same reason: GitLab takes a reviewer set from the author
42 * and from anyone with the Developer role, and states neither of those two facts here.
43 */
44export function gitLabViewerPermissions(input: {
45 readonly viewerCanMerge: boolean;
46}): PullRequestViewerPermissions {
47 return {
48 actions: CAPABILITIES.actions.filter((action) => action !== "merge" || input.viewerCanMerge),
49 comment: true,
50 resolve: true,
51 verdicts: CAPABILITIES.review.verdicts,
52 requestReviewers: true,
53 };
55​
56/** The CLI tags that mean the tool itself is unusable, rather than one request failing. */
57function reasonFor(
58 error: GitLabPullRequestCli.GitLabPullRequestCliError,
59): PullRequestProviderError["reason"] {
60 if (error._tag === "GitLabCliUnavailableError") return "missing-tool";
61 if (error._tag === "GitLabCliAuthenticationError") return "unauthenticated";
62 return "failed";
64​
65export const make = Effect.gen(function* () {
66 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
67​
68 const fail = (operation: string) => (error: GitLabPullRequestCli.GitLabPullRequestCliError) =>
69 new PullRequestProviderError({
70 provider: "gitlab",
71 operation,
72 reason: reasonFor(error),
73 detail: error.detail,
74 cause: error,
75 });
76​
77 const provider: PullRequestProviderApi = {
78 kind: "gitlab",
79 capabilities: CAPABILITIES,
80​
81 getViewer: (input) =>
82 cli.getViewerUsername({ cwd: input.cwd }).pipe(Effect.mapError(fail("getViewer"))),
83​
84 listChangeRequests: (input) =>
85 cli
86 .listMergeRequests({
87 cwd: input.cwd,
88 repository: input.repository,
89 state: input.state,
90 involvement: input.involvement,
91 viewer: input.viewer,
92 limit: input.limit,
93 query: input.query,
94 cursor: input.cursor,
95 })
96 .pipe(
97 Effect.mapError(fail("listChangeRequests")),
98 // GitLab is asked for its merge requests by update, newest first, whether or not it is
99 // being carried on from — so every page it answers is one a cursor can continue.
100 Effect.map((batch) => ({ ...batch, continues: true })),
101 ),
102​
103 getChangeRequest: (input) =>
104 Effect.all(
105 [
106 cli.getMergeRequestDetail(input),
107 cli.getProjectMergeCapabilities({ cwd: input.cwd, repository: input.repository }),
108 ],
109 { concurrency: 2 },
110 ).pipe(
111 Effect.mapError(fail("getChangeRequest")),
112 Effect.map(
113 ([mergeRequest, mergeCapabilities]): ProviderChangeRequestDetail => ({
114 ...mergeRequest,
115 mergeCapabilities,
116 viewerPermissions: gitLabViewerPermissions(mergeRequest),
117 }),
118 ),
119 ),
120​
121 getChangeRequestActivity: (input) =>
122 Effect.all(
123 [
124 cli
125 .listNotes(input)
126 .pipe(Effect.orElseSucceed(() => ({ comments: [], truncated: true }))),
127 cli.listCommits(input).pipe(Effect.orElseSucceed(() => [])),
128 cli
129 .listDiscussions(input)
130 .pipe(Effect.orElseSucceed(() => ({ threads: [], truncated: true }))),
131 ],
132 { concurrency: 3 },
133 ).pipe(
134 Effect.mapError(fail("getChangeRequestActivity")),
135 Effect.map(
136 ([notes, commits, discussions]): ProviderChangeRequestActivity => ({
137 comments: notes.comments,
138 // GitLab reports no count of its own, so the walk's own total is the host's: the
139 // notes endpoint carries every comment on the merge request, including the ones
140 // written under a discussion, and it is read until GitLab runs out.
141 commentCount: notes.comments.length,
142 commentsTruncated: notes.truncated || discussions.truncated,
143 reviewThreads: discussions.threads,
144 commits,
145 }),
146 ),
147 ),
148​
149 // The same read the detail takes it from, on its own: `user.can_merge` lives on the merge
150 // request, so there is no cheaper thing to ask GitLab.
151 getViewerPermissions: (input) =>
152 cli
153 .getMergeRequestDetail(input)
154 .pipe(Effect.mapError(fail("getViewerPermissions")), Effect.map(gitLabViewerPermissions)),
155​
156 getDiff: (input) => cli.getMergeRequestDiff(input).pipe(Effect.mapError(fail("getDiff"))),
157​
158 // Users only: GitLab requests a review of a person, and the groups that can stand in for one
159 // appear in approval rules rather than in a merge request's reviewers.
160 listReviewerCandidates: (input) =>
161 cli
162 .listReviewerCandidates({
163 cwd: input.cwd,
164 repository: input.repository,
165 number: input.number,
166 })
167 .pipe(Effect.mapError(fail("listReviewerCandidates"))),
168​
169 setReviewerRequest: (input) =>
170 cli
171 .setReviewerRequest({
172 cwd: input.cwd,
173 repository: input.repository,
174 number: input.number,
175 reviewers: input.reviewers,
176 requested: input.requested,
177 })
178 .pipe(Effect.mapError(fail("setReviewerRequest"))),
179​
180 runAction: (input) =>
181 cli
182 .runMergeRequestAction({
183 cwd: input.cwd,
184 repository: input.repository,
185 number: input.number,
186 action: input.action,
187 ...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
188 })
189 .pipe(Effect.mapError(fail("runAction"))),
190​
191 comment: (input) => cli.commentOnMergeRequest(input).pipe(Effect.mapError(fail("comment"))),
192​
193 submitReview: (input) => cli.submitReview(input).pipe(Effect.mapError(fail("submitReview"))),
194​
195 replyToThread: (input) =>
196 cli
197 .replyToDiscussion({
198 cwd: input.cwd,
199 repository: input.repository,
200 number: input.number,
201 discussionId: input.threadId,
202 body: input.body,
203 })
204 .pipe(Effect.mapError(fail("replyToThread"))),
205​
206 setThreadResolution: (input) =>
207 cli
208 .setDiscussionResolution({
209 cwd: input.cwd,
210 repository: input.repository,
211 number: input.number,
212 discussionId: input.threadId,
213 resolved: input.resolved,
214 })
215 .pipe(Effect.mapError(fail("setThreadResolution"))),
216 };
217​
218 return provider;
219});