11import {
2+ cleanUpFileDiagnostics ,
3+ createDiagnosticCollection ,
24 ggshieldApiKey ,
35 ggshieldAuthStatus ,
4- ggshieldScanFile ,
56 ignoreLastFound ,
67 ignoreSecret ,
78 loginGGShield ,
89 logoutGGShield ,
10+ scanFile ,
911 showAPIQuota ,
1012} from "./lib/ggshield-api" ;
1113import {
1214 getConfiguration ,
13- GGShieldConfiguration ,
1415 setApiKey ,
1516} from "./lib/ggshield-configuration" ;
16- import { parseGGShieldResults } from "./lib/ggshield-results-parser" ;
1717import {
18- Diagnostic ,
19- DiagnosticCollection ,
2018 ExtensionContext ,
2119 Uri ,
2220 commands ,
2321 languages ,
2422 window ,
2523 workspace ,
26- StatusBarItem ,
27- StatusBarAlignment ,
2824 WebviewView ,
2925} from "vscode" ;
3026import { GGShieldResolver } from "./lib/ggshield-resolver" ;
3127import { getCurrentFile , isGitInstalled } from "./utils" ;
3228import { 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" ;
3430import {
3531 generateSecretName ,
3632 GitGuardianSecretHoverProvider ,
3733} from "./gitguardian-interface/gitguardian-hover-provider" ;
3834import { GitGuardianQuotaWebviewProvider } from "./ggshield-webview/gitguardian-quota-webview" ;
3935import { GitGuardianRemediationMessageWebviewProvider } from "./ggshield-webview/gitguardian-remediation-message-view" ;
4036
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- }
8637
8738function registerOpenViewsCommands (
8839 context : ExtensionContext ,
@@ -162,13 +113,11 @@ export function activate(context: ExtensionContext) {
162113 ) ;
163114 context . subscriptions . push ( ggshieldViewProvider , ggshieldRemediationMessageViewProvider , ggshieldQuotaViewProvider ) ;
164115
165- statusBar = window . createStatusBarItem ( StatusBarAlignment . Left , 0 ) ;
166- updateStatusBarItem ( StatusBarStatus . initialization , statusBar ) ;
116+ createStatusBarItem ( context ) ;
167117
168118 //generic commands to open correct view on status bar click
169119 registerOpenViewsCommands ( context , outputChannel ) ;
170120 registerQuotaViewCommands ( ggshieldQuotaViewProvider ) ;
171- context . subscriptions . push ( statusBar ) ;
172121
173122 context . subscriptions . push (
174123 languages . registerHoverProvider ( "*" , new GitGuardianSecretHoverProvider ( ) )
@@ -180,14 +129,14 @@ export function activate(context: ExtensionContext) {
180129 // Check if ggshield is authenticated
181130 ggshieldAuthStatus ( configuration , context ) ;
182131 if ( context . globalState . get ( "isAuthenticated" , false ) ) {
183- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
132+ updateStatusBarItem ( StatusBarStatus . ready ) ;
184133 setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
185134 ggshieldViewProvider . refresh ( ) ;
186135 ggshieldRemediationMessageViewProvider . refresh ( ) ;
187136 ggshieldQuotaViewProvider . refresh ( ) ;
188137
189138 } else {
190- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
139+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
191140 }
192141 } )
193142 . then ( async ( ) => {
@@ -203,9 +152,7 @@ export function activate(context: ExtensionContext) {
203152 . then ( ( ) => {
204153 // Start scanning documents on activation events
205154 // (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 ) ;
209156 context . subscriptions . push (
210157 workspace . onDidSaveTextDocument ( ( textDocument ) => {
211158 // Check if the document is inside the workspace
@@ -249,7 +196,7 @@ export function activate(context: ExtensionContext) {
249196 scanFile (
250197 currentFile ,
251198 Uri . file ( currentFile ) ,
252- ggshieldResolver . configuration
199+ ggshieldResolver . configuration ,
253200 ) ;
254201 }
255202 ) ,
@@ -262,10 +209,10 @@ export function activate(context: ExtensionContext) {
262209 context
263210 ) . then ( ( ) => {
264211 if ( context . globalState . get ( "isAuthenticated" , false ) ) {
265- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
212+ updateStatusBarItem ( StatusBarStatus . ready ) ;
266213 setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
267214 } else {
268- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
215+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
269216 }
270217 ggshieldViewProvider . refresh ( ) ;
271218 ggshieldRemediationMessageViewProvider . refresh ( ) ;
@@ -276,7 +223,7 @@ export function activate(context: ExtensionContext) {
276223 } ) ,
277224 commands . registerCommand ( "gitguardian.logout" , async ( ) => {
278225 logoutGGShield ( ggshieldResolver . configuration , context ) ;
279- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
226+ updateStatusBarItem ( StatusBarStatus . unauthenticated ) ;
280227 setApiKey ( configuration , undefined ) ;
281228 ggshieldViewProvider . refresh ( ) ;
282229 ggshieldRemediationMessageViewProvider . refresh ( ) ;
@@ -286,13 +233,8 @@ export function activate(context: ExtensionContext) {
286233 } )
287234 . catch ( ( error ) => {
288235 outputChannel . appendLine ( `Error: ${ error . message } ` ) ;
289- updateStatusBarItem ( StatusBarStatus . error , statusBar ) ;
236+ updateStatusBarItem ( StatusBarStatus . error ) ;
290237 } ) ;
291238}
292239
293- export function deactivate ( ) {
294- if ( diagnosticCollection ) {
295- diagnosticCollection . dispose ( ) ;
296- statusBar . dispose ( ) ;
297- }
298- }
240+ export function deactivate ( ) { }
0 commit comments