Skip to content

Commit

Permalink
feat: new policy view with login/add config CTA
Browse files Browse the repository at this point in the history
This will show a prompt to login or add a local configuration if none is
present. If a configuration is present and/or the user is logged in, it
will show the applied configuration.
  • Loading branch information
kopecs committed Feb 6, 2025
1 parent 984146f commit 50ded7c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,19 @@
}
]
},
"viewsWelcome": [
{
"view": "semgrep.view.policy",
"contents": "[Sign in](command:semgrep.login)",
"when": "!semgrep.loggedIn"
}
],
"views": {
"semgrep-sidebar": [
{
"id": "semgrep.view.policy",
"name": "Policy Configuration"
},
{
"type": "webview",
"id": "semgrep.searchView",
Expand Down
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ export class Environment {
setSentryContext(this);
}

loginEvent?: vscode.EventEmitter<void> = undefined;

get loggedIn(): boolean {
return this.context.globalState.get("loggedIn", false);
}

set loggedIn(val: boolean) {
vscode.commands.executeCommand("setContext", "semgrep.loggedIn", val);
if (this.loginEvent) {
this.loginEvent.fire();
}
this.context.globalState.update("loggedIn", val);
}

Expand Down
11 changes: 9 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from "vscode";

import type { ConfigurationChangeEvent, ExtensionContext } from "vscode";
import { registerCommands } from "./commands";
import { VSCODE_CONFIG_KEY } from "./constants";
Expand All @@ -7,6 +8,7 @@ import { activateLsp, deactivateLsp, restartLsp } from "./lsp";
import { SemgrepDocumentProvider } from "./showAstDocument";
import { createStatusBar } from "./statusBar";
import { initTelemetry, stopTelemetry } from "./telemetry/telemetry";
import { SemgrepPolicyViewProvider } from "./views/policy";
import { SemgrepHelpProvider } from "./views/support";
import { SemgrepSearchWebviewProvider } from "./views/webview";

Expand Down Expand Up @@ -35,8 +37,7 @@ async function afterClientStart(context: ExtensionContext, env: Environment) {
return;
}
const statusBar = createStatusBar();
context.subscriptions.push(statusBar);
registerCommands(env).forEach((d) => context.subscriptions.push(d));
context.subscriptions.push(statusBar, ...registerCommands(env));
statusBar.show();

// register stuff for search webview
Expand All @@ -60,6 +61,12 @@ async function afterClientStart(context: ExtensionContext, env: Environment) {
),
),
);
context.subscriptions.push(
vscode.window.registerTreeDataProvider(
SemgrepPolicyViewProvider.viewType,
new SemgrepPolicyViewProvider(context.extensionUri, env),
),
);

// register content provider for the AST showing document
context.subscriptions.push(
Expand Down
11 changes: 7 additions & 4 deletions src/lsp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import cp from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import * as cp from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
import * as semver from "semver";
import * as vscode from "vscode";
import {
type Executable,
LanguageClient,
type LanguageClientOptions,
MessageType,

Check warning on line 10 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'MessageType' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 10 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'MessageType' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 10 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'MessageType' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 10 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'MessageType' is defined but never used. Allowed unused vars must match /^_/u
type NotificationHandler,

Check warning on line 11 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'NotificationHandler' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 11 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'NotificationHandler' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 11 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'NotificationHandler' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 11 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'NotificationHandler' is defined but never used. Allowed unused vars must match /^_/u
type ServerOptions,
ShowMessageNotification,

Check warning on line 13 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'ShowMessageNotification' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 13 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'ShowMessageNotification' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 13 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'ShowMessageNotification' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 13 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'ShowMessageNotification' is defined but never used. Allowed unused vars must match /^_/u
ShowMessageParams,

Check warning on line 14 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'ShowMessageParams' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'ShowMessageParams' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (macos-latest)

'ShowMessageParams' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in src/lsp.ts

View workflow job for this annotation

GitHub Actions / vsce-test (ubuntu-latest)

'ShowMessageParams' is defined but never used. Allowed unused vars must match /^_/u
TransportKind,
} from "vscode-languageclient/node";
import type { NotificationHandler0 } from "vscode-languageserver";
Expand Down Expand Up @@ -246,7 +250,6 @@ async function start(env: Environment): Promise<void> {
env.logger.log("Rules loaded");
env.emitRulesRefreshedEvent();
};

// Register handlers here
c.onNotification(rulesRefreshed, notificationHandler);
c.onTelemetry((e) => {
Expand Down
43 changes: 43 additions & 0 deletions src/views/policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as vscode from "vscode";
import type { Environment } from "../env";

export class SemgrepPolicyViewProvider
implements vscode.TreeDataProvider<PolicyItem>
{
public static readonly viewType = "semgrep.view.policy";

// [ + add more? ]
// root
// \ connect to org (log in) OR <ORG NAME>'s policy
constructor(
private readonly extensionUri: vscode.Uri,
private readonly env: Environment,
) {
env.loginEvent = this._onDidChangeTreeData;
}

getTreeItem(element: PolicyItem): PolicyItem {
return element;
}

getChildren(
element?: PolicyItem | undefined,
): vscode.ProviderResult<PolicyItem[]> {
if (!element) {
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];
}
return [];
}
return [];
}

private _onDidChangeTreeData: vscode.EventEmitter<void> =
new vscode.EventEmitter<void>();
readonly onDidChangeTreeData: vscode.Event<void> =
this._onDidChangeTreeData.event;
}

class PolicyItem extends vscode.TreeItem {}

0 comments on commit 50ded7c

Please sign in to comment.