1+ /* eslint-disable @typescript-eslint/naming-convention */
12import {
23 SpawnOptionsWithoutStdio ,
34 SpawnSyncOptionsWithStringEncoding ,
@@ -22,17 +23,25 @@ export function runGGShieldCommand(
2223 args : string [ ]
2324) : SpawnSyncReturns < string > {
2425 const { ggshieldPath, apiUrl, apiKey } = configuration ;
26+ let env : {
27+ GITGUARDIAN_API_URL : string ;
28+ GG_USER_AGENT : string ;
29+ GITGUARDIAN_API_KEY ?: string ; // Note the ? to indicate this property is optional
30+ } = {
31+ GITGUARDIAN_API_URL : apiUrl ,
32+ GG_USER_AGENT : "gitguardian-vscode" ,
33+ } ;
34+
35+ if ( apiKey ) {
36+ env = {
37+ ...env ,
38+ // eslint-disable-next-line @typescript-eslint/naming-convention
39+ GITGUARDIAN_API_KEY : apiKey ,
40+ } ;
41+ }
2542
2643 let options : SpawnSyncOptionsWithStringEncoding = {
2744 cwd : os . tmpdir ( ) ,
28- env : {
29- // eslint-disable-next-line @typescript-eslint/naming-convention
30- GITGUARDIAN_API_URL : apiUrl ,
31- // eslint-disable-next-line @typescript-eslint/naming-convention
32- GG_USER_AGENT : "gitguardian-vscode" ,
33- //eslint-disable-next-line @typescript-eslint/naming-convention
34- GITGUARDIAN_API_KEY : apiKey ,
35- } ,
3645 encoding : "utf-8" ,
3746 windowsHide : true ,
3847 } ;
@@ -249,9 +258,18 @@ export function ggshieldApiKey(
249258 return undefined ;
250259 } else {
251260 console . log ( proc . stdout ) ;
252- const regexToken = / t o k e n : ( [ a - z A - Z 0 - 9 ] + ) / ;
253- const matchToken = proc . stdout . match ( regexToken ) ;
254-
255- return matchToken ? matchToken [ 1 ] . trim ( ) : undefined ;
261+ const apiUrl = configuration . apiUrl ;
262+ const re = / a p i / ;
263+
264+ const regexInstanceSection = `\\[${ apiUrl . replace ( re , "dashboard" ) } \\]([\\s\\S]*?)(?=\\[|$)` ;
265+ const instanceSectionMatch = proc . stdout . match ( regexInstanceSection ) ;
266+
267+ if ( instanceSectionMatch ) {
268+ const instanceSection = instanceSectionMatch [ 0 ] ;
269+ const regexToken = / t o k e n : \s ( [ a - z A - Z 0 - 9 ] + ) / ;
270+ const matchToken = instanceSection . match ( regexToken ) ;
271+
272+ return matchToken ? matchToken [ 1 ] . trim ( ) : undefined ;
273+ }
256274 }
257275}
0 commit comments