Open History to see the last commit
1import * as Cause from "effect/Cause";
2import * as DateTime from "effect/DateTime";
3import * as Exit from "effect/Exit";
4import * as Option from "effect/Option";
5import * as Result from "effect/Result";
6import * as Schema from "effect/Schema";
7import type {
8 PullRequestActor,
9 PullRequestCheck,
10 PullRequestCheckStatus,
11 PullRequestComment,
12 PullRequestCommit,
13 PullRequestMergeability,
14 PullRequestReviewThread,
15 PullRequestReviewerCandidate,
16 PullRequestState,
17} from "@t3tools/contracts";
18import { TrimmedNonEmptyString } from "@t3tools/contracts";
19import { decodeJsonResult } from "@t3tools/shared/schemaJson";
20
21import { dedupeChecks } from "./pullRequestChecks.ts";
22
23/**
24 * Bitbucket's enums are decoded as plain strings and normalized here, in the same tolerant
25 * style as the GitHub and GitLab decoders: a new pull request state or build status must not
26 * fail a whole payload.
27 */
28const RawUserSchema = Schema.Struct({
29 /**
30 * How Bitbucket addresses an account when a reviewer set is written; the handles it shows are
31 * not accepted there. Braced, and sent back exactly as it arrived.
32 */
33 uuid: Schema.optional(Schema.NullOr(Schema.String)),
34 /** Absent on an app account, which is why `display_name` has to stand in for it. */
35 nickname: Schema.optional(Schema.NullOr(Schema.String)),
36 display_name: Schema.optional(Schema.NullOr(Schema.String)),
37 links: Schema.optional(
38 Schema.NullOr(
39 Schema.Struct({
40 avatar: Schema.optional(
41 Schema.NullOr(Schema.Struct({ href: Schema.optional(Schema.String) })),
42 ),
43 }),
44 ),
45 ),
46});
47
48/**
49 * Required, and required to be non-empty: the wire contract will not carry a change request
50 * without a branch or a link, so a row missing one is skipped rather than breaking the response
51 * it travels in.
52 */
53const RawBranchSchema = Schema.Struct({
54 branch: Schema.Struct({ name: TrimmedNonEmptyString }),
55});
56
57const RawLinkSchema = Schema.Struct({ href: Schema.optional(Schema.String) });
58
59const RawPullRequestSchema = Schema.Struct({
60 id: Schema.Int,
61 title: Schema.String,
62 description: Schema.optional(Schema.NullOr(Schema.String)),
63 state: Schema.optional(Schema.NullOr(Schema.String)),
64 draft: Schema.optional(Schema.Boolean),
65 author: Schema.optional(Schema.NullOr(RawUserSchema)),
66 source: RawBranchSchema,
67 destination: RawBranchSchema,
68 created_on: Schema.String,
69 updated_on: Schema.String,
70 reviewers: Schema.optional(Schema.NullOr(Schema.Array(RawUserSchema))),
71 participants: Schema.optional(
72 Schema.NullOr(
73 Schema.Array(
74 Schema.Struct({
75 user: Schema.optional(Schema.NullOr(RawUserSchema)),
76 role: Schema.optional(Schema.NullOr(Schema.String)),
77 approved: Schema.optional(Schema.Boolean),
78 state: Schema.optional(Schema.NullOr(Schema.String)),
79 participated_on: Schema.optional(Schema.NullOr(Schema.String)),
80 }),
81 ),
82 ),
83 ),
84 links: Schema.Struct({ html: Schema.Struct({ href: TrimmedNonEmptyString }) }),
85});
86
87const RawPageSchema = Schema.Struct({
88 values: Schema.Array(Schema.Unknown),
89 /** A total count, which Bitbucket omits on some endpoints. */
90 size: Schema.optional(Schema.NullOr(Schema.Int)),
91 /** Present only while a further page exists. */
92 next: Schema.optional(Schema.NullOr(Schema.String)),
93});
94
95const RawCommentSchema = Schema.Struct({
96 id: Schema.Int,
97 content: Schema.optional(Schema.NullOr(Schema.Struct({ raw: Schema.optional(Schema.String) }))),
98 user: Schema.optional(Schema.NullOr(RawUserSchema)),
99 created_on: Schema.String,
100 deleted: Schema.optional(Schema.Boolean),
101 /** A comment still being drafted by its author. */
102 pending: Schema.optional(Schema.Boolean),
103 /** Set on a reply, to the comment it answers — which may itself be a reply. */
104 parent: Schema.optional(Schema.NullOr(Schema.Struct({ id: Schema.Int }))),
105 inline: Schema.optional(
106 Schema.NullOr(
107 Schema.Struct({
108 path: Schema.optional(Schema.NullOr(Schema.String)),
109 /** The line in the file as it was; set instead of `to` on a removed line. */
110 from: Schema.optional(Schema.NullOr(Schema.Int)),
111 /** The line in the file as it is now. */
112 to: Schema.optional(Schema.NullOr(Schema.Int)),
113 outdated: Schema.optional(Schema.NullOr(Schema.Boolean)),
114 }),
115 ),
116 ),
117 /** Non-null once someone has marked the thread resolved. */
118 resolution: Schema.optional(Schema.NullOr(Schema.Unknown)),
119 links: Schema.optional(
120 Schema.NullOr(Schema.Struct({ html: Schema.optional(Schema.NullOr(RawLinkSchema)) })),
121 ),
122});
123
124const RawCommitSchema = Schema.Struct({
125 hash: TrimmedNonEmptyString,
126 message: Schema.optional(Schema.NullOr(Schema.String)),
127 date: Schema.optional(Schema.NullOr(Schema.String)),
128 author: Schema.optional(
129 Schema.NullOr(
130 Schema.Struct({
131 raw: Schema.optional(Schema.NullOr(Schema.String)),
132 user: Schema.optional(Schema.NullOr(RawUserSchema)),
133 }),
134 ),
135 ),
136});
137
138const RawStatusSchema = Schema.Struct({
139 key: Schema.optional(Schema.NullOr(Schema.String)),
140 name: Schema.optional(Schema.NullOr(Schema.String)),
141 state: Schema.optional(Schema.NullOr(Schema.String)),
142 description: Schema.optional(Schema.NullOr(Schema.String)),
143 url: Schema.optional(Schema.NullOr(Schema.String)),
144});
145
146const RawDiffstatSchema = Schema.Struct({
147 lines_added: Schema.optional(Schema.NullOr(Schema.Int)),
148 lines_removed: Schema.optional(Schema.NullOr(Schema.Int)),
149});
150
151/** One row of `/workspaces/{workspace}/members`, which wraps the account it is about. */
152const RawMemberSchema = Schema.Struct({
153 user: Schema.optional(Schema.NullOr(RawUserSchema)),
154});
155
156const RawViewerSchema = Schema.Struct({
157 nickname: Schema.optional(Schema.NullOr(Schema.String)),
158 display_name: Schema.optional(Schema.NullOr(Schema.String)),
159});
160
161/**
162 * `/user/permissions/repositories` filtered to one repository, which is the only place Bitbucket
163 * states what the credentials may do with it: nothing on the repository, the pull request or the
164 * workspace carries it. One row, or none where Bitbucket names no permission for this account.
165 */
166const RawRepositoryPermissionsSchema = Schema.Struct({
167 values: Schema.optional(
168 Schema.NullOr(
169 Schema.Array(Schema.Struct({ permission: Schema.optional(Schema.NullOr(Schema.String)) })),
170 ),
171 ),
172});
173
174export interface BitbucketPullRequest {
175 readonly number: number;
176 readonly title: string;
177 readonly url: string;
178 readonly author: PullRequestActor | null;
179 readonly headBranch: string;
180 readonly baseBranch: string;
181 readonly state: PullRequestState;
182 readonly isDraft: boolean;
183 /**
184 * Bitbucket reports no conflict state on a pull request, so the list leaves it unknown. The
185 * detail read asks the conflicts endpoint, which does answer.
186 */
187 readonly mergeability: PullRequestMergeability;
188 readonly createdAt: string;
189 readonly updatedAt: string;
190 readonly body: string;
191 readonly reviewRequestLogins: ReadonlyArray<string>;
192 readonly reviewers: ReadonlyArray<PullRequestActor>;
193 /** The reviewers as Bitbucket addresses them, which is what writing the set back takes. */
194 readonly reviewerIds: ReadonlyArray<string>;
195 /** Approvals and change requests, which Bitbucket keeps on its participants. */
196 readonly reviews: ReadonlyArray<PullRequestComment>;
197}
198
199function trimmed(value: string | null | undefined): string | null {
200 const text = value?.trim() ?? "";
201 return text.length > 0 ? text : null;
202}
203
204/**
205 * Bitbucket stamps times as `+00:00` with microseconds. The page sorts change requests from
206 * every host against each other as plain strings, so they are normalized to the same `Z` form
207 * the other hosts already use.
208 */
209function toIsoUtc(value: string): string {
210 return Option.match(DateTime.make(value), {
211 onNone: () => value,
212 onSome: DateTime.formatIso,
213 });
214}
215
216/** An app account has no nickname, so the display name is the only handle it has. */
217function toActor(raw: Schema.Schema.Type<typeof RawUserSchema> | null | undefined) {
218 const login = trimmed(raw?.nickname) ?? trimmed(raw?.display_name);
219 return login === null
220 ? null
221 : {
222 login,
223 name: trimmed(raw?.display_name),
224 avatarUrl: trimmed(raw?.links?.avatar?.href),
225 };
226}
227
228function toState(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): PullRequestState {
229 switch (raw.state?.trim().toUpperCase()) {
230 case "MERGED":
231 return "merged";
232 case "DECLINED":
233 case "SUPERSEDED":
234 return "closed";
235 default:
236 return "open";
237 }
238}
239
240function toBuildStatus(value: string | null | undefined): PullRequestCheckStatus {
241 switch (value?.trim().toUpperCase()) {
242 case "SUCCESSFUL":
243 return "success";
244 case "FAILED":
245 return "failure";
246 case "STOPPED":
247 return "cancelled";
248 case "INPROGRESS":
249 return "pending";
250 default:
251 return "neutral";
252 }
253}
254
255/**
256 * A participant who has voted is the closest Bitbucket has to a review, so it reads as one in
257 * the conversation. Participants who have only been added carry no verdict and are skipped.
258 */
259function toReviews(
260 raw: Schema.Schema.Type<typeof RawPullRequestSchema>,
261): ReadonlyArray<PullRequestComment> {
262 return (raw.participants ?? []).flatMap((participant): ReadonlyArray<PullRequestComment> => {
263 const author = toActor(participant.user);
264 const votedAt = trimmed(participant.participated_on);
265 const reviewState =
266 trimmed(participant.state) ?? (participant.approved === true ? "approved" : null);
267 if (author === null || votedAt === null || reviewState === null) return [];
268 return [
269 {
270 id: `${raw.id}:${author.login}`,
271 kind: "review",
272 author,
273 body: "",
274 createdAt: toIsoUtc(votedAt),
275 url: null,
276 path: null,
277 reviewState,
278 },
279 ];
280 });
281}
282
283function toPullRequest(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): BitbucketPullRequest {
284 const reviewers = (raw.reviewers ?? []).flatMap((reviewer) => {
285 const actor = toActor(reviewer);
286 return actor === null ? [] : [actor];
287 });
288 return {
289 number: raw.id,
290 title: raw.title,
291 url: raw.links.html.href,
292 author: toActor(raw.author),
293 headBranch: raw.source.branch.name,
294 baseBranch: raw.destination.branch.name,
295 state: toState(raw),
296 isDraft: raw.draft ?? false,
297 mergeability: "unknown",
298 createdAt: toIsoUtc(raw.created_on),
299 updatedAt: toIsoUtc(raw.updated_on),
300 body: raw.description ?? "",
301 reviewRequestLogins: reviewers.map((reviewer) => reviewer.login),
302 reviewers,
303 reviewerIds: (raw.reviewers ?? []).flatMap((reviewer) => trimmed(reviewer.uuid) ?? []),
304 reviews: toReviews(raw),
305 };
306}
307
308const decodePage = decodeJsonResult(RawPageSchema);
309const decodePullRequestEntry = Schema.decodeUnknownExit(RawPullRequestSchema);
310const decodePullRequest = decodeJsonResult(RawPullRequestSchema);
311const decodeCommentEntry = Schema.decodeUnknownExit(RawCommentSchema);
312const decodeCommitEntry = Schema.decodeUnknownExit(RawCommitSchema);
313const decodeStatusEntry = Schema.decodeUnknownExit(RawStatusSchema);
314const decodeDiffstatEntry = Schema.decodeUnknownExit(RawDiffstatSchema);
315const decodeMemberEntry = Schema.decodeUnknownExit(RawMemberSchema);
316const decodeViewer = decodeJsonResult(RawViewerSchema);
317const decodeConflicts = decodeJsonResult(RawPageSchema);
318const decodeRepositoryPermissions = decodeJsonResult(RawRepositoryPermissionsSchema);
319
320type DecodeFailure = Cause.Cause<Schema.SchemaError>;
321
322export interface BitbucketPage<A> {
323 readonly items: ReadonlyArray<A>;
324 /** The whole URL of the next page, which Bitbucket sends rather than an offset. */
325 readonly next: string | null;
326}
327
328/** Malformed entries are skipped rather than failing the page, as on the other hosts. */
329export function decodePullRequestPageJson(
330 raw: string,
331): Result.Result<BitbucketPage<BitbucketPullRequest>, DecodeFailure> {
332 const decoded = decodePage(raw);
333 if (!Result.isSuccess(decoded)) {
334 return Result.fail(decoded.failure);
335 }
336 const items: BitbucketPullRequest[] = [];
337 for (const entry of decoded.success.values) {
338 const item = decodePullRequestEntry(entry);
339 if (Exit.isSuccess(item)) {
340 items.push(toPullRequest(item.value));
341 }
342 }
343 return Result.succeed({ items, next: trimmed(decoded.success.next) });
344}
345
346export function decodePullRequestJson(
347 raw: string,
348): Result.Result<BitbucketPullRequest, DecodeFailure> {
349 const decoded = decodePullRequest(raw);
350 return Result.isSuccess(decoded)
351 ? Result.succeed(toPullRequest(decoded.success))
352 : Result.fail(decoded.failure);
353}
354
355export function decodeViewerJson(raw: string): Result.Result<string | null, DecodeFailure> {
356 const decoded = decodeViewer(raw);
357 return Result.isSuccess(decoded)
358 ? Result.succeed(trimmed(decoded.success.nickname) ?? trimmed(decoded.success.display_name))
359 : Result.fail(decoded.failure);
360}
361
362/**
363 * Whether the configured credentials can write to the repository, which is what merging needs.
364 * Bitbucket answers `admin`, `write` or `read`, and an empty page means it named no permission at
365 * all for this account — an unknown standing, which is granted rather than guessed away.
366 */
367export function decodeRepositoryPermissionJson(raw: string): Result.Result<boolean, DecodeFailure> {
368 const decoded = decodeRepositoryPermissions(raw);
369 if (!Result.isSuccess(decoded)) {
370 return Result.fail(decoded.failure);
371 }
372 const permission = trimmed(decoded.success.values?.[0]?.permission)?.toLowerCase() ?? null;
373 return Result.succeed(permission === null || permission === "admin" || permission === "write");
374}
375
376/**
377 * The workspace's members, which is the nearest thing Bitbucket has to "who may review this".
378 * Nothing on a repository lists the people with access to it — `permissions-config/users` is for
379 * administrators only — and a pull request can be sent to anyone in the workspace, so this is the
380 * list Bitbucket's own reviewer field is filled from too.
381 *
382 * Nobody is marked requested here: who has been asked lives on the pull request, and only the
383 * caller holds both.
384 */
385export function decodeWorkspaceMembersJson(
386 raw: string,
387): Result.Result<BitbucketPage<PullRequestReviewerCandidate>, DecodeFailure> {
388 const decoded = decodePage(raw);
389 if (!Result.isSuccess(decoded)) {
390 return Result.fail(decoded.failure);
391 }
392 const items: PullRequestReviewerCandidate[] = [];
393 for (const entry of decoded.success.values) {
394 const member = decodeMemberEntry(entry);
395 if (Exit.isFailure(member)) continue;
396 const uuid = trimmed(member.value.user?.uuid);
397 const actor = toActor(member.value.user);
398 if (uuid === null || actor === null) continue;
399 items.push({ ...actor, id: uuid, kind: "user", isRequested: false });
400 }
401 return Result.succeed({ items, next: trimmed(decoded.success.next) });
402}
403
404/** One comment as Bitbucket sent it, kept so threads can be assembled across pages. */
405export type BitbucketRawComment = Schema.Schema.Type<typeof RawCommentSchema>;
406
407export interface BitbucketComments {
408 readonly comments: ReadonlyArray<PullRequestComment>;
409 /**
410 * The same comments unread, for `buildReviewThreads`. A reply and the remark it answers can
411 * land on different pages, and only the caller holding every page can put them together.
412 */
413 readonly entries: ReadonlyArray<BitbucketRawComment>;
414 readonly next: string | null;
415}
416
417/**
418 * Bitbucket returns one flat list, so a thread is reassembled from it: a comment pinned to a
419 * line opens a thread, and every reply that leads back to it belongs in it. A reply whose
420 * parent is on a page that was not read has nowhere to go, and is left out rather than shown
421 * as a thread of its own — it still stands in the flat conversation, which needs no parent.
422 */
423export function buildReviewThreads(
424 comments: ReadonlyArray<BitbucketRawComment>,
425): ReadonlyArray<PullRequestReviewThread> {
426 const byId = new Map(comments.map((comment) => [comment.id, comment]));
427 const rootOf = (comment: Schema.Schema.Type<typeof RawCommentSchema>) => {
428 // Bounded by the number of comments read, so a parent cycle cannot spin here.
429 let current = comment;
430 for (let step = 0; step < byId.size; step += 1) {
431 const parent = current.parent === null ? undefined : byId.get(current.parent?.id ?? -1);
432 if (parent === undefined) return current;
433 current = parent;
434 }
435 return current;
436 };
437
438 const threads = new Map<number, PullRequestReviewThread>();
439 const replies = new Map<number, Array<Schema.Schema.Type<typeof RawCommentSchema>>>();
440 for (const comment of comments) {
441 const root = rootOf(comment);
442 const inline = root.inline;
443 const path = trimmed(inline?.path);
444 if (path === null) continue;
445 if (root.id === comment.id) {
446 // `to` is the line as the file stands now, `from` the line it replaced; a comment that
447 // carries only `from` was written against the removed side.
448 const side = inline?.to === null || inline?.to === undefined ? "left" : "right";
449 const line = side === "left" ? inline?.from : inline?.to;
450 threads.set(root.id, {
451 id: String(root.id),
452 path,
453 line: typeof line === "number" && line > 0 ? line : null,
454 side,
455 isResolved: root.resolution !== null && root.resolution !== undefined,
456 isOutdated: inline?.outdated === true,
457 comments: [],
458 });
459 }
460 const bucket = replies.get(root.id);
461 if (bucket === undefined) replies.set(root.id, [comment]);
462 else bucket.push(comment);
463 }
464
465 return [...threads.values()].flatMap((thread) => {
466 const entries = (replies.get(Number(thread.id)) ?? [])
467 .toSorted((left, right) => left.created_on.localeCompare(right.created_on))
468 .map((comment) => ({
469 id: String(comment.id),
470 author: toActor(comment.user),
471 body: comment.content?.raw ?? "",
472 createdAt: toIsoUtc(comment.created_on),
473 url: trimmed(comment.links?.html?.href),
474 }));
475 return entries.length === 0 ? [] : [{ ...thread, comments: entries }];
476 });
477}
478
479/**
480 * Deleted comments and ones their author has not posted yet carry nothing to show. A comment
481 * pinned to a file is a line-level review comment, which is what that kind means.
482 */
483export function decodeCommentsJson(raw: string): Result.Result<BitbucketComments, DecodeFailure> {
484 const decoded = decodePage(raw);
485 if (!Result.isSuccess(decoded)) {
486 return Result.fail(decoded.failure);
487 }
488 const comments: PullRequestComment[] = [];
489 const kept: Array<BitbucketRawComment> = [];
490 for (const entry of decoded.success.values) {
491 const decodedComment = decodeCommentEntry(entry);
492 if (Exit.isFailure(decodedComment)) continue;
493 const comment = decodedComment.value;
494 if (comment.deleted === true || comment.pending === true) continue;
495 const body = comment.content?.raw ?? "";
496 if (body.trim().length === 0) continue;
497 kept.push(comment);
498 const path = trimmed(comment.inline?.path);
499 comments.push({
500 id: String(comment.id),
501 kind: path === null ? "issue-comment" : "review-comment",
502 author: toActor(comment.user),
503 body,
504 createdAt: toIsoUtc(comment.created_on),
505 url: trimmed(comment.links?.html?.href),
506 path,
507 reviewState: null,
508 });
509 }
510 return Result.succeed({ comments, entries: kept, next: trimmed(decoded.success.next) });
511}
512
513export function decodeCommitsJson(
514 raw: string,
515): Result.Result<BitbucketPage<PullRequestCommit>, DecodeFailure> {
516 const decoded = decodePage(raw);
517 if (!Result.isSuccess(decoded)) {
518 return Result.fail(decoded.failure);
519 }
520 const commits: PullRequestCommit[] = [];
521 for (const entry of decoded.success.values) {
522 const decodedCommit = decodeCommitEntry(entry);
523 if (Exit.isFailure(decodedCommit)) continue;
524 const commit = decodedCommit.value;
525 const committedDate = trimmed(commit.date);
526 if (committedDate === null) continue;
527 const linkedAuthor = toActor(commit.author?.user);
528 const rawAuthor = trimmed(commit.author?.raw);
529 commits.push({
530 oid: commit.hash,
531 messageHeadline: (commit.message ?? "").split("\n")[0] ?? "",
532 committedDate: toIsoUtc(committedDate),
533 authors:
534 linkedAuthor !== null
535 ? [linkedAuthor]
536 : rawAuthor === null
537 ? []
538 : [{ login: rawAuthor, name: rawAuthor, avatarUrl: null }],
539 });
540 }
541 // Bitbucket lists a pull request's commits newest first; the timeline reads oldest first.
542 return Result.succeed({ items: commits.toReversed(), next: trimmed(decoded.success.next) });
543}
544
545export function decodeStatusesJson(
546 raw: string,
547): Result.Result<BitbucketPage<PullRequestCheck>, DecodeFailure> {
548 const decoded = decodePage(raw);
549 if (!Result.isSuccess(decoded)) {
550 return Result.fail(decoded.failure);
551 }
552 const checks: Array<{
553 readonly check: PullRequestCheck;
554 readonly workflowName: string | null;
555 readonly at: string | null;
556 }> = [];
557 for (const entry of decoded.success.values) {
558 const decodedStatus = decodeStatusEntry(entry);
559 if (Exit.isFailure(decodedStatus)) continue;
560 const status = decodedStatus.value;
561 const name = trimmed(status.name) ?? trimmed(status.key);
562 if (name === null) continue;
563 // Bitbucket re-uses a status key when a pipeline is run again, so the same check can appear
564 // twice on one page. Nothing decoded here says which copy is newer, so the later one wins,
565 // which is the order Bitbucket writes an update in. The key is kept as the workflow name so
566 // two different pipelines that display the same name are not folded into one.
567 checks.push({
568 check: {
569 name,
570 status: toBuildStatus(status.state),
571 description: trimmed(status.description),
572 url: trimmed(status.url),
573 },
574 workflowName: trimmed(status.key),
575 at: null,
576 });
577 }
578 return Result.succeed({ items: dedupeChecks(checks), next: trimmed(decoded.success.next) });
579}
580
581export interface BitbucketDiffStat {
582 readonly additions: number;
583 readonly deletions: number;
584 readonly changedFiles: number;
585}
586
587export interface BitbucketDiffStatPage extends BitbucketDiffStat {
588 readonly next: string | null;
589}
590
591/** One entry per changed file, each carrying that file's line counts. */
592export function decodeDiffstatJson(
593 raw: string,
594): Result.Result<BitbucketDiffStatPage, DecodeFailure> {
595 const decoded = decodePage(raw);
596 if (!Result.isSuccess(decoded)) {
597 return Result.fail(decoded.failure);
598 }
599 let additions = 0;
600 let deletions = 0;
601 let changedFiles = 0;
602 for (const entry of decoded.success.values) {
603 const decodedStat = decodeDiffstatEntry(entry);
604 if (Exit.isFailure(decodedStat)) continue;
605 additions += decodedStat.value.lines_added ?? 0;
606 deletions += decodedStat.value.lines_removed ?? 0;
607 changedFiles += 1;
608 }
609 return Result.succeed({
610 additions,
611 deletions,
612 changedFiles,
613 next: trimmed(decoded.success.next),
614 });
615}
616
617/**
618 * The conflicts endpoint answers with one entry per conflicting path, so an empty page is the
619 * only statement Bitbucket makes that a pull request merges cleanly.
620 */
621export function decodeConflictsJson(
622 raw: string,
623): Result.Result<PullRequestMergeability, DecodeFailure> {
624 const decoded = decodeConflicts(raw);
625 return Result.isSuccess(decoded)
626 ? Result.succeed(decoded.success.values.length === 0 ? "mergeable" : "conflicting")
627 : Result.fail(decoded.failure);
628}