1
1
import {
2
+ cleanUpFileDiagnostics ,
3
+ createDiagnosticCollection ,
2
4
ggshieldApiKey ,
3
5
ggshieldAuthStatus ,
4
- ggshieldScanFile ,
5
6
ignoreLastFound ,
6
7
ignoreSecret ,
7
8
loginGGShield ,
8
9
logoutGGShield ,
10
+ scanFile ,
9
11
showAPIQuota ,
10
12
} from "./lib/ggshield-api" ;
11
13
import {
12
14
getConfiguration ,
13
- GGShieldConfiguration ,
14
15
setApiKey ,
15
16
} from "./lib/ggshield-configuration" ;
16
- import { parseGGShieldResults } from "./lib/ggshield-results-parser" ;
17
17
import {
18
- Diagnostic ,
19
- DiagnosticCollection ,
20
18
ExtensionContext ,
21
19
Uri ,
22
20
commands ,
23
21
languages ,
24
22
window ,
25
23
workspace ,
26
- StatusBarItem ,
27
- StatusBarAlignment ,
28
24
WebviewView ,
29
25
} from "vscode" ;
30
26
import { GGShieldResolver } from "./lib/ggshield-resolver" ;
31
27
import { getCurrentFile , isGitInstalled } from "./utils" ;
32
28
import { GitGuardianWebviewProvider } from "./ggshield-webview/gitguardian-webview-view" ;
33
- import { StatusBarStatus , updateStatusBarItem } from "./gitguardian-interface/gitguardian-status-bar" ;
29
+ import { createStatusBarItem , StatusBarStatus , updateStatusBarItem } from "./gitguardian-interface/gitguardian-status-bar" ;
34
30
import {
35
31
generateSecretName ,
36
32
GitGuardianSecretHoverProvider ,
37
33
} from "./gitguardian-interface/gitguardian-hover-provider" ;
38
34
import { GitGuardianQuotaWebviewProvider } from "./ggshield-webview/gitguardian-quota-webview" ;
39
35
import { GitGuardianRemediationMessageWebviewProvider } from "./ggshield-webview/gitguardian-remediation-message-view" ;
40
36
41
- /**
42
- * Extension diagnostic collection
43
- */
44
- let diagnosticCollection : DiagnosticCollection ;
45
- let statusBar : StatusBarItem ;
46
-
47
- /**
48
- * Scan a file using ggshield
49
- *
50
- * - retrieve configuration
51
- * - scan file using ggshield CLI application
52
- * - parse ggshield results
53
- * - set diagnostics collection so the incdients are visible to the user
54
- *
55
- * @param filePath path to file
56
- * @param fileUri file uri
57
- */
58
- async function scanFile (
59
- this : any ,
60
- filePath : string ,
61
- fileUri : Uri ,
62
- configuration : GGShieldConfiguration
63
- ) : Promise < void > {
64
- const results = ggshieldScanFile ( filePath , configuration ) ;
65
- if ( ! results ) {
66
- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
67
- return ;
68
- }
69
- let incidentsDiagnostics : Diagnostic [ ] = parseGGShieldResults ( results ) ;
70
- if ( incidentsDiagnostics . length !== 0 ) {
71
- updateStatusBarItem ( StatusBarStatus . secretFound , statusBar ) ;
72
- } else {
73
- updateStatusBarItem ( StatusBarStatus . noSecretFound , statusBar ) ;
74
- }
75
- diagnosticCollection . set ( fileUri , incidentsDiagnostics ) ;
76
- }
77
-
78
- /**
79
- * Clean up file diagnostics
80
- *
81
- * @param fileUri file uri
82
- */
83
- function cleanUpFileDiagnostics ( fileUri : Uri ) : void {
84
- diagnosticCollection . delete ( fileUri ) ;
85
- }
86
37
87
38
function registerOpenViewsCommands (
88
39
context : ExtensionContext ,
@@ -162,13 +113,11 @@ export function activate(context: ExtensionContext) {
162
113
) ;
163
114
context . subscriptions . push ( ggshieldViewProvider , ggshieldRemediationMessageViewProvider , ggshieldQuotaViewProvider ) ;
164
115
165
- statusBar = window . createStatusBarItem ( StatusBarAlignment . Left , 0 ) ;
166
- updateStatusBarItem ( StatusBarStatus . initialization , statusBar ) ;
116
+ createStatusBarItem ( context ) ;
167
117
168
118
//generic commands to open correct view on status bar click
169
119
registerOpenViewsCommands ( context , outputChannel ) ;
170
120
registerQuotaViewCommands ( ggshieldQuotaViewProvider ) ;
171
- context . subscriptions . push ( statusBar ) ;
172
121
173
122
context . subscriptions . push (
174
123
languages . registerHoverProvider ( "*" , new GitGuardianSecretHoverProvider ( ) )
@@ -180,14 +129,14 @@ export function activate(context: ExtensionContext) {
180
129
// Check if ggshield is authenticated
181
130
ggshieldAuthStatus ( configuration , context ) ;
182
131
if ( context . globalState . get ( "isAuthenticated" , false ) ) {
183
- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
132
+ updateStatusBarItem ( StatusBarStatus . ready ) ;
184
133
setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
185
134
ggshieldViewProvider . refresh ( ) ;
186
135
ggshieldRemediationMessageViewProvider . refresh ( ) ;
187
136
ggshieldQuotaViewProvider . refresh ( ) ;
188
137
189
138
} else {
190
- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
139
+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
191
140
}
192
141
} )
193
142
. then ( async ( ) => {
@@ -203,9 +152,7 @@ export function activate(context: ExtensionContext) {
203
152
. then ( ( ) => {
204
153
// Start scanning documents on activation events
205
154
// (i.e. when a new document is opened or when the document is saved)
206
- diagnosticCollection = languages . createDiagnosticCollection ( "ggshield" ) ;
207
-
208
- context . subscriptions . push ( diagnosticCollection ) ;
155
+ createDiagnosticCollection ( context ) ;
209
156
context . subscriptions . push (
210
157
workspace . onDidSaveTextDocument ( ( textDocument ) => {
211
158
// Check if the document is inside the workspace
@@ -249,7 +196,7 @@ export function activate(context: ExtensionContext) {
249
196
scanFile (
250
197
currentFile ,
251
198
Uri . file ( currentFile ) ,
252
- ggshieldResolver . configuration
199
+ ggshieldResolver . configuration ,
253
200
) ;
254
201
}
255
202
) ,
@@ -262,10 +209,10 @@ export function activate(context: ExtensionContext) {
262
209
context
263
210
) . then ( ( ) => {
264
211
if ( context . globalState . get ( "isAuthenticated" , false ) ) {
265
- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
212
+ updateStatusBarItem ( StatusBarStatus . ready ) ;
266
213
setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
267
214
} else {
268
- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
215
+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
269
216
}
270
217
ggshieldViewProvider . refresh ( ) ;
271
218
ggshieldRemediationMessageViewProvider . refresh ( ) ;
@@ -276,7 +223,7 @@ export function activate(context: ExtensionContext) {
276
223
} ) ,
277
224
commands . registerCommand ( "gitguardian.logout" , async ( ) => {
278
225
logoutGGShield ( ggshieldResolver . configuration , context ) ;
279
- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
226
+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
280
227
setApiKey ( configuration , undefined ) ;
281
228
ggshieldViewProvider . refresh ( ) ;
282
229
ggshieldRemediationMessageViewProvider . refresh ( ) ;
@@ -286,13 +233,8 @@ export function activate(context: ExtensionContext) {
286
233
} )
287
234
. catch ( ( error ) => {
288
235
outputChannel . appendLine ( `Error: ${ error . message } ` ) ;
289
- updateStatusBarItem ( StatusBarStatus . error , statusBar ) ;
236
+ updateStatusBarItem ( StatusBarStatus . error ) ;
290
237
} ) ;
291
238
}
292
239
293
- export function deactivate ( ) {
294
- if ( diagnosticCollection ) {
295
- diagnosticCollection . dispose ( ) ;
296
- statusBar . dispose ( ) ;
297
- }
298
- }
240
+ export function deactivate ( ) { }
0 commit comments