-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathpullRequestNode.ts
362 lines (307 loc) · 13 KB
/
pullRequestNode.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*---------------------------------------------------------------------------------------------
* 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 { Repository } from '../../api/api';
import { getCommentingRanges } from '../../common/commentingRanges';
import { InMemFileChange, SlimFileChange } from '../../common/file';
import Logger from '../../common/logger';
import { FILE_LIST_LAYOUT, PR_SETTINGS_NAMESPACE, SHOW_PULL_REQUEST_NUMBER_IN_TREE } from '../../common/settingKeys';
import { createPRNodeUri, DataUri, fromPRUri, Schemes } from '../../common/uri';
import { FolderRepositoryManager } from '../../github/folderRepositoryManager';
import { NotificationProvider } from '../../github/notifications';
import { IResolvedPullRequestModel, PullRequestModel } from '../../github/pullRequestModel';
import { InMemFileChangeModel, RemoteFileChangeModel } from '../fileChangeModel';
import { getInMemPRFileSystemProvider, provideDocumentContentForChangeModel } from '../inMemPRContentProvider';
import { DescriptionNode } from './descriptionNode';
import { DirectoryTreeNode } from './directoryTreeNode';
import { InMemFileChangeNode, RemoteFileChangeNode } from './fileChangeNode';
import { TreeNode, TreeNodeParent } from './treeNode';
export class PRNode extends TreeNode implements vscode.CommentingRangeProvider2 {
static ID = 'PRNode';
private _fileChanges: (RemoteFileChangeNode | InMemFileChangeNode)[] | undefined;
private _commentController?: vscode.CommentController;
private _inMemPRContentProvider?: vscode.Disposable;
private _command: vscode.Command;
public get command(): vscode.Command {
return this._command;
}
public set command(newCommand: vscode.Command) {
this._command = newCommand;
}
public get repository(): Repository {
return this._folderReposManager.repository;
}
constructor(
parent: TreeNodeParent,
private _folderReposManager: FolderRepositoryManager,
public pullRequestModel: PullRequestModel,
private _isLocal: boolean,
private _notificationProvider: NotificationProvider
) {
super(parent);
this.registerSinceReviewChange();
this.registerConfigurationChange();
this._register(this.pullRequestModel.onDidInvalidate(() => this.refresh(this)));
this._register(this._folderReposManager.onDidChangeActivePullRequest(e => {
if (e.new === this.pullRequestModel.number || e.old === this.pullRequestModel.number) {
this.refresh(this);
}
}));
}
// #region Tree
override async getChildren(): Promise<TreeNode[]> {
super.getChildren();
Logger.debug(`Fetch children of PRNode #${this.pullRequestModel.number}`, PRNode.ID);
try {
const descriptionNode = new DescriptionNode(
this,
vscode.l10n.t('Description'),
this.pullRequestModel,
this.repository,
this._folderReposManager,
this._isLocal
);
if (!this.pullRequestModel.isResolved()) {
return [descriptionNode];
}
[, this._fileChanges, ,] = await Promise.all([
this.pullRequestModel.initializePullRequestFileViewState(),
this.resolveFileChangeNodes(),
(!this._commentController) ? this.resolvePRCommentController() : new Promise<void>(resolve => resolve()),
this.pullRequestModel.validateDraftMode()
]);
if (!this._inMemPRContentProvider) {
this._inMemPRContentProvider = getInMemPRFileSystemProvider()?.registerTextDocumentContentProvider(
this.pullRequestModel.number,
this.provideDocumentContent.bind(this),
);
if (this._inMemPRContentProvider) {
this._register(this._inMemPRContentProvider);
}
}
const result: TreeNode[] = [descriptionNode];
const layout = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE).get<string>(FILE_LIST_LAYOUT);
if (layout === 'tree') {
// tree view
const dirNode = new DirectoryTreeNode(this, '');
this._fileChanges.forEach(f => dirNode.addFile(f));
dirNode.finalize();
if (dirNode.label === '') {
// nothing on the root changed, pull children to parent
result.push(...dirNode.children);
} else {
result.push(dirNode);
}
} else {
// flat view
result.push(...this._fileChanges);
}
if (this.pullRequestModel.showChangesSinceReview !== undefined) {
this.reopenNewPrDiffs(this.pullRequestModel);
}
this.children = result;
// Kick off review thread initialization but don't await it.
// Events will be fired later that will cause the tree to update when this is ready.
this.pullRequestModel.initializeReviewThreadCache();
return result;
} catch (e) {
Logger.error(`Error getting children ${e}: ${e.message}`, PRNode.ID);
return [];
}
}
protected registerSinceReviewChange() {
this._register(this.pullRequestModel.onDidChangeChangesSinceReview(_ => {
this.refresh(this);
}));
}
protected registerConfigurationChange() {
this._register(vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(`${PR_SETTINGS_NAMESPACE}.${SHOW_PULL_REQUEST_NUMBER_IN_TREE}`)) {
this.refresh();
}
}));
}
public async reopenNewPrDiffs(pullRequest: PullRequestModel) {
let hasOpenDiff: boolean = false;
vscode.window.tabGroups.all.map(tabGroup => {
tabGroup.tabs.map(tab => {
if (
tab.input instanceof vscode.TabInputTextDiff &&
tab.input.original.scheme === Schemes.Pr &&
tab.input.modified.scheme === Schemes.Pr &&
this._fileChanges
) {
for (const localChange of this._fileChanges) {
const originalParams = fromPRUri(tab.input.original);
const modifiedParams = fromPRUri(tab.input.modified);
const newLocalChangeParams = fromPRUri(localChange.changeModel.filePath);
if (
originalParams?.prNumber === pullRequest.number &&
modifiedParams?.prNumber === pullRequest.number &&
localChange.fileName === modifiedParams.fileName &&
newLocalChangeParams?.headCommit !== modifiedParams.headCommit
) {
hasOpenDiff = true;
vscode.window.tabGroups.close(tab).then(_ => localChange.openDiff(this._folderReposManager, { preview: tab.isPreview }));
break;
}
}
}
});
});
if (pullRequest.showChangesSinceReview && !hasOpenDiff && this._fileChanges && this._fileChanges.length && !pullRequest.isActive) {
this._fileChanges[0].openDiff(this._folderReposManager, { preview: true });
}
}
private async resolvePRCommentController(): Promise<void> {
// If the current branch is this PR's branch, then we can rely on the review comment controller instead.
if (this.pullRequestModel.equals(this._folderReposManager.activePullRequest)) {
return;
}
await this.pullRequestModel.githubRepository.ensureCommentsController();
this._commentController = this.pullRequestModel.githubRepository.commentsController!;
this._register(this.pullRequestModel.githubRepository.commentsHandler!.registerCommentingRangeProvider(
this.pullRequestModel.number,
this
));
this._register(this.pullRequestModel.githubRepository.commentsHandler!.registerCommentController(
this.pullRequestModel.number,
this.pullRequestModel,
this._folderReposManager,
));
this.registerListeners();
}
private registerListeners(): void {
this._register(this.pullRequestModel.onDidChangePendingReviewState(async newDraftMode => {
if (!newDraftMode) {
(await this.getFileChanges()).forEach(fileChange => {
if (fileChange instanceof InMemFileChangeNode) {
fileChange.comments.forEach(c => (c.isDraft = newDraftMode));
}
});
}
}));
}
public async getFileChanges(noCache: boolean | void): Promise<(RemoteFileChangeNode | InMemFileChangeNode)[]> {
if (!this._fileChanges || noCache) {
this._fileChanges = await this.resolveFileChangeNodes();
}
return this._fileChanges;
}
private async resolveFileChangeNodes(): Promise<(RemoteFileChangeNode | InMemFileChangeNode)[]> {
if (!this.pullRequestModel.isResolved()) {
return [];
}
// If this PR is the the current PR, then we should be careful to use
// URIs that will cause the review comment controller to be used.
const rawChanges: (SlimFileChange | InMemFileChange)[] = [];
const isCurrentPR = this.pullRequestModel.equals(this._folderReposManager.activePullRequest);
if (isCurrentPR && (this._folderReposManager.activePullRequest !== undefined) && (this._folderReposManager.activePullRequest.fileChanges.size > 0)) {
this.pullRequestModel = this._folderReposManager.activePullRequest;
rawChanges.push(...this._folderReposManager.activePullRequest.fileChanges.values());
} else {
rawChanges.push(...await this.pullRequestModel.getFileChangesInfo());
}
// Merge base is set as part of getFileChangesInfo
const mergeBase = this.pullRequestModel.mergeBase;
if (!mergeBase) {
return [];
}
return rawChanges.map(change => {
if (change instanceof SlimFileChange) {
const changeModel = new RemoteFileChangeModel(this._folderReposManager, change, this.pullRequestModel);
return new RemoteFileChangeNode(
this,
this._folderReposManager,
this.pullRequestModel as (PullRequestModel & IResolvedPullRequestModel),
changeModel
);
}
const changeModel = new InMemFileChangeModel(this._folderReposManager,
this.pullRequestModel as (PullRequestModel & IResolvedPullRequestModel),
change, isCurrentPR, mergeBase);
const changedItem = new InMemFileChangeNode(
this._folderReposManager,
this,
this.pullRequestModel as (PullRequestModel & IResolvedPullRequestModel),
changeModel
);
return changedItem;
});
}
async getTreeItem(): Promise<vscode.TreeItem> {
const currentBranchIsForThisPR = this.pullRequestModel.equals(this._folderReposManager.activePullRequest);
const { title, number, author, isDraft, html_url } = this.pullRequestModel;
const labelTitle = this.pullRequestModel.title.length > 50 ? `${this.pullRequestModel.title.substring(0, 50)}...` : this.pullRequestModel.title;
const { login } = author;
const hasNotification = this._notificationProvider.hasNotification(this.pullRequestModel);
const formattedPRNumber = number.toString();
let labelPrefix = currentBranchIsForThisPR ? '✓ ' : '';
let tooltipPrefix = currentBranchIsForThisPR ? 'Current Branch * ' : '';
if (
vscode.workspace
.getConfiguration(PR_SETTINGS_NAMESPACE)
.get<boolean>(SHOW_PULL_REQUEST_NUMBER_IN_TREE, false)
) {
labelPrefix += `#${formattedPRNumber}: `;
tooltipPrefix += `#${formattedPRNumber}: `;
}
const label = `${labelPrefix}${isDraft ? '[DRAFT] ' : ''}${labelTitle}`;
const tooltip = `${tooltipPrefix}${title} by @${login}`;
const description = `by @${login}`;
return {
label,
id: `${this.parent instanceof TreeNode ? (this.parent.id ?? this.parent.label) : ''}${html_url}${this._isLocal ? this.pullRequestModel.localBranchName : ''}`, // unique id stable across checkout status
tooltip,
description,
collapsibleState: 1,
contextValue:
'pullrequest' +
(this._isLocal ? ':local' : '') +
(currentBranchIsForThisPR ? ':active' : ':nonactive') +
(hasNotification ? ':notification' : '') +
(((this.pullRequestModel.item.isRemoteHeadDeleted && !this._isLocal) || !this._folderReposManager.isPullRequestAssociatedWithOpenRepository(this.pullRequestModel)) ? '' : ':hasHeadRef'),
iconPath: (await DataUri.avatarCirclesAsImageDataUris(this._folderReposManager.context, [this.pullRequestModel.author], 16, 16))[0]
?? new vscode.ThemeIcon('github'),
accessibilityInformation: {
label: `${isDraft ? 'Draft ' : ''}Pull request number ${formattedPRNumber}: ${title} by ${login}`
},
resourceUri: createPRNodeUri(this.pullRequestModel),
};
}
async provideCommentingRanges(document: vscode.TextDocument, _token: vscode.CancellationToken): Promise<vscode.Range[] | { enableFileComments: boolean; ranges?: vscode.Range[] } | undefined> {
if (document.uri.scheme === Schemes.Pr) {
const params = fromPRUri(document.uri);
if (!params || params.prNumber !== this.pullRequestModel.number) {
return undefined;
}
const fileChange = (await this.getFileChanges()).find(change => change.changeModel.fileName === params.fileName);
if (!fileChange || fileChange instanceof RemoteFileChangeNode) {
return undefined;
}
return { ranges: getCommentingRanges(await fileChange.changeModel.diffHunks(), params.isBase, PRNode.ID), enableFileComments: true };
}
return undefined;
}
// #region Document Content Provider
private async provideDocumentContent(uri: vscode.Uri): Promise<string | Uint8Array> {
const params = fromPRUri(uri);
if (!params) {
return '';
}
const fileChange = (await this.getFileChanges()).find(
contentChange => contentChange.changeModel.fileName === params.fileName,
)?.changeModel;
if (!fileChange) {
Logger.appendLine(`Can not find content for document ${uri.toString()}`, 'PR');
return '';
}
return provideDocumentContentForChangeModel(this._folderReposManager, this.pullRequestModel, params, fileChange);
}
override dispose(): void {
super.dispose();
this._commentController = undefined;
}
}