forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprsTreeModel.ts
More file actions
248 lines (214 loc) · 9.33 KB
/
Copy pathprsTreeModel.ts
File metadata and controls
248 lines (214 loc) · 9.33 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
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
/*---------------------------------------------------------------------------------------------
* 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 { Disposable, disposeAll } from '../common/lifecycle';
import { getReviewMode } from '../common/settingsUtils';
import { ITelemetry } from '../common/telemetry';
import { createPRNodeIdentifier } from '../common/uri';
import { FolderRepositoryManager, ItemsResponseResult } from '../github/folderRepositoryManager';
import { CheckState, PRType, PullRequestChecks, PullRequestReviewRequirement } from '../github/interface';
import { PullRequestModel } from '../github/pullRequestModel';
import { RepositoriesManager } from '../github/repositoriesManager';
import { UnsatisfiedChecks } from '../github/utils';
import { CategoryTreeNode } from './treeNodes/categoryNode';
import { TreeNode } from './treeNodes/treeNode';
export const EXPANDED_QUERIES_STATE = 'expandedQueries';
interface PRStatusChange {
pullRequest: PullRequestModel;
status: UnsatisfiedChecks;
}
export class PrsTreeModel extends Disposable {
private _activePRDisposables: Map<FolderRepositoryManager, vscode.Disposable[]> = new Map();
private readonly _onDidChangePrStatus: vscode.EventEmitter<string[]> = this._register(new vscode.EventEmitter<string[]>());
public readonly onDidChangePrStatus = this._onDidChangePrStatus.event;
private readonly _onDidChangeData: vscode.EventEmitter<FolderRepositoryManager | void> = this._register(new vscode.EventEmitter<FolderRepositoryManager | void>());
public readonly onDidChangeData = this._onDidChangeData.event;
private _expandedQueries: Set<string> = new Set();
private _hasLoaded: boolean = false;
private _onLoaded: vscode.EventEmitter<void> = this._register(new vscode.EventEmitter<void>());
public readonly onLoaded = this._onLoaded.event;
// Key is identifier from createPRNodeUri
private readonly _queriedPullRequests: Map<string, PRStatusChange> = new Map();
private _cachedPRs: Map<FolderRepositoryManager, Map<string | PRType.LocalPullRequest | PRType.All, ItemsResponseResult<PullRequestModel>>> = new Map();
constructor(private _telemetry: ITelemetry, private readonly _reposManager: RepositoriesManager, private readonly _context: vscode.ExtensionContext) {
super();
const repoEvents = (manager: FolderRepositoryManager) => {
this._register(manager.onDidChangeActivePullRequest(() => {
this.clearRepo(manager);
if (this._activePRDisposables.has(manager)) {
disposeAll(this._activePRDisposables.get(manager)!);
this._activePRDisposables.delete(manager);
}
if (manager.activePullRequest) {
this._activePRDisposables.set(manager, [
manager.activePullRequest.onDidChangeComments(() => {
this.clearRepo(manager);
})]);
}
}));
};
for (const manager of this._reposManager.folderManagers) {
repoEvents(manager);
}
this._register(this._reposManager.onDidChangeFolderRepositories((changed) => {
if (changed.added) {
repoEvents(changed.added);
this._onDidChangeData.fire(changed.added);
}
}));
this._expandedQueries = new Set(this._context.workspaceState.get(EXPANDED_QUERIES_STATE, [] as string[]));
}
public updateExpandedQueries(element: TreeNode, isExpanded: boolean) {
if ((element instanceof CategoryTreeNode) && element.id) {
if (isExpanded) {
this._expandedQueries.add(element.id);
} else {
this._expandedQueries.delete(element.id);
}
this._context.workspaceState.update(EXPANDED_QUERIES_STATE, Array.from(this._expandedQueries.keys()));
}
}
get expandedQueries(): Set<string> {
if (this._reposManager.folderManagers.length > 3 && this._expandedQueries.size > 0) {
return new Set();
}
return this._expandedQueries;
}
get hasLoaded(): boolean {
return this._hasLoaded;
}
private set hasLoaded(value: boolean) {
this._hasLoaded = value;
this._onLoaded.fire();
}
public cachedPRStatus(identifier: string): PRStatusChange | undefined {
return this._queriedPullRequests.get(identifier);
}
public clearCache() {
this._cachedPRs.clear();
this._onDidChangeData.fire();
}
public clearRepo(folderRepoManager: FolderRepositoryManager) {
this._cachedPRs.delete(folderRepoManager);
this._onDidChangeData.fire(folderRepoManager);
}
private async _getChecks(pullRequests: PullRequestModel[]) {
// If there are too many pull requests then we could hit our internal rate limit
// or even GitHub's secondary rate limit. If there are more than 100 PRs,
// chunk them into 100s.
let checks: [PullRequestChecks | null, PullRequestReviewRequirement | null][] = [];
for (let i = 0; i < pullRequests.length; i += 100) {
const sliceEnd = (i + 100 < pullRequests.length) ? i + 100 : pullRequests.length;
checks.push(...await Promise.all(pullRequests.slice(i, sliceEnd).map(pullRequest => {
return pullRequest.getStatusChecks();
})));
}
const changedStatuses: string[] = [];
for (let i = 0; i < pullRequests.length; i++) {
const pullRequest = pullRequests[i];
const [check, reviewRequirement] = checks[i];
let newStatus: UnsatisfiedChecks = UnsatisfiedChecks.None;
if (reviewRequirement) {
if (reviewRequirement.state === CheckState.Failure) {
newStatus |= UnsatisfiedChecks.ReviewRequired;
} else if (reviewRequirement.state == CheckState.Pending) {
newStatus |= UnsatisfiedChecks.ChangesRequested;
}
}
if (!check || check.state === CheckState.Unknown) {
continue;
}
if (check.state !== CheckState.Success) {
for (const status of check.statuses) {
if (status.state === CheckState.Failure) {
newStatus |= UnsatisfiedChecks.CIFailed;
} else if (status.state === CheckState.Pending) {
newStatus |= UnsatisfiedChecks.CIPending;
}
}
if (newStatus === UnsatisfiedChecks.None) {
newStatus |= UnsatisfiedChecks.CIPending;
}
}
const identifier = createPRNodeIdentifier(pullRequest);
const oldState = this._queriedPullRequests.get(identifier);
if ((oldState === undefined) || (oldState.status !== newStatus)) {
const newState = { pullRequest, status: newStatus };
changedStatuses.push(identifier);
this._queriedPullRequests.set(identifier, newState);
}
}
this._onDidChangePrStatus.fire(changedStatuses);
}
private getFolderCache(folderRepoManager: FolderRepositoryManager): Map<string | PRType.LocalPullRequest | PRType.All, ItemsResponseResult<PullRequestModel>> {
let cache = this._cachedPRs.get(folderRepoManager);
if (!cache) {
cache = new Map();
this._cachedPRs.set(folderRepoManager, cache);
}
return cache;
}
async getLocalPullRequests(folderRepoManager: FolderRepositoryManager, update?: boolean) {
const cache = this.getFolderCache(folderRepoManager);
if (!update && cache.has(PRType.LocalPullRequest)) {
return cache.get(PRType.LocalPullRequest)!;
}
const useReviewConfiguration = getReviewMode();
const prs = (await folderRepoManager.getLocalPullRequests())
.filter(pr => pr.isOpen || (pr.isClosed && useReviewConfiguration.closed) || (pr.isMerged && useReviewConfiguration.merged));
cache.set(PRType.LocalPullRequest, { hasMorePages: false, hasUnsearchedRepositories: false, items: prs, totalCount: prs.length });
/* __GDPR__
"pr.expand.local" : {}
*/
this._telemetry.sendTelemetryEvent('pr.expand.local');
// Don't await this._getChecks. It fires an event that will be listened to.
this._getChecks(prs);
this.hasLoaded = true;
return { hasMorePages: false, hasUnsearchedRepositories: false, items: prs };
}
async getPullRequestsForQuery(folderRepoManager: FolderRepositoryManager, fetchNextPage: boolean, query: string): Promise<ItemsResponseResult<PullRequestModel>> {
const cache = this.getFolderCache(folderRepoManager);
if (!fetchNextPage && cache.has(query)) {
return cache.get(query)!;
}
const prs = await folderRepoManager.getPullRequests(
PRType.Query,
{ fetchNextPage },
query,
);
cache.set(query, prs);
/* __GDPR__
"pr.expand.query" : {}
*/
this._telemetry.sendTelemetryEvent('pr.expand.query');
// Don't await this._getChecks. It fires an event that will be listened to.
this._getChecks(prs.items);
this.hasLoaded = true;
return prs;
}
async getAllPullRequests(folderRepoManager: FolderRepositoryManager, fetchNextPage: boolean, update?: boolean): Promise<ItemsResponseResult<PullRequestModel>> {
const cache = this.getFolderCache(folderRepoManager);
if (!update && cache.has(PRType.All) && !fetchNextPage) {
return cache.get(PRType.All)!;
}
const prs = await folderRepoManager.getPullRequests(
PRType.All,
{ fetchNextPage }
);
cache.set(PRType.All, prs);
/* __GDPR__
"pr.expand.all" : {}
*/
this._telemetry.sendTelemetryEvent('pr.expand.all');
// Don't await this._getChecks. It fires an event that will be listened to.
this._getChecks(prs.items);
this.hasLoaded = true;
return prs;
}
override dispose() {
super.dispose();
disposeAll(Array.from(this._activePRDisposables.values()).flat());
}
}