The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage"; |
| 2 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 3 | import * as Cache from "effect/Cache"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Exit from "effect/Exit"; |
| 7 | import type { |
| 8 | PullRequestActor, |
| 9 | PullRequestCapabilities, |
| 10 | PullRequestReaction, |
| 11 | PullRequestThreadComment, |
| 12 | PullRequestViewerPermissions, |
| 13 | } from "@t3tools/contracts"; |
| 14 | |
| 15 | import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts"; |
| 16 | import type { GitLabDiffRefs, GitLabMergeRequestListState } from "./gitLabMergeRequestJson.ts"; |
| 17 | import { |
| 18 | PullRequestProviderError, |
| 19 | type PullRequestProviderFailure, |
| 20 | type ProviderChangeRequestActivity, |
| 21 | type ProviderChangeRequestDetail, |
| 22 | type PullRequestProviderApi, |
| 23 | } from "@t3tools/source-control-core/server/PullRequestProvider"; |
| 24 | import { definition } from "../shared/definition.ts"; |
| 25 | |
| 26 | const CAPABILITIES: PullRequestCapabilities = { |
| 27 | diff: true, |
| 28 | comment: true, |
| 29 | actions: [...definition.changeRequestActions], |
| 30 | mergeMethods: ["merge", "squash", "rebase"], |
| 31 | deleteBranch: true, |
| 32 | // GitLab has no merge-target-into-source operation. |
| 33 | updateMethods: ["rebase"], |
| 34 | search: true, |
| 35 | reactions: true, |
| 36 | mentions: true, |
| 37 | referenceSigils: ["#", "!"], |
| 38 | // GitLab's viewed marks live in browser-local storage. |
| 39 | viewedFiles: "environment", |
| 40 | review: { |
| 41 | inlineComment: true, |
| 42 | rangeComment: true, |
| 43 | reply: true, |
| 44 | resolve: true, |
| 45 | verdicts: ["comment", "approve", "request-changes"], |
| 46 | }, |
| 47 | reviewers: { request: true, listCandidates: true, searchCandidates: true }, |
| 48 | labels: true, |
| 49 | edit: { changeRequest: true, comment: true, maintainerEdits: true }, |
| 50 | withheldFileDiff: true, |
| 51 | }; |
| 52 | |
| 53 | const UNAVAILABLE_REQUIREMENT = { |
| 54 | missing: |
| 55 | "GitLab CLI (`glab`) is required to browse change requests on this host. Install it from https://gitlab.com/gitlab-org/cli and reload.", |
| 56 | unauthenticated: "GitLab CLI is not authenticated. Run `glab auth login` and retry.", |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * The actions `user.can_merge` answers for. Rebasing writes to the source branch rather than to |
| 61 | * the target, so it is not literally the same permission — but GitLab reports nothing narrower, |
| 62 | * and someone it will not let land this change has no business rewriting its branch either. |
| 63 | * Reverting pushes a branch to the target project, which the same permission covers. |
| 64 | */ |
| 65 | const MERGE_ACTIONS: ReadonlySet<string> = new Set([ |
| 66 | "merge", |
| 67 | "update-branch", |
| 68 | "enable-auto-merge", |
| 69 | "disable-auto-merge", |
| 70 | "revert", |
| 71 | ]); |
| 72 | |
| 73 | /** Authors may close/reopen/change draft state regardless of role; REST omits authorship. */ |
| 74 | export function gitLabViewerPermissions(input: { |
| 75 | readonly viewerCanMerge: boolean; |
| 76 | readonly viewerCanEditMaintainerEdits?: boolean | undefined; |
| 77 | readonly viewerCanDeleteBranch?: boolean; |
| 78 | readonly viewerCanEditLabels?: boolean | undefined; |
| 79 | }): PullRequestViewerPermissions { |
| 80 | return { |
| 81 | actions: CAPABILITIES.actions.filter((action) => |
| 82 | action === "delete-branch" |
| 83 | ? input.viewerCanDeleteBranch === true |
| 84 | : !MERGE_ACTIONS.has(action) || input.viewerCanMerge, |
| 85 | ), |
| 86 | comment: true, |
| 87 | resolve: true, |
| 88 | verdicts: CAPABILITIES.review.verdicts, |
| 89 | requestReviewers: true, |
| 90 | maintainerEdits: input.viewerCanEditMaintainerEdits === true, |
| 91 | labels: input.viewerCanEditLabels === true, |
| 92 | ...(input.viewerCanMerge ? { updateMethods: CAPABILITIES.updateMethods } : {}), |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * One page view asks for the detail and then its checks within a second, and the poll that |
| 98 | * follows asks again. A minute keeps those to one `glab` call without hiding a pipeline for long; |
| 99 | * a refresh or a finished turn drops it sooner through `forgetChangeRequestReads`. |
| 100 | */ |
| 101 | const MERGE_REQUEST_DETAIL_TTL = Duration.minutes(1); |
| 102 | /** A project's merge methods change in its settings, which nobody does between page views. */ |
| 103 | const PROJECT_MERGE_CAPABILITIES_TTL = Duration.minutes(15); |
| 104 | const READ_CACHE_CAPACITY = 256; |
| 105 | /** A host's GitLab version only changes on upgrade. */ |
| 106 | const GRAPHQL_LIST_SUPPORT_TTL = Duration.hours(1); |
| 107 | |
| 108 | function gitLabProviderFailure( |
| 109 | error: GitLabPullRequestCli.GitLabPullRequestCliError, |
| 110 | ): PullRequestProviderFailure { |
| 111 | if (error._tag === "GitLabCliUnavailableError") return { reason: "missing-tool" }; |
| 112 | if (error._tag === "GitLabCliAuthenticationError") return { reason: "unauthenticated" }; |
| 113 | if (error._tag === "GitLabCliRateLimitError") return { reason: "rate-limited" }; |
| 114 | if (error._tag === "GitLabMergeRequestNotFoundError") return { reason: "not-found" }; |
| 115 | return { reason: "failed" }; |
| 116 | } |
| 117 | |
| 118 | // GitLab keeps a thread on the latest diff while its line survives, so older refs mean the code moved. |
| 119 | const isOutdated = ( |
| 120 | thread: GitLabDiffRefs | undefined, |
| 121 | current: GitLabDiffRefs | undefined, |
| 122 | ): boolean => |
| 123 | thread !== undefined && |
| 124 | current !== undefined && |
| 125 | (thread.baseSha !== current.baseSha || |
| 126 | thread.startSha !== current.startSha || |
| 127 | thread.headSha !== current.headSha); |
| 128 | |
| 129 | export const make = Effect.gen(function* () { |
| 130 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 131 | |
| 132 | // Keyed on the checkout as well: `glab` picks the host and the account to answer as from the |
| 133 | // checkout's remote and config, so two checkouts may read the same project as different people. |
| 134 | const mergeRequestDetails = yield* Cache.makeWith( |
| 135 | (key: string) => { |
| 136 | const [cwd, repository, number, host] = JSON.parse(key) as [string, string, number, string]; |
| 137 | return cli.getMergeRequestDetail({ cwd, repository, number, host }); |
| 138 | }, |
| 139 | { |
| 140 | capacity: READ_CACHE_CAPACITY, |
| 141 | timeToLive: (exit) => (Exit.isSuccess(exit) ? MERGE_REQUEST_DETAIL_TTL : Duration.zero), |
| 142 | }, |
| 143 | ); |
| 144 | const graphqlListSupport = yield* Cache.makeWith( |
| 145 | (key: string) => { |
| 146 | const [cwd, host] = JSON.parse(key) as [string, string | undefined]; |
| 147 | // A host whose version read fails keeps failing it; remember REST rather than ask each poll. |
| 148 | return cli.supportsGraphqlList({ cwd, host }).pipe( |
| 149 | Effect.catchIf( |
| 150 | (cause) => |
| 151 | cause._tag !== "GitLabCliUnavailableError" && |
| 152 | cause._tag !== "GitLabCliAuthenticationError" && |
| 153 | cause._tag !== "GitLabCliRateLimitError", |
| 154 | () => Effect.succeed(false), |
| 155 | ), |
| 156 | ); |
| 157 | }, |
| 158 | { |
| 159 | capacity: READ_CACHE_CAPACITY, |
| 160 | timeToLive: (exit) => (Exit.isSuccess(exit) ? GRAPHQL_LIST_SUPPORT_TTL : Duration.zero), |
| 161 | }, |
| 162 | ); |
| 163 | const projectMergeCapabilities = yield* Cache.makeWith( |
| 164 | (key: string) => { |
| 165 | const [cwd, repository, host] = JSON.parse(key) as [string, string, string]; |
| 166 | return cli.getProjectMergeCapabilities({ cwd, repository, host }); |
| 167 | }, |
| 168 | { |
| 169 | capacity: READ_CACHE_CAPACITY, |
| 170 | timeToLive: (exit) => (Exit.isSuccess(exit) ? PROJECT_MERGE_CAPABILITIES_TTL : Duration.zero), |
| 171 | }, |
| 172 | ); |
| 173 | |
| 174 | const detailKey = (input: { |
| 175 | readonly cwd: string; |
| 176 | readonly host: string; |
| 177 | readonly repository: string; |
| 178 | readonly number: number; |
| 179 | }) => JSON.stringify([input.cwd, input.repository, input.number, input.host]); |
| 180 | const mergeRequestDetail = (input: { |
| 181 | readonly cwd: string; |
| 182 | readonly host: string; |
| 183 | readonly repository: string; |
| 184 | readonly number: number; |
| 185 | }) => Cache.get(mergeRequestDetails, detailKey(input)); |
| 186 | |
| 187 | // Dropped on both sides, as the service does with its own reads: a read that starts while the |
| 188 | // write is in flight must not outlive it. |
| 189 | const writing = |
| 190 | < |
| 191 | I extends { |
| 192 | readonly cwd: string; |
| 193 | readonly host: string; |
| 194 | readonly repository: string; |
| 195 | readonly number: number; |
| 196 | }, |
| 197 | A, |
| 198 | E, |
| 199 | >( |
| 200 | write: (input: I) => Effect.Effect<A, E>, |
| 201 | ) => |
| 202 | (input: I): Effect.Effect<A, E> => { |
| 203 | const invalidate = Cache.invalidate(mergeRequestDetails, detailKey(input)); |
| 204 | return invalidate.pipe(Effect.andThen(write(input)), Effect.ensuring(invalidate)); |
| 205 | }; |
| 206 | |
| 207 | const fail = (operation: string) => (error: GitLabPullRequestCli.GitLabPullRequestCliError) => |
| 208 | new PullRequestProviderError({ |
| 209 | provider: SourceControlProviderKind.make("gitlab"), |
| 210 | operation, |
| 211 | ...gitLabProviderFailure(error), |
| 212 | detail: error.detail, |
| 213 | cause: error, |
| 214 | }); |
| 215 | |
| 216 | // Optional endpoints may lose part of the activity, but an unusable CLI or account must |
| 217 | // reach the caller so it can report authentication failures and pause a throttled host. |
| 218 | const recoverRead = <A>( |
| 219 | read: Effect.Effect<A, GitLabPullRequestCli.GitLabPullRequestCliError>, |
| 220 | fallback: A, |
| 221 | ) => |
| 222 | read.pipe( |
| 223 | Effect.catch((error) => { |
| 224 | const { reason } = gitLabProviderFailure(error); |
| 225 | return reason === "rate-limited" || |
| 226 | reason === "unauthenticated" || |
| 227 | reason === "missing-tool" |
| 228 | ? Effect.fail(error) |
| 229 | : Effect.succeed(fallback); |
| 230 | }), |
| 231 | ); |
| 232 | |
| 233 | // The rows were already read, so even a rate limit only leaves their states unknown. |
| 234 | const listStates = (input: Parameters<typeof cli.getListStates>[0]) => |
| 235 | cli.getListStates(input).pipe( |
| 236 | Effect.catch((error) => |
| 237 | Effect.logWarning("GitLab merge request approval and pipeline read failed", { |
| 238 | repository: input.repository, |
| 239 | reason: error._tag, |
| 240 | }).pipe(Effect.as<ReadonlyMap<number, GitLabMergeRequestListState>>(new Map())), |
| 241 | ), |
| 242 | ); |
| 243 | |
| 244 | // REST omits line counts; the CLI recovers optional stats failures per batch. |
| 245 | const mergeRequestWithStats = ( |
| 246 | input: { |
| 247 | readonly cwd: string; |
| 248 | readonly host: string; |
| 249 | readonly repository: string; |
| 250 | readonly number: number; |
| 251 | }, |
| 252 | detail = cli.getMergeRequestDetail(input), |
| 253 | ) => |
| 254 | Effect.all( |
| 255 | [ |
| 256 | detail, |
| 257 | cli.listMergeRequestStats({ cwd: input.cwd, host: input.host, changeRequests: [input] }), |
| 258 | ], |
| 259 | { |
| 260 | concurrency: 2, |
| 261 | }, |
| 262 | ).pipe( |
| 263 | Effect.map(([mergeRequest, [stat]]) => |
| 264 | stat === undefined |
| 265 | ? mergeRequest |
| 266 | : { ...mergeRequest, additions: stat.additions, deletions: stat.deletions }, |
| 267 | ), |
| 268 | ); |
| 269 | |
| 270 | const provider: PullRequestProviderApi = { |
| 271 | kind: SourceControlProviderKind.make("gitlab"), |
| 272 | capabilities: CAPABILITIES, |
| 273 | mergeMessageRewrite: removeAgentCredits, |
| 274 | unavailableRequirement: UNAVAILABLE_REQUIREMENT, |
| 275 | |
| 276 | getViewer: (input) => cli.getViewerUsername(input).pipe(Effect.mapError(fail("getViewer"))), |
| 277 | |
| 278 | listChangeRequests: (input) => |
| 279 | Cache.get(graphqlListSupport, JSON.stringify([input.cwd, input.host])).pipe( |
| 280 | // Without a known version the REST list still answers, only with fewer filters. |
| 281 | Effect.orElseSucceed(() => false), |
| 282 | Effect.flatMap((graphql) => |
| 283 | cli.listMergeRequests({ |
| 284 | cwd: input.cwd, |
| 285 | host: input.host, |
| 286 | repository: input.repository, |
| 287 | state: input.state, |
| 288 | involvement: input.involvement, |
| 289 | viewer: input.viewer, |
| 290 | limit: input.limit, |
| 291 | query: input.query, |
| 292 | cursor: input.cursor, |
| 293 | filters: input.filters, |
| 294 | graphql, |
| 295 | }), |
| 296 | ), |
| 297 | Effect.flatMap(({ listStates: carried, ...batch }) => |
| 298 | (carried === undefined |
| 299 | ? listStates({ |
| 300 | cwd: input.cwd, |
| 301 | host: input.host, |
| 302 | repository: input.repository, |
| 303 | numbers: batch.items.map((item) => item.number), |
| 304 | }) |
| 305 | : Effect.succeed(carried) |
| 306 | ).pipe( |
| 307 | Effect.map((states) => ({ |
| 308 | ...batch, |
| 309 | items: batch.items.map((item) => withListState(item, states)), |
| 310 | continues: true, |
| 311 | })), |
| 312 | ), |
| 313 | ), |
| 314 | Effect.mapError(fail("listChangeRequests")), |
| 315 | ), |
| 316 | |
| 317 | getFileStats: (input) => |
| 318 | cli.getMergeRequestFileStats(input).pipe(Effect.mapError(fail("getFileStats"))), |
| 319 | |
| 320 | listChangeRequestStats: (input) => |
| 321 | cli.listMergeRequestStats(input).pipe(Effect.mapError(fail("listChangeRequestStats"))), |
| 322 | |
| 323 | getChangeRequestChecks: (input) => |
| 324 | mergeRequestDetail(input).pipe( |
| 325 | Effect.map(({ state, checks }) => ({ state, checks })), |
| 326 | Effect.mapError(fail("getChangeRequestChecks")), |
| 327 | ), |
| 328 | |
| 329 | // Merge settings only affect the detail panel's actions. |
| 330 | getChangeRequestPreview: (input) => |
| 331 | cli |
| 332 | .getMergeRequestDetail({ ...input, maintainerEdits: false }) |
| 333 | .pipe(Effect.mapError(fail("getChangeRequestPreview"))), |
| 334 | |
| 335 | getChangeRequestSummary: (input) => |
| 336 | Effect.all( |
| 337 | [ |
| 338 | mergeRequestWithStats( |
| 339 | input, |
| 340 | cli.getMergeRequestDetail({ ...input, maintainerEdits: false }), |
| 341 | ), |
| 342 | listStates({ |
| 343 | cwd: input.cwd, |
| 344 | host: input.host, |
| 345 | repository: input.repository, |
| 346 | numbers: [input.number], |
| 347 | }), |
| 348 | ], |
| 349 | { concurrency: 2 }, |
| 350 | ).pipe( |
| 351 | Effect.map(([mergeRequest, states]) => withListState(mergeRequest, states)), |
| 352 | Effect.mapError(fail("getChangeRequestSummary")), |
| 353 | ), |
| 354 | |
| 355 | getChangeRequest: (input) => |
| 356 | Effect.all( |
| 357 | [ |
| 358 | mergeRequestWithStats(input, mergeRequestDetail(input)), |
| 359 | Cache.get( |
| 360 | projectMergeCapabilities, |
| 361 | JSON.stringify([input.cwd, input.repository, input.host]), |
| 362 | ), |
| 363 | ], |
| 364 | { concurrency: 2 }, |
| 365 | ).pipe( |
| 366 | Effect.mapError(fail("getChangeRequest")), |
| 367 | Effect.map(([mergeRequest, mergeCapabilities]): ProviderChangeRequestDetail => ({ |
| 368 | ...mergeRequest, |
| 369 | mergeCapabilities, |
| 370 | viewerPermissions: gitLabViewerPermissions(mergeRequest), |
| 371 | // Missing divergence counts mean unknown, not up to date. |
| 372 | baseComparison: |
| 373 | mergeRequest.divergedCommits === undefined |
| 374 | ? "unknown" |
| 375 | : mergeRequest.divergedCommits > 0 |
| 376 | ? "behind" |
| 377 | : "up-to-date", |
| 378 | ...(mergeRequest.divergedCommits === undefined |
| 379 | ? {} |
| 380 | : { behindBy: mergeRequest.divergedCommits }), |
| 381 | })), |
| 382 | ), |
| 383 | |
| 384 | getChangeRequestWatchFingerprint: (input) => |
| 385 | cli |
| 386 | .getMergeRequestWatchFingerprint(input) |
| 387 | .pipe(Effect.mapError(fail("getChangeRequestWatchFingerprint"))), |
| 388 | |
| 389 | getChangeRequestActivity: (input) => |
| 390 | Effect.all( |
| 391 | [ |
| 392 | recoverRead(cli.listNotes(input), { comments: [], truncated: true }), |
| 393 | recoverRead(cli.listCommits(input), []), |
| 394 | recoverRead(cli.listDiscussions(input), { |
| 395 | threads: [], |
| 396 | diffRefsByThreadId: new Map(), |
| 397 | overviewFindingIds: [], |
| 398 | truncated: true, |
| 399 | }), |
| 400 | recoverRead(cli.listReactions(input), { |
| 401 | reactions: [] as ReadonlyArray<PullRequestReaction>, |
| 402 | reactionsByNoteId: new Map<string, ReadonlyArray<PullRequestReaction>>(), |
| 403 | editedAtByNoteId: new Map<string, string>(), |
| 404 | botLogins: new Set<string>(), |
| 405 | }), |
| 406 | // Fresh, not the cached detail: refs from before a push would mark every thread outdated. |
| 407 | recoverRead( |
| 408 | cli.getMergeRequestDetail({ ...input, downstreamChecks: false }).pipe( |
| 409 | Effect.map((mergeRequest) => ({ |
| 410 | url: mergeRequest.url, |
| 411 | diffRefs: mergeRequest.diffRefs, |
| 412 | })), |
| 413 | ), |
| 414 | null, |
| 415 | ), |
| 416 | ], |
| 417 | { concurrency: 5 }, |
| 418 | ).pipe( |
| 419 | Effect.mapError(fail("getChangeRequestActivity")), |
| 420 | Effect.map( |
| 421 | ([notes, commits, discussions, awards, detail]): ProviderChangeRequestActivity => { |
| 422 | const noteUrl = (id: string) => (detail === null ? null : `${detail.url}#note_${id}`); |
| 423 | const findingIds = new Set(discussions.overviewFindingIds); |
| 424 | const edited = (id: string) => { |
| 425 | const editedAt = awards.editedAtByNoteId.get(id); |
| 426 | return editedAt === undefined ? {} : { editedAt }; |
| 427 | }; |
| 428 | const botLogins = new Set([...awards.botLogins].map((login) => login.toLowerCase())); |
| 429 | const withBotFlag = (author: PullRequestActor | null) => |
| 430 | author !== null && botLogins.has(author.login.toLowerCase()) |
| 431 | ? { ...author, isBot: true } |
| 432 | : author; |
| 433 | const withNoteDetails = (comment: PullRequestThreadComment) => ({ |
| 434 | ...comment, |
| 435 | ...edited(comment.id), |
| 436 | author: withBotFlag(comment.author), |
| 437 | url: noteUrl(comment.id), |
| 438 | reactions: awards.reactionsByNoteId.get(comment.id) ?? [], |
| 439 | }); |
| 440 | return { |
| 441 | reactions: awards.reactions, |
| 442 | comments: notes.comments.map((comment) => ({ |
| 443 | ...comment, |
| 444 | ...(findingIds.has(comment.id) ? { kind: "review-comment" as const } : {}), |
| 445 | ...edited(comment.id), |
| 446 | author: withBotFlag(comment.author), |
| 447 | url: noteUrl(comment.id), |
| 448 | reactions: awards.reactionsByNoteId.get(comment.id) ?? [], |
| 449 | })), |
| 450 | commentCount: notes.comments.length, |
| 451 | commentsTruncated: notes.truncated || discussions.truncated, |
| 452 | reviewThreads: discussions.threads.flatMap((thread) => |
| 453 | thread.path === null |
| 454 | ? [] |
| 455 | : [ |
| 456 | { |
| 457 | ...thread, |
| 458 | isOutdated: isOutdated( |
| 459 | discussions.diffRefsByThreadId.get(thread.id), |
| 460 | detail?.diffRefs, |
| 461 | ), |
| 462 | comments: thread.comments.map(withNoteDetails), |
| 463 | }, |
| 464 | ], |
| 465 | ), |
| 466 | overviewThreads: discussions.threads.flatMap((thread) => |
| 467 | thread.path === null |
| 468 | ? [{ ...thread, comments: thread.comments.map(withNoteDetails) }] |
| 469 | : [], |
| 470 | ), |
| 471 | commits, |
| 472 | }; |
| 473 | }, |
| 474 | ), |
| 475 | ), |
| 476 | |
| 477 | // Straight from the host, never the shared read: this is what a write is refused or let |
| 478 | // through on, and access granted or withdrawn within the minute must count. |
| 479 | getViewerPermissions: (input) => |
| 480 | cli |
| 481 | .getMergeRequestDetail({ ...input, downstreamChecks: false }) |
| 482 | .pipe(Effect.mapError(fail("getViewerPermissions")), Effect.map(gitLabViewerPermissions)), |
| 483 | |
| 484 | forgetChangeRequestReads: (scope) => |
| 485 | Cache.keys(mergeRequestDetails).pipe( |
| 486 | Effect.flatMap((keys) => |
| 487 | Effect.forEach( |
| 488 | Array.from(keys).filter((key) => { |
| 489 | if (scope === undefined) return true; |
| 490 | const [, repository, number] = JSON.parse(key) as [string, string, number]; |
| 491 | return ( |
| 492 | repository.toLowerCase() === scope.repository.toLowerCase() && |
| 493 | (scope.number === undefined || number === scope.number) |
| 494 | ); |
| 495 | }), |
| 496 | (key) => Cache.invalidate(mergeRequestDetails, key), |
| 497 | { discard: true }, |
| 498 | ), |
| 499 | ), |
| 500 | ), |
| 501 | |
| 502 | getDiff: (input) => cli.getMergeRequestDiff(input).pipe(Effect.mapError(fail("getDiff"))), |
| 503 | |
| 504 | getDiffFileContents: (input) => |
| 505 | cli.getMergeRequestDiffFileContents(input).pipe(Effect.mapError(fail("getDiffFileContents"))), |
| 506 | |
| 507 | getFileRevisions: (input) => |
| 508 | cli.getFileRevisions(input).pipe( |
| 509 | Effect.mapError(fail("getFileRevisions")), |
| 510 | Effect.map((revisions) => ({ revisions })), |
| 511 | ), |
| 512 | |
| 513 | // MR reviewers are users; groups belong to approval rules. |
| 514 | listReviewerCandidates: (input) => |
| 515 | cli |
| 516 | .listReviewerCandidates({ |
| 517 | cwd: input.cwd, |
| 518 | host: input.host, |
| 519 | repository: input.repository, |
| 520 | number: input.number, |
| 521 | query: input.query, |
| 522 | }) |
| 523 | .pipe(Effect.mapError(fail("listReviewerCandidates"))), |
| 524 | |
| 525 | listLabelCandidates: (input) => |
| 526 | cli |
| 527 | .listLabelCandidates({ |
| 528 | cwd: input.cwd, |
| 529 | host: input.host, |
| 530 | repository: input.repository, |
| 531 | number: input.number, |
| 532 | }) |
| 533 | .pipe(Effect.mapError(fail("listLabelCandidates"))), |
| 534 | |
| 535 | setLabels: writing((input) => |
| 536 | cli |
| 537 | .setLabels({ |
| 538 | cwd: input.cwd, |
| 539 | host: input.host, |
| 540 | repository: input.repository, |
| 541 | number: input.number, |
| 542 | labels: input.labels, |
| 543 | applied: input.applied, |
| 544 | }) |
| 545 | .pipe(Effect.mapError(fail("setLabels"))), |
| 546 | ), |
| 547 | |
| 548 | listRepositoryLabels: (input) => |
| 549 | cli |
| 550 | .listProjectLabels({ |
| 551 | cwd: input.cwd, |
| 552 | host: input.host, |
| 553 | repository: input.repository, |
| 554 | search: input.query, |
| 555 | }) |
| 556 | .pipe(Effect.mapError(fail("listRepositoryLabels"))), |
| 557 | |
| 558 | searchRepositoryAuthors: (input) => |
| 559 | cli |
| 560 | .listProjectUsers({ |
| 561 | cwd: input.cwd, |
| 562 | host: input.host, |
| 563 | repository: input.repository, |
| 564 | search: input.query, |
| 565 | }) |
| 566 | .pipe(Effect.mapError(fail("searchRepositoryAuthors"))), |
| 567 | |
| 568 | searchRepositoryReferences: (input) => |
| 569 | cli |
| 570 | .searchProjectReferences({ |
| 571 | cwd: input.cwd, |
| 572 | host: input.host, |
| 573 | repository: input.repository, |
| 574 | search: input.query, |
| 575 | sigil: input.sigil, |
| 576 | }) |
| 577 | .pipe(Effect.mapError(fail("searchRepositoryReferences"))), |
| 578 | |
| 579 | setReviewerRequest: writing((input) => |
| 580 | cli |
| 581 | .setReviewerRequest({ |
| 582 | cwd: input.cwd, |
| 583 | host: input.host, |
| 584 | repository: input.repository, |
| 585 | number: input.number, |
| 586 | reviewers: input.reviewers, |
| 587 | requested: input.requested, |
| 588 | }) |
| 589 | .pipe(Effect.mapError(fail("setReviewerRequest"))), |
| 590 | ), |
| 591 | |
| 592 | runAction: writing((input) => |
| 593 | cli |
| 594 | .runMergeRequestAction({ |
| 595 | cwd: input.cwd, |
| 596 | host: input.host, |
| 597 | repository: input.repository, |
| 598 | number: input.number, |
| 599 | action: input.action, |
| 600 | ...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }), |
| 601 | ...(input.removeAgentCreditsOnMerge === true ? { removeAgentCreditsOnMerge: true } : {}), |
| 602 | ...(input.expectedHeadSha === undefined |
| 603 | ? {} |
| 604 | : { expectedHeadSha: input.expectedHeadSha }), |
| 605 | }) |
| 606 | .pipe(Effect.mapError(fail("runAction"))), |
| 607 | ), |
| 608 | |
| 609 | updateChangeRequest: writing((input) => |
| 610 | cli |
| 611 | .updateMergeRequest({ |
| 612 | cwd: input.cwd, |
| 613 | host: input.host, |
| 614 | repository: input.repository, |
| 615 | number: input.number, |
| 616 | ...(input.title === undefined ? {} : { title: input.title }), |
| 617 | ...(input.body === undefined ? {} : { description: input.body }), |
| 618 | ...(input.maintainerCanModify === undefined |
| 619 | ? {} |
| 620 | : { allowCollaboration: input.maintainerCanModify }), |
| 621 | }) |
| 622 | .pipe(Effect.mapError(fail("updateChangeRequest"))), |
| 623 | ), |
| 624 | |
| 625 | comment: writing((input) => |
| 626 | cli.commentOnMergeRequest(input).pipe(Effect.mapError(fail("comment"))), |
| 627 | ), |
| 628 | |
| 629 | updateComment: writing((input) => |
| 630 | cli |
| 631 | .updateNote({ |
| 632 | cwd: input.cwd, |
| 633 | host: input.host, |
| 634 | repository: input.repository, |
| 635 | number: input.number, |
| 636 | noteId: input.commentId, |
| 637 | body: input.body, |
| 638 | }) |
| 639 | .pipe(Effect.mapError(fail("updateComment"))), |
| 640 | ), |
| 641 | |
| 642 | submitReview: writing((input) => |
| 643 | cli.submitReview(input).pipe(Effect.mapError(fail("submitReview"))), |
| 644 | ), |
| 645 | |
| 646 | replyToThread: writing((input) => |
| 647 | cli |
| 648 | .replyToDiscussion({ |
| 649 | cwd: input.cwd, |
| 650 | host: input.host, |
| 651 | repository: input.repository, |
| 652 | number: input.number, |
| 653 | discussionId: input.threadId, |
| 654 | body: input.body, |
| 655 | }) |
| 656 | .pipe(Effect.mapError(fail("replyToThread"))), |
| 657 | ), |
| 658 | |
| 659 | setReaction: writing((input) => |
| 660 | cli |
| 661 | .setReaction({ |
| 662 | cwd: input.cwd, |
| 663 | host: input.host, |
| 664 | repository: input.repository, |
| 665 | number: input.number, |
| 666 | ...(input.subjectId === undefined ? {} : { noteId: input.subjectId }), |
| 667 | content: input.content, |
| 668 | reacted: input.reacted, |
| 669 | }) |
| 670 | .pipe(Effect.mapError(fail("setReaction"))), |
| 671 | ), |
| 672 | |
| 673 | setThreadResolution: writing((input) => |
| 674 | cli |
| 675 | .setDiscussionResolution({ |
| 676 | cwd: input.cwd, |
| 677 | host: input.host, |
| 678 | repository: input.repository, |
| 679 | number: input.number, |
| 680 | discussionId: input.threadId, |
| 681 | resolved: input.resolved, |
| 682 | }) |
| 683 | .pipe(Effect.mapError(fail("setThreadResolution"))), |
| 684 | ), |
| 685 | }; |
| 686 | |
| 687 | return provider; |
| 688 | }); |
| 689 | |
| 690 | function withListState<Item extends { readonly number: number }>( |
| 691 | item: Item, |
| 692 | states: ReadonlyMap<number, GitLabMergeRequestListState>, |
| 693 | ): Item & Partial<GitLabMergeRequestListState> { |
| 694 | const state = states.get(item.number); |
| 695 | return state === undefined ? item : { ...item, ...state }; |
| 696 | } |