Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/issue/electron-browser/issueReporterService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import { $, reset } from '../../../../base/browser/dom.js';
6
import { VSBuffer } from '../../../../base/common/buffer.js';
7
import { CancellationError } from '../../../../base/common/errors.js';
8
import { Schemas } from '../../../../base/common/network.js';
9
import { IProductConfiguration } from '../../../../base/common/product.js';
10
import { joinPath } from '../../../../base/common/resources.js';
11
import { URI } from '../../../../base/common/uri.js';
12
import { localize } from '../../../../nls.js';
13
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
14
import { isRemoteDiagnosticError } from '../../../../platform/diagnostics/common/diagnostics.js';
15
import { IFileDialogService } from '../../../../platform/dialogs/common/dialogs.js';
16
import { IFileService } from '../../../../platform/files/common/files.js';
17
import { INativeHostService } from '../../../../platform/native/common/native.js';
18
import { IOpenerService } from '../../../../platform/opener/common/opener.js';
19
import { IProcessService } from '../../../../platform/process/common/process.js';
20
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
21
import { IUpdateService, StateType } from '../../../../platform/update/common/update.js';
22
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
23
import { applyZoom } from '../../../../platform/window/electron-browser/window.js';
24
import { IAuthenticationService } from '../../../services/authentication/common/authentication.js';
25
import { BaseIssueReporterService } from '../browser/baseIssueReporterService.js';
26
import { IssueReporterData as IssueReporterModelData } from '../browser/issueReporterModel.js';
27
import { IIssueFormService, IssueReporterData, IssueType } from '../common/issue.js';
28
29
// GitHub has let us know that we could up our limit here to 8k. We chose 7500 to play it safe.
30
// ref https://github.com/microsoft/vscode/issues/159191
31
const MAX_URL_LENGTH = 7500;
32
33
// Github API and issues on web has a limit of 65536. We chose 65500 to play it safe.
34
// ref https://github.com/github/issues/issues/12858
35
const MAX_GITHUB_API_LENGTH = 65500;
36
37
38
export class IssueReporter extends BaseIssueReporterService {
39
private readonly processService: IProcessService;
40
constructor(
41
disableExtensions: boolean,
42
data: IssueReporterData,
43
os: {
44
type: string;
45
arch: string;
46
release: string;
47
},
48
product: IProductConfiguration,
49
window: Window,
50
@INativeHostService private readonly nativeHostService: INativeHostService,
51
@IIssueFormService issueFormService: IIssueFormService,
52
@IProcessService processService: IProcessService,
53
@IThemeService themeService: IThemeService,
54
@IFileService fileService: IFileService,
55
@IFileDialogService fileDialogService: IFileDialogService,
56
@IUpdateService private readonly updateService: IUpdateService,
57
@IContextKeyService contextKeyService: IContextKeyService,
58
@IContextMenuService contextMenuService: IContextMenuService,
59
@IAuthenticationService authenticationService: IAuthenticationService,
60
@IOpenerService openerService: IOpenerService
61
) {
62
super(disableExtensions, data, os, product, window, false, issueFormService, themeService, fileService, fileDialogService, contextMenuService, authenticationService, openerService);
63
this.processService = processService;
64
this.processService.getSystemInfo().then(info => {
65
this.issueReporterModel.update({ systemInfo: info });
66
this.receivedSystemInfo = true;
67
68
this.updateSystemInfo(this.issueReporterModel.getData());
69
this.updateButtonStates();
70
});
71
if (this.data.issueType === IssueType.PerformanceIssue) {
72
this.processService.getPerformanceInfo().then(info => {
73
this.updatePerformanceInfo(info as Partial<IssueReporterData>);
74
});
75
}
76
77
this.checkForUpdates();
78
this.setEventHandlers();
79
applyZoom(this.data.zoomLevel, this.window);
80
this.updateExperimentsInfo(this.data.experiments);
81
this.updateRestrictedMode(this.data.restrictedMode);
82
this.updateUnsupportedMode(this.data.isUnsupported);
83
}
84
85
private async checkForUpdates(): Promise<void> {
86
const updateState = this.updateService.state;
87
if (updateState.type === StateType.Ready || updateState.type === StateType.Downloaded) {
88
this.needsUpdate = true;
89
const includeAcknowledgement = this.getElementById('version-acknowledgements');
90
const updateBanner = this.getElementById('update-banner');
91
if (updateBanner && includeAcknowledgement) {
92
includeAcknowledgement.classList.remove('hidden');
93
updateBanner.classList.remove('hidden');
94
updateBanner.textContent = localize('updateAvailable', "A new version of {0} is available.", this.product.nameLong);
95
}
96
}
97
}
98
99
public override setEventHandlers(): void {
100
super.setEventHandlers();
101
102
this.addEventListener('issue-type', 'change', (event: Event) => {
103
const issueType = parseInt((<HTMLInputElement>event.target).value);
104
this.issueReporterModel.update({ issueType: issueType });
105
if (issueType === IssueType.PerformanceIssue && !this.receivedPerformanceInfo) {
106
this.processService.getPerformanceInfo().then(info => {
107
this.updatePerformanceInfo(info as Partial<IssueReporterData>);
108
});
109
}
110
111
// Resets placeholder
112
const descriptionTextArea = <HTMLInputElement>this.getElementById('issue-title');
113
if (descriptionTextArea) {
114
descriptionTextArea.placeholder = localize('undefinedPlaceholder', "Please enter a title");
115
}
116
117
this.updateButtonStates();
118
this.setSourceOptions();
119
this.render();
120
});
121
}
122
123
public override async submitToGitHub(issueTitle: string, issueBody: string, gitHubDetails: { owner: string; repositoryName: string }): Promise<boolean> {
124
if (issueBody.length > MAX_GITHUB_API_LENGTH) {
125
const extensionData = this.issueReporterModel.getData().extensionData;
126
if (extensionData) {
127
issueBody = issueBody.replace(extensionData, '');
128
const date = new Date();
129
const formattedDate = date.toISOString().split('T')[0]; // YYYY-MM-DD
130
const formattedTime = date.toTimeString().split(' ')[0].replace(/:/g, '-'); // HH-MM-SS
131
const fileName = `extensionData_${formattedDate}_${formattedTime}.md`;
132
try {
133
const downloadPath = await this.fileDialogService.showSaveDialog({
134
title: localize('saveExtensionData', "Save Extension Data"),
135
availableFileSystems: [Schemas.file],
136
defaultUri: joinPath(await this.fileDialogService.defaultFilePath(Schemas.file), fileName),
137
});
138
139
if (downloadPath) {
140
await this.fileService.writeFile(downloadPath, VSBuffer.fromString(extensionData));
141
}
142
} catch (e) {
143
console.error('Writing extension data to file failed');
144
return false;
145
}
146
} else {
147
console.error('Issue body too large to submit to GitHub');
148
return false;
149
}
150
}
151
const url = `https://api.github.com/repos/${gitHubDetails.owner}/${gitHubDetails.repositoryName}/issues`;
152
const init = {
153
method: 'POST',
154
body: JSON.stringify({
155
title: issueTitle,
156
body: issueBody
157
}),
158
headers: new Headers({
159
'Content-Type': 'application/json',
160
'Authorization': `Bearer ${this.data.githubAccessToken}`
161
})
162
};
163
164
const response = await fetch(url, init);
165
if (!response.ok) {
166
console.error('Invalid GitHub URL provided.');
167
return false;
168
}
169
const result = await response.json();
170
await this.openerService.open(result.html_url, { openExternal: true });
171
this.close();
172
return true;
173
}
174
175
public override async createIssue(shouldCreate?: boolean, privateUri?: boolean): Promise<boolean> {
176
const selectedExtension = this.issueReporterModel.getData().selectedExtension;
177
// Short circuit if the extension provides a custom issue handler
178
if (this.nonGitHubIssueUrl) {
179
const url = this.getExtensionBugsUrl();
180
if (url) {
181
this.hasBeenSubmitted = true;
182
await this.openerService.open(url, { openExternal: true });
183
return true;
184
}
185
}
186
187
if (!this.validateInputs()) {
188
// If inputs are invalid, set focus to the first one and add listeners on them
189
// to detect further changes
190
const invalidInput = this.window.document.getElementsByClassName('invalid-input');
191
if (invalidInput.length) {
192
(<HTMLInputElement>invalidInput[0]).focus();
193
}
194
195
this.addEventListener('issue-title', 'input', _ => {
196
this.validateInput('issue-title');
197
});
198
199
this.addEventListener('description', 'input', _ => {
200
this.validateInput('description');
201
});
202
203
this.addEventListener('issue-source', 'change', _ => {
204
this.validateInput('issue-source');
205
});
206
207
if (this.issueReporterModel.fileOnExtension()) {
208
this.addEventListener('extension-selector', 'change', _ => {
209
this.validateInput('extension-selector');
210
this.validateInput('description');
211
});
212
}
213
214
return false;
215
}
216
217
this.hasBeenSubmitted = true;
218
219
const issueTitle = (<HTMLInputElement>this.getElementById('issue-title')).value;
220
const issueBody = this.issueReporterModel.serialize();
221
222
let issueUrl = privateUri ? this.getPrivateIssueUrl() : this.getIssueUrl();
223
if (!issueUrl && selectedExtension?.uri) {
224
const uri = URI.revive(selectedExtension.uri);
225
issueUrl = uri.toString();
226
} else if (!issueUrl) {
227
console.error(`No ${privateUri ? 'private ' : ''}issue url found`);
228
return false;
229
}
230
231
const gitHubDetails = this.parseGitHubUrl(issueUrl);
232
233
const baseUrl = this.getIssueUrlWithTitle((<HTMLInputElement>this.getElementById('issue-title')).value, issueUrl);
234
let url = baseUrl + `&body=${encodeURIComponent(issueBody)}`;
235
236
url = this.addTemplateToUrl(url, gitHubDetails?.owner, gitHubDetails?.repositoryName);
237
238
if (this.data.githubAccessToken && gitHubDetails && shouldCreate) {
239
if (await this.submitToGitHub(issueTitle, issueBody, gitHubDetails)) {
240
return true;
241
}
242
}
243
244
try {
245
if (url.length > MAX_URL_LENGTH || issueBody.length > MAX_GITHUB_API_LENGTH) {
246
url = await this.writeToClipboard(baseUrl, issueBody);
247
url = this.addTemplateToUrl(url, gitHubDetails?.owner, gitHubDetails?.repositoryName);
248
}
249
} catch (_) {
250
console.error('Writing to clipboard failed');
251
return false;
252
}
253
254
await this.openerService.open(url, { openExternal: true });
255
return true;
256
}
257
258
public override async writeToClipboard(baseUrl: string, issueBody: string): Promise<string> {
259
const shouldWrite = await this.issueFormService.showClipboardDialog();
260
if (!shouldWrite) {
261
throw new CancellationError();
262
}
263
264
await this.nativeHostService.writeClipboardText(issueBody);
265
266
return baseUrl + `&body=${encodeURIComponent(localize('pasteData', "We have written the needed data into your clipboard because it was too large to send. Please paste."))}`;
267
}
268
269
private updateSystemInfo(state: IssueReporterModelData) {
270
const target = this.window.document.querySelector<HTMLElement>('.block-system .block-info');
271
272
if (target) {
273
const systemInfo = state.systemInfo!;
274
const renderedDataTable = $('table', undefined,
275
$('tr', undefined,
276
$('td', undefined, 'CPUs'),
277
$('td', undefined, systemInfo.cpus || '')
278
),
279
$('tr', undefined,
280
$('td', undefined, 'GPU Status' as string),
281
$('td', undefined, Object.keys(systemInfo.gpuStatus).map(key => `${key}: ${systemInfo.gpuStatus[key]}`).join('\n'))
282
),
283
$('tr', undefined,
284
$('td', undefined, 'Load (avg)' as string),
285
$('td', undefined, systemInfo.load || '')
286
),
287
$('tr', undefined,
288
$('td', undefined, 'Memory (System)' as string),
289
$('td', undefined, systemInfo.memory)
290
),
291
$('tr', undefined,
292
$('td', undefined, 'Process Argv' as string),
293
$('td', undefined, systemInfo.processArgs)
294
),
295
$('tr', undefined,
296
$('td', undefined, 'Screen Reader' as string),
297
$('td', undefined, systemInfo.screenReader)
298
),
299
$('tr', undefined,
300
$('td', undefined, 'VM'),
301
$('td', undefined, systemInfo.vmHint)
302
)
303
);
304
reset(target, renderedDataTable);
305
306
systemInfo.remoteData.forEach(remote => {
307
target.appendChild($<HTMLHRElement>('hr'));
308
if (isRemoteDiagnosticError(remote)) {
309
const remoteDataTable = $('table', undefined,
310
$('tr', undefined,
311
$('td', undefined, 'Remote'),
312
$('td', undefined, remote.hostName)
313
),
314
$('tr', undefined,
315
$('td', undefined, ''),
316
$('td', undefined, remote.errorMessage)
317
)
318
);
319
target.appendChild(remoteDataTable);
320
} else {
321
const remoteDataTable = $('table', undefined,
322
$('tr', undefined,
323
$('td', undefined, 'Remote'),
324
$('td', undefined, remote.latency ? `${remote.hostName} (latency: ${remote.latency.current.toFixed(2)}ms last, ${remote.latency.average.toFixed(2)}ms average)` : remote.hostName)
325
),
326
$('tr', undefined,
327
$('td', undefined, 'OS'),
328
$('td', undefined, remote.machineInfo.os)
329
),
330
$('tr', undefined,
331
$('td', undefined, 'CPUs'),
332
$('td', undefined, remote.machineInfo.cpus || '')
333
),
334
$('tr', undefined,
335
$('td', undefined, 'Memory (System)' as string),
336
$('td', undefined, remote.machineInfo.memory)
337
),
338
$('tr', undefined,
339
$('td', undefined, 'VM'),
340
$('td', undefined, remote.machineInfo.vmHint)
341
)
342
);
343
target.appendChild(remoteDataTable);
344
}
345
});
346
}
347
}
348
349
private updateRestrictedMode(restrictedMode: boolean) {
350
this.issueReporterModel.update({ restrictedMode });
351
}
352
353
private updateUnsupportedMode(isUnsupported: boolean) {
354
this.issueReporterModel.update({ isUnsupported });
355
}
356
357
private updateExperimentsInfo(experimentInfo: string | undefined) {
358
this.issueReporterModel.update({ experimentInfo });
359
const target = this.window.document.querySelector<HTMLElement>('.block-experiments .block-info');
360
if (target) {
361
target.textContent = experimentInfo ? experimentInfo : localize('noCurrentExperiments', "No current experiments.");
362
}
363
}
364
}
365
366