Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new policy view with login CTA #186

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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(
kopecs marked this conversation as resolved.
Show resolved Hide resolved
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 {}
Loading