Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/github/prComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 39 additions & 1 deletion src/test/github/prComment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
Comment on lines +117 to +121
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`')}`);
});
});
Loading