3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { URLSearchParams } from 'url' ;
6
+ import { URL , URLSearchParams } from 'url' ;
7
7
import LRUCache from 'lru-cache' ;
8
8
import * as marked from 'marked' ;
9
9
import * as vscode from 'vscode' ;
@@ -15,7 +15,7 @@ import { GithubItemStateEnum, User } from '../github/interface';
15
15
import { IssueModel } from '../github/issueModel' ;
16
16
import { PullRequestModel } from '../github/pullRequestModel' ;
17
17
import { RepositoriesManager } from '../github/repositoriesManager' ;
18
- import { getRepositoryForFile } from '../github/utils' ;
18
+ import { getEnterpriseUri , getRepositoryForFile } from '../github/utils' ;
19
19
import { ReviewManager } from '../view/reviewManager' ;
20
20
import { CODE_PERMALINK , findCodeLinkLocally } from './issueLinkLookup' ;
21
21
import { StateManager } from './stateManager' ;
@@ -508,15 +508,37 @@ export async function createGithubPermalink(
508
508
}
509
509
}
510
510
const pathSegment = uri . path . substring ( repository . rootUri . path . length ) ;
511
-
511
+ const originOfFetchUrl = getUpstreamOrigin ( upstream ) . replace ( / \/ $ / , '' ) ;
512
512
return {
513
- permalink : `https://github.com /${ new Protocol ( upstream . fetchUrl ) . nameWithOwner } /blob/${ commitHash
513
+ permalink : `${ originOfFetchUrl } /${ new Protocol ( upstream . fetchUrl ) . nameWithOwner } /blob/${ commitHash
514
514
} ${ pathSegment } ${ rangeString ( range ) } `,
515
515
error : undefined ,
516
516
originalFile : uri
517
517
} ;
518
518
}
519
519
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
+
520
542
function rangeString ( range : vscode . Range | undefined ) {
521
543
if ( ! range ) {
522
544
return '' ;
0 commit comments