forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
188 lines (162 loc) · 7.69 KB
/
Copy pathcommands.ts
File metadata and controls
188 lines (162 loc) · 7.69 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { ReviewManager } from './view/reviewManager';
import { PullRequestOverviewPanel } from './github/pullRequestOverview';
import { fromReviewUri, ReviewUriParams } from './common/uri';
import { GitFileChangeNode } from './view/treeNodes/fileChangeNode';
import { PRNode } from './view/treeNodes/pullRequestNode';
import { IPullRequestManager, IPullRequestModel, IPullRequest, ITelemetry } from './github/interface';
import { Comment } from './common/comment';
import { formatError } from './common/utils';
import { GitChangeType } from './common/file';
import { getDiffLineByPosition, getZeroBased } from './common/diffPositionMapping';
import { DiffChangeType } from './common/diffHunk';
import { DescriptionNode } from './view/treeNodes/descriptionNode';
const _onDidClosePR = new vscode.EventEmitter<IPullRequest>();
export const onDidClosePR: vscode.Event<IPullRequest> = _onDidClosePR.event;
function ensurePR(prManager: IPullRequestManager, pr?: PRNode | IPullRequestModel): IPullRequestModel {
// If the command is called from the command palette, no arguments are passed.
if (!pr) {
if (!prManager.activePullRequest) {
vscode.window.showErrorMessage('Unable to find current pull request.');
return;
}
return prManager.activePullRequest;
} else {
return pr instanceof PRNode ? pr.pullRequestModel : pr;
}
}
export function registerCommands(context: vscode.ExtensionContext, prManager: IPullRequestManager,
reviewManager: ReviewManager, telemetry: ITelemetry) {
// initialize resources
context.subscriptions.push(vscode.commands.registerCommand('pr.openPullRequestInGitHub', (e: PRNode | IPullRequestModel) => {
if (!e) {
if (prManager.activePullRequest) {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(prManager.activePullRequest.html_url));
}
} else if (e instanceof PRNode) {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(e.pullRequestModel.html_url));
} else {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(e.html_url));
}
telemetry.on('pr.openInGitHub');
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.openFileInGitHub', (e: GitFileChangeNode) => {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(e.blobUrl));
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.openDiffView', (parentFilePath: string, filePath: string, fileName: string, isPartial: boolean, opts: any) => {
if (isPartial) {
vscode.window.showInformationMessage('Your local repository is not up to date so only partial content is being displayed');
}
vscode.commands.executeCommand('vscode.diff', parentFilePath, filePath, fileName, opts);
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.deleteLocalBranch', async (e: PRNode) => {
const pullRequestModel = ensurePR(prManager, e);
try {
await prManager.deleteLocalPullRequest(pullRequestModel);
vscode.commands.executeCommand('pr.refreshList');
} catch (e) {
vscode.window.showErrorMessage(`Deleting local pull request branch failed: ${e}`);
}
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.pick', async (pr: PRNode | DescriptionNode | IPullRequestModel) => {
let pullRequestModel: IPullRequestModel;
if (pr instanceof PRNode || pr instanceof DescriptionNode) {
pullRequestModel = pr.pullRequestModel;
telemetry.on('pr.checkout.context');
} else {
pullRequestModel = pr;
telemetry.on('pr.checkout.description');
}
return vscode.window.withProgress({
location: vscode.ProgressLocation.SourceControl,
title: `Switching to Pull Request #${pullRequestModel.prNumber}`,
}, async (progress, token) => {
await reviewManager.switch(pullRequestModel);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.close', async (pr?: PRNode, message?: string) => {
const pullRequest = ensurePR(prManager, pr);
return vscode.window.showWarningMessage(`Are you sure you want to close this pull request on GitHub? This will close the pull request without merging.`, 'Yes', 'No').then(async value => {
if (value === 'Yes') {
try {
let newComment: Comment;
if (message) {
newComment = await prManager.createIssueComment(pullRequest, message);
}
let newPR = await prManager.closePullRequest(pullRequest);
vscode.commands.executeCommand('pr.refreshList');
_onDidClosePR.fire(newPR);
return newComment;
} catch (e) {
vscode.window.showErrorMessage(`Unable to close pull request. ${formatError(e)}`);
_onDidClosePR.fire(null);
}
}
_onDidClosePR.fire(null);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.approve', async (pr: IPullRequestModel, message?: string) => {
return await prManager.approvePullRequest(pr, message);
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.requestChanges', async (pr: IPullRequestModel, message?: string) => {
return await prManager.requestChanges(pr, message);
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.openDescription', async (pr: IPullRequestModel) => {
const pullRequest = ensurePR(prManager, pr);
// Create and show a new webview
PullRequestOverviewPanel.createOrShow(context.extensionPath, prManager, pullRequest);
telemetry.on('pr.openDescription');
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.viewChanges', async (fileChange: GitFileChangeNode) => {
if (fileChange.status === GitChangeType.DELETE) {
// create an empty `review` uri without any path/commit info.
const emptyFileUri = fileChange.parentFilePath.with({
query: JSON.stringify({
path: null,
commit: null,
})
});
return vscode.commands.executeCommand('vscode.diff', fileChange.parentFilePath, emptyFileUri, `${fileChange.fileName}`, { preserveFocus: true });
}
// Show the file change in a diff view.
let { path, ref, commit } = fromReviewUri(fileChange.filePath);
let previousCommit = `${commit}^`;
const query: ReviewUriParams = {
path: path,
ref: ref,
commit: previousCommit,
base: true,
isOutdated: true
};
const previousFileUri = fileChange.filePath.with({ query: JSON.stringify(query) });
const options: vscode.TextDocumentShowOptions = {
preserveFocus: true
};
if (fileChange.comments && fileChange.comments.length) {
const sortedOutdatedComments = fileChange.comments.filter(comment => comment.position === null).sort((a, b) => {
return a.original_position - b.original_position;
});
if (sortedOutdatedComments.length) {
const diffLine = getDiffLineByPosition(fileChange.diffHunks, sortedOutdatedComments[0].original_position);
if (diffLine) {
let lineNumber = Math.max(getZeroBased(diffLine.type === DiffChangeType.Delete ? diffLine.oldLineNumber : diffLine.newLineNumber), 0);
options.selection = new vscode.Range(lineNumber, 0, lineNumber, 0);
}
}
}
return vscode.commands.executeCommand('vscode.diff', previousFileUri, fileChange.filePath, `${fileChange.fileName} from ${commit.substr(0, 8)}`, options);
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.signin', async () => {
await prManager.authenticate();
}));
context.subscriptions.push(vscode.commands.registerCommand('pr.signinAndRefreshList', async () => {
if (await prManager.authenticate()) {
vscode.commands.executeCommand('pr.refreshList');
}
}));
}