Skip to content

Commit c5a1fa7

Browse files
fix(bitbucket): preserve source in native mention displays
1 parent 5b80b87 commit c5a1fa7

7 files changed

Lines changed: 53 additions & 18 deletions

File tree

‎apps/mobile/src/features/pull-requests/pullRequestDiffComments.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ describe("PR comments in continuous native diffs", () => {
8989
]);
9090
expect(annotated.commentTargetsByRowId).toBe(data.commentTargetsByRowId);
9191
});
92+
it("substitutes mention labels while preserving the native row's authored source", () => {
93+
const token = "@{account-id}";
94+
const source = `See [design](https://example.test/spec) and \`${token}\`; <span title="literal">HTML</span> and \\_literal\\_. Ping `;
95+
const comment = {
96+
id: "comment",
97+
author: null,
98+
body: `${source}${token}`,
99+
displayBody: `${source}@Alex\\_Smith \\& Co`,
100+
displayText: `${source}@Alex_Smith & Co`,
101+
createdAt: "2026-10-02T00:00:00Z",
102+
updatedAt: null,
103+
url: null,
104+
};
105+
const annotated = annotatePullRequestDiff(
106+
data,
107+
[{ ...thread, comments: [comment] }],
108+
[],
109+
"abc",
110+
);
111+
expect(annotated.rows.find((row) => row.id === "discussion:t")?.commentText).toBe(
112+
`Discussion\n${source}@Alex_Smith & Co\n1 comments · Tap to open`,
113+
);
114+
expect(comment.body).toBe(`${source}${token}`);
115+
expect(comment.displayText).toBe(`${source}@Alex_Smith & Co`);
116+
expect(comment.displayBody).toBe(`${source}@Alex\\_Smith \\& Co`);
117+
});
92118
it("does not guess positions for missing lines, notices, or binary files", () => {
93119
expect(reviewPosition(data.rows[0]!)).toBeNull();
94120
expect(reviewPosition(data.rows[1]!)).toEqual({ kind: "deleted", oldLine: 2 });

‎apps/mobile/src/features/pull-requests/pullRequestDiffComments.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import type { PullRequestReviewPosition, PullRequestReviewThread } from "@t3tool
22
import type { NativeReviewDiffRow } from "../diffs/nativeReviewDiffSurface";
33
import type { NativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
44
import type { PendingReviewComment } from "./pullRequestReviewDrafts";
5-
import { parseMarkdownDocument } from "../../lib/markdownDocument";
6-
import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent";
75

86
export function reviewPosition(row: NativeReviewDiffRow): PullRequestReviewPosition | null {
97
if (row.kind !== "line") return null;
@@ -78,10 +76,7 @@ export function annotatePullRequestDiff(
7876
: lines.get(JSON.stringify([thread.path, thread.side, thread.line]));
7977
if (!anchor) continue;
8078
const first = thread.comments[0];
81-
const preview =
82-
first?.displayBody === undefined
83-
? (first?.body ?? "")
84-
: pullRequestDescriptionContent(parseMarkdownDocument(first.displayBody)).preview;
79+
const preview = first?.displayText ?? first?.body ?? "";
8580
attach(
8681
anchor,
8782
`discussion:${thread.id}`,

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,7 @@ describe("mention display source", () => {
20782078
...TIMELINE_SOURCE.comments[0]!,
20792079
body: "@{account-id}",
20802080
displayBody: "@Alex Smith",
2081+
displayText: "@Alex Smith",
20812082
};
20822083
const untouched = { ...comment, id: "other" };
20832084
const edited = editPullRequestThreadComment(
@@ -2087,10 +2088,15 @@ describe("mention display source", () => {
20872088
);
20882089
expect(edited[0]?.body).toBe("@{account-id} updated");
20892090
expect(edited[0]?.displayBody).toBeUndefined();
2091+
expect(edited[0]?.displayText).toBeUndefined();
20902092
const events = buildPullRequestTimeline({ ...TIMELINE_SOURCE, comments: edited });
20912093
expect(events.find((event) => event.id === comment.id)?.body).toBe("@{account-id} updated");
20922094
expect(events.find((event) => event.id === untouched.id)?.body).toBe("@Alex Smith");
20932095
expect(edited[1]).toBe(untouched);
2094-
expect(comment).toMatchObject({ body: "@{account-id}", displayBody: "@Alex Smith" });
2096+
expect(comment).toMatchObject({
2097+
body: "@{account-id}",
2098+
displayBody: "@Alex Smith",
2099+
displayText: "@Alex Smith",
2100+
});
20952101
});
20962102
});

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ export function editPullRequestThreadComment<
166166
readonly id: string;
167167
readonly body: string;
168168
readonly displayBody?: string | undefined;
169+
readonly displayText?: string | undefined;
169170
},
170171
>(comments: ReadonlyArray<T>, commentId: string, body: string): ReadonlyArray<T> {
171172
return comments.map((comment) => {
172173
if (comment.id !== commentId) return comment;
173-
return { ...comment, body, displayBody: undefined };
174+
return { ...comment, body, displayBody: undefined, displayText: undefined };
174175
});
175176
}
176177

‎packages/source-control-bitbucket/src/server/bitbucketMentions.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function mentionLabels(html: string) {
8080
return labels;
8181
}
8282

83-
export function bitbucketMentionDisplayBody(body: string, html: unknown) {
83+
export function bitbucketMentionDisplay(body: string, html: unknown) {
8484
if (typeof html !== "string" || !html || !body.includes("@{")) return undefined;
8585
const labels = mentionLabels(html);
8686
if (labels.size === 0) return undefined;
@@ -108,7 +108,7 @@ export function bitbucketMentionDisplayBody(body: string, html: unknown) {
108108
replacements.push({
109109
start: start + match.index,
110110
end: start + match.index + match[0].length,
111-
label: label.replace(/[\\`*_{}[\]()<>!#|~&]/gu, "\\$&"),
111+
label,
112112
});
113113
}
114114
}
@@ -117,12 +117,16 @@ export function bitbucketMentionDisplayBody(body: string, html: unknown) {
117117
visit(tree);
118118
if (replacements.length === 0) return undefined;
119119
const fragments: string[] = [];
120+
const textFragments: string[] = [];
120121
let cursor = 0;
121122
for (const replacement of replacements) {
122-
fragments.push(body.slice(cursor, replacement.start), replacement.label);
123+
const source = body.slice(cursor, replacement.start);
124+
fragments.push(source, replacement.label.replace(/[\\`*_{}[\]()<>!#|~&]/gu, "\\$&"));
125+
textFragments.push(source, replacement.label);
123126
cursor = replacement.end;
124127
}
125128
fragments.push(body.slice(cursor));
129+
textFragments.push(body.slice(cursor));
126130
const displayBody = fragments.join("");
127-
return displayBody === body ? undefined : displayBody;
131+
return displayBody === body ? undefined : { displayBody, displayText: textFragments.join("") };
128132
}

‎packages/source-control-bitbucket/src/server/bitbucketPullRequestJson.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,13 @@ describe("mention display bodies", () => {
409409
`\`\`\`text\n${token}\n\`\`\``,
410410
` ${token}`,
411411
`\\${token}`,
412+
"\\_literal\\_",
412413
`[${token}](https://example.com/${token})`,
413414
`<span title="${token}">${token}</span>`,
414415
].join("\n\n");
415416
const body = `## Review\n\n**Thanks ${token}**\n\n${literals}\n\n> ${token}, ask @{unknown}.\n\n${token} <br> <span><em>${token}</em></span> ${token}`;
416417
const displayBody = `## Review\n\n**Thanks @Alex\\_Smith \\& Co**\n\n${literals}\n\n> @Alex\\_Smith \\& Co, ask @{unknown}.\n\n@Alex\\_Smith \\& Co <br> <span><em>${token}</em></span> @Alex\\_Smith \\& Co`;
418+
const displayText = `## Review\n\n**Thanks @Alex_Smith & Co**\n\n${literals}\n\n> @Alex_Smith & Co, ask @{unknown}.\n\n@Alex_Smith & Co <br> <span><em>${token}</em></span> @Alex_Smith & Co`;
417419
const genericBody = `Returns Promise<void> now. Use <T> generics.\n\n${token}`;
418420
expect(
419421
expectSuccess(
@@ -475,10 +477,11 @@ describe("mention display bodies", () => {
475477
Schema.encodeSync(threadsCodec)(buildReviewThreads(decoded.entries)),
476478
);
477479
expect(threads.flatMap((thread) => thread.comments)).toMatchObject([
478-
{ body, displayBody },
480+
{ body, displayBody, displayText },
479481
{ body: token },
480482
]);
481483
expect(threads[1]?.comments[0]?.displayBody).toBeUndefined();
484+
expect(threads[1]?.comments[0]?.displayText).toBeUndefined();
482485

483486
const missingDescription = expectSuccess(
484487
decodePullRequestJson(JSON.stringify(pullRequest({ description: token }))),

‎packages/source-control-bitbucket/src/server/bitbucketPullRequestJson.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { TrimmedNonEmptyString } from "@t3tools/contracts";
1919
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
2020
import { dedupeChecks } from "@t3tools/source-control-core/server/pullRequestChecks";
2121

22-
import { bitbucketMentionDisplayBody } from "./bitbucketMentions.ts";
22+
import { bitbucketMentionDisplay } from "./bitbucketMentions.ts";
2323

2424
/**
2525
* Bitbucket's enums are decoded as plain strings and normalized here, in the same tolerant
@@ -298,15 +298,15 @@ function toPullRequest(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): Bi
298298
updatedAt: toIsoUtc(raw.updated_on),
299299
closedAt: raw.closed_on == null ? null : toIsoUtc(raw.closed_on),
300300
body: raw.description ?? "",
301-
displayBody: bitbucketMentionDisplayBody(
301+
displayBody: bitbucketMentionDisplay(
302302
raw.description ?? "",
303303
Option.getOrUndefined(
304304
Option.map(
305305
decodeRenderedDescription(raw.rendered),
306306
(rendered) => rendered.description.html,
307307
),
308308
),
309-
),
309+
)?.displayBody,
310310
reviewRequestLogins: reviewers.map((reviewer) => reviewer.login),
311311
reviewers,
312312
reviewerIds: (raw.reviewers ?? []).flatMap((reviewer) => trimmed(reviewer.uuid) ?? []),
@@ -457,7 +457,7 @@ export function buildReviewThreads(
457457
id: String(comment.id),
458458
author: toActor(comment.user),
459459
body: comment.content?.raw ?? "",
460-
displayBody: bitbucketMentionDisplayBody(comment.content?.raw ?? "", comment.content?.html),
460+
...bitbucketMentionDisplay(comment.content?.raw ?? "", comment.content?.html),
461461
createdAt: toIsoUtc(comment.created_on),
462462
url: trimmed(comment.links?.html?.href),
463463
}));
@@ -486,7 +486,7 @@ export function decodeCommentsJson(raw: string): Result.Result<BitbucketComments
486486
kind: path === null ? "issue-comment" : "review-comment",
487487
author: toActor(comment.user),
488488
body,
489-
displayBody: bitbucketMentionDisplayBody(body, comment.content?.html),
489+
displayBody: bitbucketMentionDisplay(body, comment.content?.html)?.displayBody,
490490
createdAt: toIsoUtc(comment.created_on),
491491
url: trimmed(comment.links?.html?.href),
492492
path,

0 commit comments

Comments
 (0)