Skip to content

Commit

Permalink
feat: policy configuration for local config (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
kopecs authored Feb 6, 2025
1 parent 720c788 commit 47fecb6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
44 changes: 39 additions & 5 deletions src/views/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ export class SemgrepPolicyViewProvider
// [ + add more? ]
// root
// \ connect to org (log in) OR <ORG NAME>'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 {
Expand All @@ -24,12 +31,31 @@ export class SemgrepPolicyViewProvider
element?: PolicyItem | undefined,
): vscode.ProviderResult<PolicyItem[]> {
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<string[]>("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 [];
}
Expand All @@ -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);
}
}

0 comments on commit 47fecb6

Please sign in to comment.