Skip to content

Commit

Permalink
feat: consume new login flow (#189)
Browse files Browse the repository at this point in the history
This should let us avoid stale state caused by the prior implementation,
which handled the "finish" step as a notification, rather than as a
request.
  • Loading branch information
kopecs authored Jan 10, 2025
1 parent fc1633d commit 5cfd521
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Environment } from "./env";
import { restartLsp } from "./lsp";
import {
type SearchParams,
login,
loginFinish,
loginStart,
loginStatus,
logout,
refreshRules,
Expand Down Expand Up @@ -61,10 +61,13 @@ export function registerCommands(env: Environment): Disposable[] {
/************/

vscode.commands.registerCommand("semgrep.login", async () => {
const result = await env.client?.sendRequest(login);
const result = await env.client?.sendRequest(loginStart);
if (result) {
vscode.env.openExternal(vscode.Uri.parse(result.url));
env.client?.sendNotification(loginFinish, result);
const status = await env.client?.sendRequest(loginFinish, result);
if (status) {
env.loggedIn = status.loggedIn;
}
}
}),

Expand Down
23 changes: 13 additions & 10 deletions src/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export const scanWorkspace = new lc.NotificationType<ScanWorkspaceParams>(
"semgrep/scanWorkspace",
);

export interface LoginParams {
export interface LoginStartResponse {
url: string;
sessionId: string;
}

export interface LoginStatusParams {
export interface LoginStatusResponse {
loggedIn: boolean;
}

Expand Down Expand Up @@ -55,13 +55,15 @@ export interface LspErrorParams {
stack: string;
}

export const login = new lc.RequestType0<LoginParams | null, void>(
"semgrep/login",
export const loginStart = new lc.RequestType0<LoginStartResponse | null, void>(
"semgrep/loginStart",
);

export const loginFinish = new lc.NotificationType<LoginParams>(
"semgrep/loginFinish",
);
export const loginFinish = new lc.RequestType<
LoginStartResponse,
LoginStatusResponse,
void
>("semgrep/loginFinish");

export const logout = new lc.NotificationType("semgrep/logout");

Expand All @@ -75,9 +77,10 @@ export const workspaceRules = new lc.RequestType0<any[], void>(
"semgrep/workspaceRules",
);

export const loginStatus = new lc.RequestType0<LoginStatusParams | null, void>(
"semgrep/loginStatus",
);
export const loginStatus = new lc.RequestType0<
LoginStatusResponse | null,
void
>("semgrep/loginStatus");

export const search = new lc.RequestType<LspSearchParams, SearchResults, void>(
"semgrep/search",
Expand Down

0 comments on commit 5cfd521

Please sign in to comment.