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
119 lines (103 loc) · 3.78 KB
/
createPRLinkProvider.ts
File metadata and controls
119 lines (103 loc) · 3.78 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*---------------------------------------------------------------------------------------------
* 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 { PR_SETTINGS_NAMESPACE, TERMINAL_LINK_HANDLER } from '../common/settingKeys';
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,
) { }
private static getSettingsValue() {
return vscode.workspace
.getConfiguration(PR_SETTINGS_NAMESPACE)
.get<'vscode' | 'github' | undefined>(TERMINAL_LINK_HANDLER);
}
static registerProvider(disposables: vscode.Disposable[], reviewManager: ReviewManager, folderManager: FolderRepositoryManager) {
disposables.push(
vscode.window.registerTerminalLinkProvider(
new GitHubCreatePullRequestLinkProvider(reviewManager, folderManager),
)
);
}
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: vscode.l10n.t('Create a Pull Request'),
url,
},
];
}
}
return [];
}
private openLink(link: GitHubCreateTerminalLink) {
return vscode.env.openExternal(vscode.Uri.parse(link.url));
}
handleTerminalLink(link: GitHubCreateTerminalLink): vscode.ProviderResult<void> {
const defaultHandler = GitHubCreatePullRequestLinkProvider.getSettingsValue();
if (defaultHandler === 'github') {
this.openLink(link);
return;
}
if (defaultHandler === 'vscode') {
this.reviewManager.createPullRequest();
return;
}
const yes = 'Yes';
const neverShow = 'Don\'t Show Again';
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',
neverShow
)
.then(notificationResult => {
switch (notificationResult) {
case yes: {
this.reviewManager.createPullRequest();
break;
}
case neverShow: {
vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE).update(TERMINAL_LINK_HANDLER, 'github', vscode.ConfigurationTarget.Global);
this.openLink(link);
break;
}
default: this.openLink(link);
}
});
}
}