-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathgitImportLoader.ts
More file actions
293 lines (277 loc) · 13.5 KB
/
gitImportLoader.ts
File metadata and controls
293 lines (277 loc) · 13.5 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
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { ExtensionID } from '../../util/constants';
import { GitProvider } from '../../git-import/types/git';
import { getGitService } from '../../git-import/services';
import { DetectedServiceData, DetectedStrategy, detectImportStrategies } from '../../git-import/utils';
import { ComponentTypesView } from '../../registriesView';
import { ComponentTypeDescription } from '../../odo/componentType';
import { Response } from '../../git-import/types';
import { Uri, workspace } from 'vscode';
import { Component } from '../../openshift/component';
import OpenShiftItem from '../../openshift/openshiftItem';
import { selectWorkspaceFolder } from '../../util/workspace';
import jsYaml = require('js-yaml');
import GitUrlParse = require('git-url-parse');
import cp = require('child_process');
import { vsCommand } from '../../vscommand';
let panel: vscode.WebviewPanel;
export class Command {
@vsCommand('openshift.component.importFromGit')
static async createComponent(event: any) {
const workspacePath = await selectWorkspaceFolder();
const appendedUri = Uri.joinPath(workspacePath, event.projectName);
//const wsFolderLength = workspace?.workspaceFolders?.length || 0;
panel.webview.postMessage({
action: 'cloneStarted'
})
//workspace.updateWorkspaceFolders(wsFolderLength, 0, { uri: appendedUri });
workspace.updateWorkspaceFolders(workspace.workspaceFolders? workspace.workspaceFolders.length : 0 , null, { uri: appendedUri });
await clone(event, appendedUri.fsPath);
if (!event.isDevFile) {
panel.webview.postMessage({
action: 'start_create_component'
})
try {
await Component.createFromRootWorkspaceFolder(appendedUri, undefined,
{
componentTypeName: event.compDesc?.devfileData.devfile.metadata.name,
projectName: event.projectName,
applicationName: event.applicationName,
compName: event.componentName,
registryName: event.compDesc?.registry.name,
devFilePath: !event.devFilePath || event.devFilePath === 'devfile.yaml' || event.devFilePath === 'devfile.yml' ?
'' : event.devFilePath
}, true);
panel.webview.postMessage({
action: event.action,
status: true
});
vscode.window.showInformationMessage(`Component '${event.componentName}' successfully created. Perform actions on it from Components View.`);
} catch (e) {
vscode.window.showErrorMessage(`Error occurred while creating Component '${event.componentName}': ${e.message}`);
panel.webview.postMessage({
action: event.action,
status: false
});
}
} else {
panel.webview.postMessage({
action: event.action,
status: true
});
vscode.window.showInformationMessage('Selected Component added to the workspace.');
}
}
}
async function gitImportMessageListener(event: any): Promise<any> {
switch (event?.action) {
case 'validateGitURL':
validateGitURL(event);
break;
case 'validateComponentName':
validateComponentName(event)
break;
case 'validateDevFilePath':
validateDevFilePath(event)
break;
case 'parseGitURL':
let compDescription: ComponentTypeDescription[];
let isDevFile = false;
const gitProvider = getGitProvider(event.parser.host);
if (gitProvider !== GitProvider.INVALID) {
const service = getGitService(event.param.value, gitProvider, '', '', undefined, 'devfile.yaml');
const importService: DetectedServiceData = await detectImportStrategies(event.param, service);
const response: Response = await service.isDevfilePresent();
if (importService.strategies.length === 1) {
response.status = true;
const strategy: DetectedStrategy = importService.strategies[0];
const detectedCustomData = strategy.detectedCustomData[0];
compDescription = getCompDescription(detectedCustomData.name.toLowerCase(), detectedCustomData.language.toLowerCase());
} else if (response.status) {
isDevFile = true;
const devFileContent = await service.getDevfileContent();
const yamlDoc: any = jsYaml.load(devFileContent);
compDescription = getCompDescription(yamlDoc.metadata.projectType.toLowerCase(), yamlDoc.metadata.language.toLowerCase())
}
panel.webview.postMessage({
action: event?.action,
gitURL: event.param.value,
appName: response.status ? event.parser.name + '-app' : undefined,
name: response.status ? event.parser.name + '-comp' : undefined,
error: !response.status,
isDevFile: isDevFile,
helpText: response.status ? 'The git repo is valid.' : 'Rate limit exceeded',
compDescription: compDescription,
parser: event.parser
});
break;
}
break;
case 'createComponent': {
vscode.commands.executeCommand('openshift.component.importFromGit', event);
break;
}
case 'close': {
panel.dispose();
break;
}
}
}
export default class GitImportLoader {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
static get extensionPath() {
return vscode.extensions.getExtension(ExtensionID).extensionPath
}
// eslint-disable-next-line @typescript-eslint/require-await
static async loadView(title: string): Promise<vscode.WebviewPanel> {
const localResourceRoot = vscode.Uri.file(path.join(GitImportLoader.extensionPath, 'out', 'gitImportViewer'));
if (panel) {
panel.reveal(vscode.ViewColumn.One);
} else {
panel = vscode.window.createWebviewPanel('gitImportView', title, vscode.ViewColumn.One, {
enableScripts: true,
localResourceRoots: [localResourceRoot],
retainContextWhenHidden: true
});
panel.iconPath = vscode.Uri.file(path.join(GitImportLoader.extensionPath, 'images/gitImport/git.svg'));
panel.webview.html = GitImportLoader.getWebviewContent(GitImportLoader.extensionPath, panel);
panel.onDidDispose(() => {
panel = undefined;
});
panel.webview.onDidReceiveMessage(gitImportMessageListener);
}
return panel;
}
private static getWebviewContent(extensionPath: string, p: vscode.WebviewPanel): string {
// Local path to main script run in the webview
const reactAppRootOnDisk = path.join(extensionPath, 'out', 'gitImportViewer');
const reactAppPathOnDisk = vscode.Uri.file(
path.join(reactAppRootOnDisk, 'gitImportViewer.js'),
);
const reactAppUri = p.webview.asWebviewUri(reactAppPathOnDisk);
const htmlString: Buffer = fs.readFileSync(path.join(reactAppRootOnDisk, 'index.html'));
const meta = `<meta http-equiv="Content-Security-Policy"
content="connect-src *;
default-src 'none';
img-src ${p.webview.cspSource} https: 'self' data:;
script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
style-src 'self' vscode-resource: 'unsafe-inline';">`;
return `${htmlString}`
.replace('%COMMAND%', '')
.replace('%PLATFORM%', process.platform)
.replace('gitImportViewer.js', `${reactAppUri}`)
.replace('%BASE_URL%', `${reactAppUri}`)
.replace('<!-- meta http-equiv="Content-Security-Policy" -->', meta);
}
static refresh(): void {
if (panel) {
panel.webview.postMessage({ action: 'loadingComponents' });
}
}
}
function validateGitURL(event: any) {
if (event.param.trim().length === 0) {
panel.webview.postMessage({
action: event.action,
error: true,
helpText: 'Required',
gitURL: event.param
});
} else {
try {
const parse = GitUrlParse(event.param);
const isGitRepo = isGitURL(parse.host);
if(!isGitRepo) {
throw 'Invalid Git URL';
}
if (parse.organization !== '' && parse.name !== '') {
panel.webview.postMessage({
action: event.action,
error: false,
helpText: 'The git repo is valid.',
parser: parse,
gitURL: event.param
});
} else {
panel.webview.postMessage({
action: event.action,
error: false,
helpText: 'URL is valid but cannot be reached.',
parser: parse,
gitURL: event.param
});
}
} catch (e) {
panel.webview.postMessage({
action: event.action,
error: true,
helpText: 'Invalid Git URL.',
gitURL: event.param
});
}
}
}
function getGitProvider(host: string): GitProvider {
return host.indexOf(GitProvider.GITHUB) !== -1 ? GitProvider.GITHUB :
host.indexOf(GitProvider.BITBUCKET) !== -1 ? GitProvider.BITBUCKET :
host.indexOf(GitProvider.GITLAB) !== -1 ? GitProvider.GITLAB : GitProvider.INVALID;
}
function getCompDescription(projectType: string, language: string): ComponentTypeDescription[] {
const compDescriptions = ComponentTypesView.instance.getCompDescriptions();
return Array.from(compDescriptions).filter((desc) => desc.devfileData.devfile.metadata.projectType.toLowerCase() === projectType ||
desc.devfileData.devfile.metadata.language.toLowerCase() === language || desc.devfileData.devfile.metadata.name.toLowerCase() === language);
}
function clone(event: any, location: string): Promise<any> {
const gitExtension = vscode.extensions.getExtension('vscode.git').exports;
const git = gitExtension.getAPI(1).git.path;
// run 'git clone url location' as external process and return location
return new Promise((resolve, reject) => cp.exec(`${git} clone ${event.gitURL} ${location}`, (error: cp.ExecException) => error ?
showError(event, location, error.message) : resolve(true)));
}
function validateComponentName(event: any) {
let validationMessage = OpenShiftItem.emptyName(`Required ${event.param}`, event.param.trim());
if (!validationMessage) validationMessage = OpenShiftItem.validateMatches(`Not a valid ${event.param}.
Please use lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character`, event.param);
if (!validationMessage) validationMessage = OpenShiftItem.lengthName(`${event.param} should be between 2-63 characters`, event.param, 0);
panel.webview.postMessage({
action: event.action,
error: !validationMessage ? false : true,
helpText: !validationMessage ? 'A unique name given to the component that will be used to name associated resources.' : validationMessage,
compName: event.param
});
}
function validateDevFilePath(event: any) {
let validationMessage = OpenShiftItem.emptyName(`Required ${event.param}`, event.param.trim());
if (!validationMessage) validationMessage = OpenShiftItem.validateFilePath(`Not matches ^[a-z]:((\/|\\\\)[a-zA-Z0-9_ \\-]+)+\\.yaml$`, event.param);
if (!validationMessage && event.param !== 'devfile.yaml' && event.param !== 'devfile.yml') {
const uri = Uri.parse(event.param);
const devFileLocation = path.join(uri.fsPath);
validationMessage = fs.existsSync(devFileLocation) ? null : 'devfile not available on the given path';
}
panel.webview.postMessage({
action: event.action,
error: !validationMessage ? false : true,
helpText: !validationMessage ? 'Validated' : validationMessage,
devFilePath: event.param
});
}
function showError(event: any, location: string, message: string): void {
panel.webview.postMessage({
action: event.action,
status: false
});
if (message.indexOf('already exists') !== -1) {
vscode.window.showErrorMessage(`Folder already exists on the selected ${location.substring(0, location.lastIndexOf('\\'))}`);
} else {
vscode.window.showErrorMessage('Error occurred while cloning the repository. Please try again.');
}
}
function isGitURL(host: string): boolean {
return ['github.com','bitbucket.org','gitlab.com'].includes(host);
}