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