-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathextension.ts
129 lines (112 loc) · 5.38 KB
/
extension.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
121
122
123
124
125
126
127
128
129
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let request = require('request');
let fs = require('fs');
let output : vscode.OutputChannel = vscode.window.createOutputChannel("Jenkins Pipeline Linter");
let lastInput: string;
let validate = vscode.commands.registerCommand('jenkins.pipeline.linter.connector.validate', async () => {
let url = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.url') as string | undefined;
let user = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.user') as string | undefined;
let pass = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.pass') as string | undefined;
let token = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.token') as string | undefined;
let crumbUrl = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.crumbUrl') as string | undefined;
let strictssl = vscode.workspace.getConfiguration().get('jenkins.pipeline.linter.connector.strictssl') as boolean;
if (url === undefined || url.length === 0) {
url = await vscode.window.showInputBox({ prompt: 'Enter Jenkins Pipeline Linter Url.', value: lastInput });
}
if ((user !== undefined && user.length > 0) && (pass === undefined || pass.length === 0) && (token === undefined || token.length === 0)) {
pass = await vscode.window.showInputBox({ prompt: 'Enter password.', password: true });
if(pass === undefined || pass.length === 0) {
token = await vscode.window.showInputBox({ prompt: 'Enter token.', password: false });
}
}
if (url !== undefined && url.length > 0) {
lastInput = url;
if(crumbUrl !== undefined && crumbUrl.length > 0) {
requestCrumb(fs, request, url, crumbUrl, user, pass, token, strictssl, output);
} else {
validateRequest(fs, request, url, user, pass, token, undefined, strictssl, output);
}
} else {
output.appendLine('Jenkins Pipeline Linter Url is not defined.');
}
output.show(true);
});
context.subscriptions.push(validate);
}
function requestCrumb(fs: any, request: any, url: string, crumbUrl: string, user: string|undefined, pass: string|undefined, token: string|undefined, strictssl: boolean, output: vscode.OutputChannel) {
let options: any = {
method: 'GET',
url: crumbUrl,
strictSSL: strictssl
};
if (user !== undefined && user.length > 0) {
if(pass !== undefined && pass.length > 0) {
options.auth = {
'user': user,
'pass': pass
};
} else if ( token !== undefined && token.length > 0) {
let authToken = new Buffer(user + ':' + token).toString('base64');
options.headers = Object.assign(options.headers, { Authorization: 'Basic ' + authToken });
}
}
request(options, (err: any, httpResponse: any, body: any) => {
if (err) {
output.appendLine(err);
} else {
validateRequest(fs, request, url, user, pass, token, body, strictssl, output);
}
});
}
function validateRequest(fs: any, request: any, url: string, user: string|undefined, pass: string|undefined, token: string|undefined, crumb: string|undefined, strictssl: boolean, output: vscode.OutputChannel) {
output.clear();
let activeTextEditor = vscode.window.activeTextEditor;
if (activeTextEditor !== undefined) {
let path = activeTextEditor.document.uri.fsPath;
let filestream = fs.createReadStream(path);
const chunks: any = [];
filestream.on('data', (chunk: any) => {
chunks.push(chunk.toString());
});
filestream.on('end', () => {
let options: any = {
method: 'POST',
url: url,
strictSSL: strictssl,
formData: {
'jenkinsfile': chunks.join()
},
headers: {}
};
if(crumb !== undefined && crumb.length > 0) {
let crumbSplit = crumb.split(':');
options.headers = Object.assign(options.headers, {'Jenkins-Crumb': crumbSplit[1]});
}
if (user !== undefined && user.length > 0) {
if(pass !== undefined && pass.length > 0) {
options.auth = {
'user': user,
'pass': pass
};
} else if ( token !== undefined && token.length > 0) {
let authToken = new Buffer(user + ':' + token).toString('base64');
options.headers = Object.assign(options.headers, { Authorization: 'Basic ' + authToken });
}
}
request(options, (err: any, httpResponse: any, body: any) => {
if (err) {
output.appendLine(err);
} else {
output.appendLine(body);
}
});
});
} else {
output.appendLine('No active text editor. Open the jenkinsfile you want to validate.');
}
}
// this method is called when your extension is deactivated
export function deactivate() {
}