forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
issueService.ts
120 lines (100 loc) · 5.06 KB
/
issueService.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { userAgent } from 'vs/base/common/platform';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { IssueReporterData } from 'vs/platform/issue/common/issue';
import { normalizeGitHubUrl } from 'vs/platform/issue/common/issueReporterUtil';
import { ILogService } from 'vs/platform/log/common/log';
import { IProductService } from 'vs/platform/product/common/productService';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IIssueDataProvider, IIssueUriRequestHandler, IWorkbenchIssueService } from 'vs/workbench/services/issue/common/issue';
export class WebIssueService implements IWorkbenchIssueService {
declare readonly _serviceBrand: undefined;
private readonly _handlers = new Map<string, IIssueUriRequestHandler>();
private readonly _providers = new Map<string, IIssueDataProvider>();
constructor(
@IExtensionService private readonly extensionService: IExtensionService,
@IProductService private readonly productService: IProductService,
@ILogService private readonly logService: ILogService
) { }
//TODO @TylerLeonhardt @Tyriar to implement a process explorer for the web
async openProcessExplorer(): Promise<void> {
console.error('openProcessExplorer is not implemented in web');
}
async openReporter(options: Partial<IssueReporterData>): Promise<void> {
const extensionId = options.extensionId;
// If we don't have a extensionId, treat this as a Core issue
if (!extensionId) {
if (this.productService.reportIssueUrl) {
const uri = this.getIssueUriFromStaticContent(this.productService.reportIssueUrl);
dom.windowOpenNoOpener(uri);
return;
}
throw new Error(`No issue reporting URL configured for ${this.productService.nameLong}.`);
}
// If we have a handler registered for this extension, use it instead of anything else
if (this._handlers.has(extensionId)) {
try {
const uri = await this.getIssueUriFromHandler(extensionId, CancellationToken.None);
dom.windowOpenNoOpener(uri);
return;
} catch (e) {
this.logService.error(e);
}
}
// if we don't have a handler, or the handler failed, try to get the extension's github url
const selectedExtension = this.extensionService.extensions.filter(ext => ext.identifier.value === options.extensionId)[0];
const extensionGitHubUrl = this.getExtensionGitHubUrl(selectedExtension);
if (!extensionGitHubUrl) {
throw new Error(`Unable to find issue reporting url for ${extensionId}`);
}
const uri = this.getIssueUriFromStaticContent(`${extensionGitHubUrl}/issues/new`, selectedExtension);
dom.windowOpenNoOpener(uri);
}
registerIssueUriRequestHandler(extensionId: string, handler: IIssueUriRequestHandler): IDisposable {
this._handlers.set(extensionId, handler);
return toDisposable(() => this._handlers.delete(extensionId));
}
registerIssueDataProvider(extensionId: string, handler: IIssueDataProvider): IDisposable {
this._providers.set(extensionId, handler);
return toDisposable(() => this._providers.delete(extensionId));
}
private async getIssueUriFromHandler(extensionId: string, token: CancellationToken): Promise<string> {
const handler = this._handlers.get(extensionId);
if (!handler) {
throw new Error(`No handler registered for extension ${extensionId}`);
}
const result = await handler.provideIssueUrl(token);
return result.toString(true);
}
private getExtensionGitHubUrl(extension: IExtensionDescription): string {
if (extension.isBuiltin && this.productService.reportIssueUrl) {
return normalizeGitHubUrl(this.productService.reportIssueUrl);
}
let repositoryUrl = '';
const bugsUrl = extension?.bugs?.url;
const extensionUrl = extension?.repository?.url;
// If given, try to match the extension's bug url
if (bugsUrl && bugsUrl.match(/^https?:\/\/github\.com\/(.*)/)) {
repositoryUrl = normalizeGitHubUrl(bugsUrl);
} else if (extensionUrl && extensionUrl.match(/^https?:\/\/github\.com\/(.*)/)) {
repositoryUrl = normalizeGitHubUrl(extensionUrl);
}
return repositoryUrl;
}
private getIssueUriFromStaticContent(baseUri: string, extension?: IExtensionDescription): string {
const issueDescription = `ADD ISSUE DESCRIPTION HERE
Version: ${this.productService.version}
Commit: ${this.productService.commit ?? 'unknown'}
User Agent: ${userAgent ?? 'unknown'}
Embedder: ${this.productService.embedderIdentifier ?? 'unknown'}
${extension?.version ? `\nExtension version: ${extension.version}` : ''}
<!-- generated by web issue reporter -->`;
return `${baseUri}?body=${encodeURIComponent(issueDescription)}&labels=web`;
}
}