1
1
/* eslint-disable @typescript-eslint/naming-convention */
2
+ import { SpawnOptionsWithoutStdio , spawn } from "child_process" ;
2
3
import {
3
- SpawnOptionsWithoutStdio ,
4
- spawn ,
5
- } from "child_process" ;
6
- import { window , WebviewView , DiagnosticCollection , commands , ExtensionContext , languages , Uri , Diagnostic } from "vscode" ;
7
- import axios from 'axios' ;
4
+ window ,
5
+ WebviewView ,
6
+ DiagnosticCollection ,
7
+ commands ,
8
+ ExtensionContext ,
9
+ languages ,
10
+ Uri ,
11
+ Diagnostic ,
12
+ } from "vscode" ;
13
+ import axios from "axios" ;
8
14
import { GGShieldConfiguration } from "./ggshield-configuration" ;
9
15
import { GGShieldScanResults } from "./api-types" ;
10
16
import * as os from "os" ;
11
17
import { apiToDashboard , dasboardToApi , isFileGitignored } from "../utils" ;
12
18
import { runGGShieldCommand } from "./run-ggshield" ;
13
- import { StatusBarStatus , updateStatusBarItem } from "../gitguardian-interface/gitguardian-status-bar" ;
19
+ import {
20
+ StatusBarStatus ,
21
+ updateStatusBarItem ,
22
+ } from "../gitguardian-interface/gitguardian-status-bar" ;
14
23
import { parseGGShieldResults } from "./ggshield-results-parser" ;
15
24
16
25
/**
@@ -50,22 +59,21 @@ export async function getAPIquota(
50
59
}
51
60
}
52
61
53
-
54
62
export async function getRemediationMessage (
55
63
configuration : GGShieldConfiguration
56
64
) : Promise < string > {
57
65
const apiUrl = dasboardToApi ( configuration . apiUrl ) ;
58
- const path = require ( ' node:path' ) ;
59
- try {
60
- const response = await axios . get ( path . join ( apiUrl , ' v1/metadata' ) , {
61
- headers : {
62
- ' authorization' : `Token ${ configuration . apiKey } `
63
- }
64
- } ) ;
65
- return response . data . remediation_messages . pre_commit ;
66
- } catch ( error ) {
67
- return "An error occured ." ;
68
- }
66
+ const path = require ( " node:path" ) ;
67
+ try {
68
+ const response = await axios . get ( path . join ( apiUrl , " v1/metadata" ) , {
69
+ headers : {
70
+ authorization : `Token ${ configuration . apiKey } ` ,
71
+ } ,
72
+ } ) ;
73
+ return response . data . remediation_messages . pre_commit ;
74
+ } catch ( error ) {
75
+ return "An error occurred ." ;
76
+ }
69
77
}
70
78
71
79
/**
@@ -135,7 +143,6 @@ export function cleanUpFileDiagnostics(fileUri: Uri): void {
135
143
diagnosticCollection . delete ( fileUri ) ;
136
144
}
137
145
138
-
139
146
/**
140
147
* Scan a file using ggshield
141
148
*
@@ -167,6 +174,15 @@ export async function scanFile(
167
174
168
175
// Ignore errors concerning usage
169
176
// This occurs when the path of the file is invalid (i.e.VSCode sending an event for files not on the file system)
177
+ // or when the file is ignored in the .gitguardian.yaml
178
+ if (
179
+ proc . stderr . includes (
180
+ "Error: An ignored file or directory cannot be scanned"
181
+ )
182
+ ) {
183
+ updateStatusBarItem ( StatusBarStatus . ignoredFile ) ;
184
+ return ;
185
+ }
170
186
if ( proc . stderr . includes ( "Usage: ggshield secret scan path" ) ) {
171
187
return undefined ;
172
188
}
@@ -195,11 +211,10 @@ export async function scanFile(
195
211
} else {
196
212
updateStatusBarItem ( StatusBarStatus . noSecretFound ) ;
197
213
}
198
-
214
+
199
215
diagnosticCollection . set ( fileUri , incidentsDiagnostics ) ;
200
216
}
201
217
202
-
203
218
export async function loginGGShield (
204
219
configuration : GGShieldConfiguration ,
205
220
outputChannel : any ,
@@ -232,7 +247,7 @@ export async function loginGGShield(
232
247
if ( urlLine ) {
233
248
const authUrl = urlLine [ 0 ] ;
234
249
webviewView . webview . postMessage ( {
235
- type : ' authLink' ,
250
+ type : " authLink" ,
236
251
link : authUrl ,
237
252
} ) ;
238
253
}
@@ -249,7 +264,7 @@ export async function loginGGShield(
249
264
} else {
250
265
outputChannel . appendLine ( "ggshield login completed successfully" ) ;
251
266
commands . executeCommand ( "setContext" , "isAuthenticated" , true ) ;
252
- await context . globalState . update ( ' isAuthenticated' , true ) ;
267
+ await context . globalState . update ( " isAuthenticated" , true ) ;
253
268
resolve ( ) ;
254
269
}
255
270
} ) ;
@@ -261,44 +276,41 @@ export async function loginGGShield(
261
276
} ) ;
262
277
}
263
278
264
-
265
279
export async function logoutGGShield (
266
280
configuration : GGShieldConfiguration ,
267
281
context : ExtensionContext
268
282
) : Promise < void > {
269
283
runGGShieldCommand ( configuration , [ "auth" , "logout" ] ) ;
270
- commands . executeCommand ( 'setContext' , 'isAuthenticated' , false ) ;
271
- await context . globalState . update ( 'isAuthenticated' , false ) ;
272
-
284
+ commands . executeCommand ( "setContext" , "isAuthenticated" , false ) ;
285
+ await context . globalState . update ( "isAuthenticated" , false ) ;
273
286
}
274
287
275
288
export async function ggshieldAuthStatus (
276
289
configuration : GGShieldConfiguration ,
277
290
context : ExtensionContext
278
- ) : Promise < void > {
279
- let isAuthenticated : boolean ;
280
- const proc = runGGShieldCommand ( configuration , [ "api-status" , "--json" ] ) ;
281
- if ( proc . status === 0 && JSON . parse ( proc . stdout ) . status_code === 200 ) {
282
- isAuthenticated = true ;
291
+ ) : Promise < void > {
292
+ let isAuthenticated : boolean ;
293
+ const proc = runGGShieldCommand ( configuration , [ "api-status" , "--json" ] ) ;
294
+ if ( proc . status === 0 && JSON . parse ( proc . stdout ) . status_code === 200 ) {
295
+ isAuthenticated = true ;
296
+ } else {
297
+ if ( proc . stderr && proc . stderr . includes ( "Config key" ) ) {
298
+ window . showErrorMessage ( `Gitguardian: ${ proc . stderr } ` ) ;
283
299
}
284
- else {
285
- if ( proc . stderr && proc . stderr . includes ( "Config key" ) ) {
286
- window . showErrorMessage ( `Gitguardian: ${ proc . stderr } ` ) ;
287
- }
288
- console . log ( proc . stderr ) ;
289
- isAuthenticated = false ;
290
- }
291
- commands . executeCommand ( 'setContext' , 'isAuthenticated' , isAuthenticated ) ;
292
- await context . globalState . update ( 'isAuthenticated' , isAuthenticated ) ;
300
+ console . log ( proc . stderr ) ;
301
+ isAuthenticated = false ;
302
+ }
303
+ commands . executeCommand ( "setContext" , "isAuthenticated" , isAuthenticated ) ;
304
+ await context . globalState . update ( "isAuthenticated" , isAuthenticated ) ;
293
305
}
294
306
295
307
/**
296
308
* Get ggshield API key from ggshield config list
297
- *
309
+ *
298
310
* Search for the correct instance section and return the token
299
311
* */
300
312
export function ggshieldApiKey (
301
- configuration : GGShieldConfiguration ,
313
+ configuration : GGShieldConfiguration
302
314
) : string | undefined {
303
315
const proc = runGGShieldCommand ( configuration , [ "config" , "list" ] ) ;
304
316
if ( proc . stderr || proc . error ) {
@@ -308,7 +320,9 @@ export function ggshieldApiKey(
308
320
console . log ( proc . stdout ) ;
309
321
const apiUrl = configuration . apiUrl ;
310
322
311
- const regexInstanceSection = `\\[${ apiToDashboard ( apiUrl ) } \\]([\\s\\S]*?)(?=\\[|$)` ;
323
+ const regexInstanceSection = `\\[${ apiToDashboard (
324
+ apiUrl
325
+ ) } \\]([\\s\\S]*?)(?=\\[|$)`;
312
326
const instanceSectionMatch = proc . stdout . match ( regexInstanceSection ) ;
313
327
314
328
if ( instanceSectionMatch ) {
@@ -317,11 +331,11 @@ export function ggshieldApiKey(
317
331
const matchToken = instanceSection . match ( regexToken ) ;
318
332
319
333
// if the token is not found, or is not a valid token, return undefined
320
- if ( ! matchToken || matchToken [ 1 ] . trim ( ) . length !== 71 ) {
334
+ if ( ! matchToken || matchToken [ 1 ] . trim ( ) . length !== 71 ) {
321
335
return undefined ;
322
336
}
323
-
337
+
324
338
return matchToken [ 1 ] . trim ( ) ;
325
339
}
326
340
}
327
- }
341
+ }
0 commit comments