1+ import * as vscode from "vscode" ;
2+ import { getNonce } from "./ts/getNonce" ;
3+ import * as wsLib from 'ws' ;
4+ const { setupWs,
5+ request,
6+ getUaSocket,
7+ checkCiPipelineStructure
8+ } = require ( './ts/wsService' ) ;
9+
10+ export class CiPipelineProvider implements vscode . WebviewViewProvider {
11+ _view ?: vscode . WebviewView ;
12+ _doc ?: vscode . TextDocument ;
13+
14+ constructor ( private readonly _extensionUri : vscode . Uri , public extensionContext : vscode . ExtensionContext ) { }
15+
16+
17+
18+ public resolveWebviewView ( webviewView : vscode . WebviewView ) {
19+ this . _view = webviewView ;
20+ let cache = this . extensionContext . globalState ;
21+
22+ webviewView . webview . options = {
23+ // Allow scripts in the webview
24+ enableScripts : true ,
25+
26+ localResourceRoots : [ this . _extensionUri ] ,
27+ } ;
28+
29+ webviewView . webview . html = this . _getHtmlWebview ( webviewView . webview ) ;
30+
31+ webviewView . webview . onDidReceiveMessage ( async ( data ) => {
32+ let socket : any ;
33+ let uaSocket = getUaSocket ( ) ;
34+
35+ switch ( data . type ) {
36+ case "getCiPipelineStages" : {
37+ const socketURL = `wss://${ data . value . dataCenterId } .codesphere.com/workspace-proxy` ;
38+ console . log ( socketURL + `socketURL` ) ;
39+ const accessToken = await this . extensionContext . secrets . get ( "codesphere.accessToken" ) as string ;
40+ const workspaceID : number = parseInt ( data . value . workspaceId ) ;
41+ socket = await setupWs ( new wsLib . WebSocket ( socketURL ) , "workspace-proxy" , accessToken , cache , workspaceID ) ;
42+
43+ uaSocket = getUaSocket ( ) ;
44+ let ciStructure : any ;
45+ const ciPipelineCheck = checkCiPipelineStructure ( uaSocket , 324 ) ;
46+ ciPipelineCheck . then ( ( ci : any ) => {
47+ ciStructure = ci ;
48+ console . log ( 'ciStructure: ' + JSON . stringify ( ciStructure ) ) ;
49+ this . _view ?. webview . postMessage ( {
50+ type : "CIPipelineStages" ,
51+ value : {
52+ 'CIArray' : `${ JSON . stringify ( ciStructure ) } `
53+ }
54+ } ) ;
55+ }
56+ ) ;
57+
58+ await request ( uaSocket , "pipelineStream" , { workspaceId : workspaceID } , "workspace-proxy" , 324 ) ;
59+
60+ break ;
61+ }
62+
63+ case "currentWorkspace" : {
64+ const workspace : any = await cache . get ( "codesphere.workspaceOverview" ) ;
65+
66+ const workspaceId = workspace . id ;
67+ const teamId = workspace . teamId ;
68+ const dataCenterId = workspace . dataCenterId ;
69+
70+ this . _view ?. webview . postMessage ( {
71+ type : "currentWorkspace" ,
72+ value : {
73+ 'currentWorkspace' : `${ workspaceId } ` ,
74+ 'teamId' : `${ teamId } ` ,
75+ 'dcId' : `${ dataCenterId } `
76+ }
77+ } ) ;
78+
79+ break ;
80+ }
81+ }
82+ } ) ;
83+ }
84+
85+
86+ public updateWebviewContent ( ) {
87+ if ( this . _view ) {
88+ this . _view . webview . html = this . _getHtmlWebview ( this . _view . webview ) ;
89+ }
90+ }
91+
92+ public revive ( panel : vscode . WebviewView ) {
93+ this . _view = panel ;
94+ }
95+
96+
97+ private _getHtmlWebview ( webview : vscode . Webview ) {
98+ const styleResetUri = webview . asWebviewUri (
99+ vscode . Uri . joinPath ( this . _extensionUri , "media" , "reset.css" )
100+ ) ;
101+ const scriptUri = webview . asWebviewUri (
102+ vscode . Uri . joinPath ( this . _extensionUri , "out" , "compiled/cipipeline.js" )
103+ ) ;
104+ const styleMainUri = webview . asWebviewUri (
105+ vscode . Uri . joinPath ( this . _extensionUri , "out" , "compiled/cipipeline.css" )
106+ ) ;
107+ const styleVSCodeUri = webview . asWebviewUri (
108+ vscode . Uri . joinPath ( this . _extensionUri , "media" , "vscode.css" )
109+ ) ;
110+
111+ // Use a nonce to only allow a specific script to be run.
112+ const nonce = getNonce ( ) ;
113+
114+ return `<!DOCTYPE html>
115+ <html lang="en">
116+ <head>
117+ <meta charset="UTF-8">
118+ <!--
119+ Use a content security policy to only allow loading images from https or from our extension directory,
120+ and only allow scripts that have a specific nonce.
121+ -->
122+ <meta http-equiv="Content-Security-Policy" content="img-src https: data:; style-src 'unsafe-inline' ${
123+ webview . cspSource
124+ } ; script-src 'nonce-${ nonce } ';">
125+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
126+ <link href="${ styleResetUri } " rel="stylesheet">
127+ <link href="${ styleVSCodeUri } " rel="stylesheet">
128+ <link href="${ styleMainUri } " rel="stylesheet">
129+ <script nonce="${ nonce } ">
130+ const vscode = acquireVsCodeApi();
131+ </script>
132+ </head>
133+ <body>
134+ <script nonce="${ nonce } " src="${ scriptUri } "></script>
135+ </body>
136+ </html>` ;
137+ }
138+ }
0 commit comments