@@ -3,14 +3,20 @@ import {
3
3
SpawnOptionsWithoutStdio ,
4
4
spawn ,
5
5
} from "child_process" ;
6
- import { window , WebviewView , ExtensionContext , commands } from "vscode" ;
6
+ import { window , WebviewView , DiagnosticCollection , commands , ExtensionContext , languages , Uri , Diagnostic } from "vscode" ;
7
7
import axios from 'axios' ;
8
8
import { GGShieldConfiguration } from "./ggshield-configuration" ;
9
9
import { GGShieldScanResults } from "./api-types" ;
10
10
import * as os from "os" ;
11
11
import { apiToDashboard , dasboardToApi } from "../utils" ;
12
12
import { runGGShieldCommand } from "./run-ggshield" ;
13
+ import { StatusBarStatus , updateStatusBarItem } from "../gitguardian-interface/gitguardian-status-bar" ;
14
+ import { parseGGShieldResults } from "./ggshield-results-parser" ;
13
15
16
+ /**
17
+ * Extension diagnostic collection
18
+ */
19
+ let diagnosticCollection : DiagnosticCollection ;
14
20
15
21
/**
16
22
* Display API quota
@@ -115,19 +121,38 @@ export function ignoreSecret(
115
121
}
116
122
}
117
123
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
+
118
139
/**
119
- * Scan a file using ggshield CLI application
140
+ * Scan a file using ggshield
120
141
*
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
122
146
*
123
147
* @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
126
149
*/
127
- export function ggshieldScanFile (
150
+ export async function scanFile (
151
+ this : any ,
128
152
filePath : string ,
153
+ fileUri : Uri ,
129
154
configuration : GGShieldConfiguration
130
- ) : GGShieldScanResults | undefined {
155
+ ) : Promise < void > {
131
156
const proc = runGGShieldCommand ( configuration , [
132
157
"secret" ,
133
158
"scan" ,
@@ -155,9 +180,22 @@ export function ggshieldScanFile(
155
180
return undefined ;
156
181
}
157
182
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 ) ;
159
196
}
160
197
198
+
161
199
export async function loginGGShield (
162
200
configuration : GGShieldConfiguration ,
163
201
outputChannel : any ,
0 commit comments