Skip to content

Commit a33a46a

Browse files
authored
Support the copied permalink for Github Enterprise URL (#3460)
1 parent bfbecf7 commit a33a46a

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/issues/util.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { URLSearchParams } from 'url';
6+
import { URL, URLSearchParams } from 'url';
77
import LRUCache from 'lru-cache';
88
import * as marked from 'marked';
99
import * as vscode from 'vscode';
@@ -15,7 +15,7 @@ import { GithubItemStateEnum, User } from '../github/interface';
1515
import { IssueModel } from '../github/issueModel';
1616
import { PullRequestModel } from '../github/pullRequestModel';
1717
import { RepositoriesManager } from '../github/repositoriesManager';
18-
import { getRepositoryForFile } from '../github/utils';
18+
import { getEnterpriseUri, getRepositoryForFile } from '../github/utils';
1919
import { ReviewManager } from '../view/reviewManager';
2020
import { CODE_PERMALINK, findCodeLinkLocally } from './issueLinkLookup';
2121
import { StateManager } from './stateManager';
@@ -508,15 +508,37 @@ export async function createGithubPermalink(
508508
}
509509
}
510510
const pathSegment = uri.path.substring(repository.rootUri.path.length);
511-
511+
const originOfFetchUrl = getUpstreamOrigin(upstream).replace(/\/$/, '');
512512
return {
513-
permalink: `https://github.com/${new Protocol(upstream.fetchUrl).nameWithOwner}/blob/${commitHash
513+
permalink: `${originOfFetchUrl}/${new Protocol(upstream.fetchUrl).nameWithOwner}/blob/${commitHash
514514
}${pathSegment}${rangeString(range)}`,
515515
error: undefined,
516516
originalFile: uri
517517
};
518518
}
519519

520+
function getUpstreamOrigin(upstream: Remote) {
521+
let resultHost: string = 'github.com';
522+
const enterpriseUri = getEnterpriseUri();
523+
if (enterpriseUri && upstream.fetchUrl) {
524+
// upstream's origin by https
525+
if (upstream.fetchUrl.startsWith('https://') && !upstream.fetchUrl.startsWith('https://github.com/')) {
526+
const host = new URL(upstream.fetchUrl).host;
527+
if (host === enterpriseUri.authority) {
528+
resultHost = host;
529+
}
530+
}
531+
// upstream's origin by ssh
532+
if (upstream.fetchUrl.startsWith('git@') && !upstream.fetchUrl.startsWith('[email protected]')) {
533+
const host = upstream.fetchUrl.split('@')[1]?.split(':')[0];
534+
if (host === enterpriseUri.authority) {
535+
resultHost = host;
536+
}
537+
}
538+
}
539+
return `https://${resultHost}`;
540+
}
541+
520542
function rangeString(range: vscode.Range | undefined) {
521543
if (!range) {
522544
return '';

0 commit comments

Comments
 (0)