@@ -3,14 +3,20 @@ import {
33 SpawnOptionsWithoutStdio ,
44 spawn ,
55} from "child_process" ;
6- import { window , WebviewView , ExtensionContext , commands } from "vscode" ;
6+ import { window , WebviewView , DiagnosticCollection , commands , ExtensionContext , languages , Uri , Diagnostic } from "vscode" ;
77import axios from 'axios' ;
88import { GGShieldConfiguration } from "./ggshield-configuration" ;
99import { GGShieldScanResults } from "./api-types" ;
1010import * as os from "os" ;
1111import { apiToDashboard , dasboardToApi } from "../utils" ;
1212import { runGGShieldCommand } from "./run-ggshield" ;
13+ import { StatusBarStatus , updateStatusBarItem } from "../gitguardian-interface/gitguardian-status-bar" ;
14+ import { parseGGShieldResults } from "./ggshield-results-parser" ;
1315
16+ /**
17+ * Extension diagnostic collection
18+ */
19+ let diagnosticCollection : DiagnosticCollection ;
1420
1521/**
1622 * Display API quota
@@ -115,19 +121,38 @@ export function ignoreSecret(
115121 }
116122}
117123
124+ export function createDiagnosticCollection ( context : ExtensionContext ) : void {
125+ diagnosticCollection = languages . createDiagnosticCollection ( "ggshield" ) ;
126+ context . subscriptions . push ( diagnosticCollection ) ;
127+ }
128+
129+ /**
130+ * Clean up file diagnostics
131+ *
132+ * @param fileUri file uri
133+ */
134+ export function cleanUpFileDiagnostics ( fileUri : Uri ) : void {
135+ diagnosticCollection . delete ( fileUri ) ;
136+ }
137+
138+
118139/**
119- * Scan a file using ggshield CLI application
140+ * Scan a file using ggshield
120141 *
121- * Show error messages on failure
142+ * - retrieve configuration
143+ * - scan file using ggshield CLI application
144+ * - parse ggshield results
145+ * - set diagnostics collection so the incdients are visible to the user
122146 *
123147 * @param filePath path to file
124- * @param configuration ggshield configuration
125- * @returns results or undefined if there was an error
148+ * @param fileUri file uri
126149 */
127- export function ggshieldScanFile (
150+ export async function scanFile (
151+ this : any ,
128152 filePath : string ,
153+ fileUri : Uri ,
129154 configuration : GGShieldConfiguration
130- ) : GGShieldScanResults | undefined {
155+ ) : Promise < void > {
131156 const proc = runGGShieldCommand ( configuration , [
132157 "secret" ,
133158 "scan" ,
@@ -155,9 +180,22 @@ export function ggshieldScanFile(
155180 return undefined ;
156181 }
157182
158- return JSON . parse ( proc . stdout ) ;
183+ const results = JSON . parse ( proc . stdout ) ;
184+ if ( ! results ) {
185+ updateStatusBarItem ( StatusBarStatus . ready ) ;
186+ return ;
187+ }
188+ let incidentsDiagnostics : Diagnostic [ ] = parseGGShieldResults ( results ) ;
189+ if ( incidentsDiagnostics . length !== 0 ) {
190+ updateStatusBarItem ( StatusBarStatus . secretFound ) ;
191+ } else {
192+ updateStatusBarItem ( StatusBarStatus . noSecretFound ) ;
193+ }
194+
195+ diagnosticCollection . set ( fileUri , incidentsDiagnostics ) ;
159196}
160197
198+
161199export async function loginGGShield (
162200 configuration : GGShieldConfiguration ,
163201 outputChannel : any ,
0 commit comments