diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a4416..ad9dd7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - New sidebar section with support information (email and slack contact; documentation) +- New sidebar section with scan config/policy information ### Changed diff --git a/package.json b/package.json index 2f01b70..eb0920e 100644 --- a/package.json +++ b/package.json @@ -220,8 +220,8 @@ "viewsWelcome": [ { "view": "semgrep.view.policy", - "contents": "[Sign in](command:semgrep.login)", - "when": "!semgrep.loggedIn" + "contents": "Using Semgrep with your organization? \n[Sign in](command:semgrep.login)\n or [configure a local policy](command:workbench.action.openSettings?%22semgrep.scan.configuration%22) in the settings", + "when": "!semgrep.loggedIn && (config.semgrep.scan.configuration.length === 0)" } ], "views": { diff --git a/src/views/policy.ts b/src/views/policy.ts index f4b4dd1..84f93de 100644 --- a/src/views/policy.ts +++ b/src/views/policy.ts @@ -9,11 +9,18 @@ export class SemgrepPolicyViewProvider // [ + add more? ] // root // \ connect to org (log in) OR 's policy + // \ ...items from config constructor( private readonly extensionUri: vscode.Uri, private readonly env: Environment, ) { env.loginEvent = this._onDidChangeTreeData; + // Also refresh when configuration changes + vscode.workspace.onDidChangeConfiguration((e) => { + if (e.affectsConfiguration("semgrep.scan.configuration")) { + this._onDidChangeTreeData.fire(); + } + }); } getTreeItem(element: PolicyItem): PolicyItem { @@ -24,12 +31,31 @@ export class SemgrepPolicyViewProvider element?: PolicyItem | undefined, ): vscode.ProviderResult { if (!element) { + const items: PolicyItem[] = []; + + // Show org policy if logged in if (this.env.loggedIn) { - const login_status = new PolicyItem("Using your organization's policy"); - login_status.iconPath = new vscode.ThemeIcon("cloud-download"); - return [login_status]; + const loginStatus = new PolicyItem( + "Using your organization's policy", + vscode.TreeItemCollapsibleState.None, + ); + loginStatus.iconPath = new vscode.ThemeIcon("cloud-download"); + items.push(loginStatus); } - return []; + + // Show local configurations if any exist + const localConfigs = + this.env.config.cfg.get("scan.configuration") || []; + for (const config of localConfigs) { + const configItem = new PolicyItem( + config, + vscode.TreeItemCollapsibleState.None, + ); + configItem.iconPath = new vscode.ThemeIcon("file-code"); + items.push(configItem); + } + + return items; } return []; } @@ -40,4 +66,12 @@ export class SemgrepPolicyViewProvider this._onDidChangeTreeData.event; } -class PolicyItem extends vscode.TreeItem {} +class PolicyItem extends vscode.TreeItem { + constructor( + label: string, + collapsibleState: vscode.TreeItemCollapsibleState = vscode + .TreeItemCollapsibleState.None, + ) { + super(label, collapsibleState); + } +}