forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatePRLinkProvider.ts
More file actions
91 lines (79 loc) · 2.94 KB
/
createPRLinkProvider.ts
File metadata and controls
91 lines (79 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { ReviewManager } from '../view/reviewManager';
import { FolderRepositoryManager } from './folderRepositoryManager';
interface GitHubCreateTerminalLink extends vscode.TerminalLink {
url: string;
}
export class GitHubCreatePullRequestLinkProvider implements vscode.TerminalLinkProvider {
constructor(
private readonly reviewManager: ReviewManager,
private readonly folderRepositoryManager: FolderRepositoryManager,
) {}
provideTerminalLinks(
context: vscode.TerminalLinkContext,
_token: vscode.CancellationToken,
): vscode.ProviderResult<GitHubCreateTerminalLink[]> {
const startIndex = context.line.indexOf('https://github.com');
if (startIndex === -1) {
return [];
}
/**
* When a branch is published, a line like the following is written to the terminal:
* remote: https://github.com/RMacfarlane/pullrequest-demo/pull/new/rmacfarlane/testbranch3
*/
const url = context.line.substring(startIndex);
const regex = new RegExp(/https:\/\/github\.com\/(.*)\/(.*)\/pull\/new\/(.*)/);
const result = url.match(regex);
if (result && result.length === 4) {
const owner = result[1];
const repositoryName = result[2];
const branchName = result[3];
const hasMatchingGitHubRepo =
this.folderRepositoryManager.gitHubRepositories.findIndex(
repo => repo.remote.owner === owner && repo.remote.repositoryName === repositoryName,
) > -1;
// The create flow compares against the current branch, so check that the published branch is this branch
if (hasMatchingGitHubRepo && this.reviewManager.repository.state.HEAD?.name === branchName) {
return [
{
startIndex,
length: context.line.length - startIndex,
tooltip: 'Create a Pull Request',
url,
},
];
}
}
return [];
}
handleTerminalLink(link: GitHubCreateTerminalLink): vscode.ProviderResult<void> {
const defaultHandler = vscode.workspace
.getConfiguration('githubPullRequests')
.get<'vscode' | 'github' | undefined>('terminalLinksHandler');
if (defaultHandler === 'github') {
vscode.env.openExternal(vscode.Uri.parse(link.url));
return;
}
if (defaultHandler === 'vscode') {
this.reviewManager.createPullRequest();
return;
}
vscode.window
.showInformationMessage(
'Do you want to create a pull request using the GitHub Pull Requests and Issues extension?',
'Yes',
'No, continue to github.com',
)
.then(notificationResult => {
if (notificationResult === 'Yes') {
this.reviewManager.createPullRequest();
} else {
vscode.env.openExternal(vscode.Uri.parse(link.url));
}
});
}
}