From 647d26679d880db4348c5d91652bf46469b9bd4f Mon Sep 17 00:00:00 2001 From: Federico Brancasi Date: Sun, 29 Nov 2026 21:17:05 +0100 Subject: [PATCH 1/3] Fix permalinks on the same line merging in review comments When a review comment has two code permalinks to the repository on the same line, the comment widget in the editor shows only one code snippet and drops the rest of the line. For example, `Compare https://github.com/owner/repo/blob//src/a.ts#L1 and https://github.com/owner/repo/blob//src/b.ts#L2-L3` is shown as a single snippet of `src/b.ts`: the `src/a.ts` link and the word "and" are gone, and the `src/b.ts` link points to the whole text from the first URL to the second one. Why: `GHPRComment.replacePermalink` matches the owner with `(.+)` and the file path with `(.*)`. Both are greedy, so one match runs from the first `https://github.com/` to the last `#L` on the line. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/github/prComment.ts | 2 +- src/test/github/prComment.test.ts | 35 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/github/prComment.ts b/src/github/prComment.ts index eec7df13a0..5e8b94decf 100644 --- a/src/github/prComment.ts +++ b/src/github/prComment.ts @@ -391,7 +391,7 @@ ${args[3] ?? ''} } const repoName = escapeRegExp(githubRepository.remote.repositoryName); - const expression = new RegExp(`https://github.com/(.+)/${repoName}/blob/([0-9a-f]{40})/(.*)#L([0-9]+)(-L([0-9]+))?`, 'g'); + const expression = new RegExp(`https://github.com/([^/\\s]+)/${repoName}/blob/([0-9a-f]{40})/([^#\\s]+)#L([0-9]+)(-L([0-9]+))?`, 'g'); return stringReplaceAsync(body, expression, async (match: string, owner: string, sha: string, file: string, start: string, _endGroup?: string, end?: string, index?: number) => { if (index && (index > 0) && (body.charAt(index - 1) === '(')) { return match; diff --git a/src/test/github/prComment.test.ts b/src/test/github/prComment.test.ts index 41786c8a82..b7c9fab4e9 100644 --- a/src/test/github/prComment.test.ts +++ b/src/test/github/prComment.test.ts @@ -4,7 +4,11 @@ *--------------------------------------------------------------------------------------------*/ import { default as assert } from 'assert'; -import { COMMIT_SHA_EXPRESSION, replaceImages } from '../../github/prComment'; +import * as vscode from 'vscode'; +import { COMMIT_SHA_EXPRESSION, GHPRComment, GHPRCommentThread, replaceImages } from '../../github/prComment'; +import { GitHubRepository } from '../../github/githubRepository'; +import { AccountType } from '../../github/interface'; +import { MockExtensionContext } from '../mocks/mockExtensionContext'; describe('commit SHA replacement', function () { it('should match 7-character commit SHAs', function () { @@ -103,3 +107,32 @@ test again assert.strictEqual(replaced, expected); }); }); + +describe('permalink replacement', function () { + it('expands each permalink when there are several on one line', async function () { + const sha = '5cf56bc1234567890abcdef1234567890abcdef0'; + const first = `https://github.com/owner/repo/blob/${sha}/src/a.ts#L1`; + const second = `https://github.com/owner/repo/blob/${sha}/src/b.ts#L2-L3`; + const githubRepository = { + remote: { host: 'github.com', owner: 'owner', repositoryName: 'repo' }, + rootUri: vscode.Uri.file('/nonexistent-permalink-root'), + getLines: async (_sha: string, file: string) => `contents of ${file}`, + } as unknown as GitHubRepository; + const document = await vscode.workspace.openTextDocument({ content: '' }); + const thread = { uri: document.uri, comments: [] } as unknown as GHPRCommentThread; + const comment = new GHPRComment(new MockExtensionContext(), { + id: 1, + graphNodeId: 'comment-1', + body: '', + createdAt: '', + htmlUrl: '', + url: 'https://github.com/owner/repo/pull/1#discussion_r1', + diffHunk: '', + user: { login: 'reviewer', id: '1', url: 'https://github.com/reviewer', accountType: AccountType.User }, + }, thread, [githubRepository]); + + const replaced: string = await (comment as any).replacePermalink(`Compare ${first} and ${second}`); + + assert.deepStrictEqual(replaced.match(/^\[.*$/gm), [`[src/a.ts](${first})`, `[src/b.ts](${second})`]); + }); +}); From 2d997916e5a63a49db9c6e1d14df10742c7a9f8b Mon Sep 17 00:00:00 2001 From: Federico Brancasi Date: Sun, 29 Nov 2026 22:56:54 +0100 Subject: [PATCH 2/3] Check the whole comment in the permalink replacement test Compare the full replaced body, so the test also fails when the text around the permalinks is lost or when two permalinks end up in one expansion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/test/github/prComment.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/github/prComment.test.ts b/src/test/github/prComment.test.ts index b7c9fab4e9..50ea357d25 100644 --- a/src/test/github/prComment.test.ts +++ b/src/test/github/prComment.test.ts @@ -133,6 +133,7 @@ describe('permalink replacement', function () { const replaced: string = await (comment as any).replacePermalink(`Compare ${first} and ${second}`); - assert.deepStrictEqual(replaced.match(/^\[.*$/gm), [`[src/a.ts](${first})`, `[src/b.ts](${second})`]); + const expanded = (file: string, permalink: string, lines: string) => `\n***\n[${file}](${permalink})\n\n${lines}\n\`\`\`\ncontents of ${file}\n\`\`\`\n***`; + assert.strictEqual(replaced, `Compare ${expanded('src/a.ts', first, 'Line 1 in `5cf56bc`')} and ${expanded('src/b.ts', second, 'Lines 2 to 3 in `5cf56bc`')}`); }); }); From 8729f113d77e04d7fd9b14e56e7c7a4a5c8a1da9 Mon Sep 17 00:00:00 2001 From: Federico Brancasi Date: Sun, 29 Nov 2026 23:18:36 +0100 Subject: [PATCH 3/3] Use the extension's URI in the permalink replacement test `GHPRComment` loads the emojis relative to the context's extension URI, and the mock context points to the test folder, which has no `resources/emojis.json`. Run on its own, the test left an unhandled rejection and a cached failed load for later tests. Set the real extension URI, as the issue overview tests do. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/test/github/prComment.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/github/prComment.test.ts b/src/test/github/prComment.test.ts index 50ea357d25..f9cb3616eb 100644 --- a/src/test/github/prComment.test.ts +++ b/src/test/github/prComment.test.ts @@ -5,6 +5,7 @@ import { default as assert } from 'assert'; import * as vscode from 'vscode'; +import { EXTENSION_ID } from '../../constants'; import { COMMIT_SHA_EXPRESSION, GHPRComment, GHPRCommentThread, replaceImages } from '../../github/prComment'; import { GitHubRepository } from '../../github/githubRepository'; import { AccountType } from '../../github/interface'; @@ -120,7 +121,10 @@ describe('permalink replacement', function () { } as unknown as GitHubRepository; const document = await vscode.workspace.openTextDocument({ content: '' }); const thread = { uri: document.uri, comments: [] } as unknown as GHPRCommentThread; - const comment = new GHPRComment(new MockExtensionContext(), { + const context = new MockExtensionContext(); + // Load the emojis from the extension, not from the test folder, so a failed load isn't cached for later tests + context.extensionUri = vscode.extensions.getExtension(EXTENSION_ID)!.extensionUri; + const comment = new GHPRComment(context, { id: 1, graphNodeId: 'comment-1', body: '',