-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCiPipelineProvider.ts
More file actions
279 lines (224 loc) · 10.1 KB
/
CiPipelineProvider.ts
File metadata and controls
279 lines (224 loc) · 10.1 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
import * as vscode from "vscode";
import { getNonce } from "./ts/getNonce";
import * as wsLib from 'ws';
const { setupWs,
request,
getUaSocket,
checkCiPipelineStructure,
checkCiPipelineState,
ciStepHandler,
ciStageStatusHandler,
} = require('./ts/wsService');
export class CiPipelineProvider implements vscode.WebviewViewProvider {
_view?: vscode.WebviewView;
_doc?: vscode.TextDocument;
constructor(private readonly _extensionUri: vscode.Uri, public extensionContext: vscode.ExtensionContext) {
this.postMessageToWebview = this.postMessageToWebview.bind(this);
}
postMessageToWebview(type: string, values: any): any {
this._view?.webview.postMessage({
type: type,
value: values
});
}
public resolveWebviewView(webviewView: vscode.WebviewView) {
this._view = webviewView;
let cache = this.extensionContext.globalState;
webviewView.webview.options = {
enableScripts: true,
localResourceRoots: [this._extensionUri],
};
webviewView.webview.html = this._getHtmlWebview(webviewView.webview);
webviewView.webview.onDidReceiveMessage(async (data) => {
let socket: any;
let uaSocket = getUaSocket();
let instanceURL: string = cache.get("codesphere.instanceURL") as string;
instanceURL = instanceURL.replace(/^https?:\/\//, '');
instanceURL = instanceURL.replace(/\/$/, '');
switch (data.type) {
case "getCiPipelineStages": {
const socketURL = `wss://${data.value.dataCenterId}.${instanceURL}/workspace-proxy`;
const accessToken = await this.extensionContext.secrets.get("codesphere.accessToken") as string;
const workspaceID: number = parseInt(data.value.workspaceId);
socket = await setupWs(new wsLib.WebSocket(socketURL), "workspace-proxy", accessToken, cache, workspaceID);
uaSocket = getUaSocket();
let ciStructure: any;
const ciPipelineCheck = checkCiPipelineStructure(uaSocket, 324);
ciPipelineCheck.then((ci: any) => {
ciStructure = ci;
console.log("ciStructure: ", JSON.stringify(ciStructure));
this._view?.webview.postMessage({
type: "CIPipelineStages",
value: {
'CIArray': `${JSON.stringify(ciStructure)}`
}
});
}
);
await request(uaSocket, "pipelineStream", { workspaceId: workspaceID}, "workspace-proxy", 324);
break;
}
case "currentWorkspace": {
const workspace: any = await cache.get("codesphere.workspaceOverview");
const workspaceId = workspace.id;
const teamId = workspace.teamId;
const dataCenterId = workspace.dataCenterId;
this._view?.webview.postMessage({
type: "currentWorkspace",
value: {
'currentWorkspace': `${workspaceId}`,
'teamId': `${teamId}`,
'dcId': `${dataCenterId}`
}
});
break;
}
case "stopCiStage": {
await request(uaSocket, "abortPipeline", { workspaceId: parseInt(data.value.workspaceId), stage: data.value.stage }, "workspace-proxy", 666);
this._view?.webview.postMessage({
type: "setActionButtion",
value: {
stage: data.value.stage
}
});
break;
}
case "getCiStageStatus": {
if (!data.value) {
return;
}
const delay = (ms: any) => new Promise(resolve => setTimeout(resolve, ms));
const workspaceId = parseInt(data.value.workspaceId);
const socketURL = `wss://${data.value.datacenterId}.${instanceURL}/workspace-proxy`;
const accessToken = cache.get("codesphere.accessTokenCache");
socket = await setupWs(new wsLib.WebSocket(socketURL), "workspace-proxy", accessToken, cache, workspaceId);
let uaSocketconnect2 = getUaSocket();
let prepare;
let test;
let run;
const prepareCheck = checkCiPipelineState(uaSocketconnect2, 35);
const testCheck = checkCiPipelineState(uaSocketconnect2, 136);
const runCheck = checkCiPipelineState(uaSocketconnect2, 237);
testCheck.then((resultTest: any) => {
test = resultTest;
}
);
runCheck.then((resultRun: any) => {
run = resultRun;
});
prepareCheck.then((result: any) => {
prepare = result;
});
await request(uaSocketconnect2, "executionInfo", { workspaceId: workspaceId, stage: 'test' }, "workspace-proxy", 136);
await delay(200);
await request(uaSocketconnect2, "executionInfo", { workspaceId: workspaceId, stage: 'run' }, "workspace-proxy", 237);
await delay(200);
await request(uaSocketconnect2, "executionInfo", { workspaceId: workspaceId, stage: 'prepare' }, "workspace-proxy", 35);
await delay(200);
this._view?.webview.postMessage({
type: "ciPipelineStatus",
value: {
prepare: prepare,
test: test,
run: run,
dynamic: false
},
});
break;
}
case "startCiStage": {
if (!data.value) {
return;
}
const delay = (ms: any) => new Promise(resolve => setTimeout(resolve, ms));
const workspaceId = parseInt(data.value.workspaceId);
const stage = data.value.stage;
let endpoint: number = 0;
const socketURL = `wss://${data.value.dataCenterId}.${instanceURL}/workspace-proxy`;
const accessToken = await this.extensionContext.secrets.get("codesphere.accessToken") as string;
socket = await setupWs(new wsLib.WebSocket(socketURL), "workspace-proxy", accessToken, cache, workspaceId);
let uaSocket = getUaSocket();
let ciStageFinished = false;
let ciPipelineStatus;
if (stage === "prepare") {
endpoint = 36;
}
if (stage === "test") {
endpoint = 137;
}
if (stage === "run") {
endpoint = 238;
}
await request(uaSocket, "executionInfo", { workspaceId: workspaceId, stage: stage }, "workspace-proxy", endpoint);
const endpointArray = Array.from({length: data.value.stepcount}, (_, i) => 400 + i);
await request(uaSocket, "startPipeline", { workspaceId: workspaceId, stage: stage }, "workspace-proxy", 32);
for (let i = 0; i < data.value.stepcount; i++) {
request(uaSocket, "logs", { workspaceId: workspaceId, stage: stage, step:i }, "workspace-proxy", (400+i));
}
ciStageStatusHandler(uaSocket, endpoint, this.postMessageToWebview, stage).then((status: any) => {
ciPipelineStatus = status;
});
let log = ciStepHandler(uaSocket, endpoint, endpointArray, this.postMessageToWebview, stage);
while (!ciStageFinished) {
await delay(1000);
if (ciPipelineStatus === 'success') {
ciStageFinished = true;
}
if (ciPipelineStatus === 'failure') {
ciStageFinished = true;
}
}
break;
}
};
}
);
}
public updateWebviewContent() {
if (this._view) {
this._view.webview.html = this._getHtmlWebview(this._view.webview);
}
}
public revive(panel: vscode.WebviewView) {
this._view = panel;
}
private _getHtmlWebview(webview: vscode.Webview) {
const styleResetUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, "media", "reset.css")
);
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, "out", "compiled/cipipeline.js")
);
const styleMainUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, "out", "compiled/cipipeline.css")
);
const styleVSCodeUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, "media", "vscode.css")
);
// Use a nonce to only allow a specific script to be run.
const nonce = getNonce();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--
Use a content security policy to only allow loading images from https or from our extension directory,
and only allow scripts that have a specific nonce.
-->
<meta http-equiv="Content-Security-Policy" content="img-src https: data:; style-src 'unsafe-inline' ${
webview.cspSource
}; script-src 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${styleResetUri}" rel="stylesheet">
<link href="${styleVSCodeUri}" rel="stylesheet">
<link href="${styleMainUri}" rel="stylesheet">
<script nonce="${nonce}">
const vscode = acquireVsCodeApi();
</script>
</head>
<body>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
}
}