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..f9cb3616eb 100644 --- a/src/test/github/prComment.test.ts +++ b/src/test/github/prComment.test.ts @@ -4,7 +4,12 @@ *--------------------------------------------------------------------------------------------*/ import { default as assert } from 'assert'; -import { COMMIT_SHA_EXPRESSION, replaceImages } from '../../github/prComment'; +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'; +import { MockExtensionContext } from '../mocks/mockExtensionContext'; describe('commit SHA replacement', function () { it('should match 7-character commit SHAs', function () { @@ -103,3 +108,36 @@ 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 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: '', + 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}`); + + 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`')}`); + }); +});