From 6ebeb2b201e4feecb00c2a6d238a73983599b723 Mon Sep 17 00:00:00 2001 From: Elena Tanasoiu Date: Tue, 31 Jan 2023 13:16:27 +0000 Subject: [PATCH 1/4] Introduce command to set default Code Tour database We have a codespace template which houses our CodeQL tour: https://github.com/github/codespaces-codeql This contains a repo with a default databases already loaded for the user so that they can start writing queries more quickly. At the moment we're asking the user to manually right click on the database folder ('codeql-tutorial-database') and set it as the current database. We can take this one step further by defining a command that gets triggered when we arrive at the step for setting up the database. The command ("codeQL.setDefaultTourDatabase") will build the URI pointing to our preloaded database and set it as the current one. We initially considered whether we can re-use the setCurrentDatabase command and pass the URI of the database from the codespace itself, but the URI would be hardcoded as: ``` file://0-62/workspaces/codespaces-codeql/codeql-tutorial-database ``` as we can only pass the codeTour extension a command and string parameters. This would have been brittle as the filepath for a codespace might change in the future. Instead we can define a custom tour command ("setDefaultTourDatabase") to look at the current workspace folder and build the path to the database in the CodeQL extension. Co-authored-by: Shati Patel --- extensions/ql-vscode/src/databases-ui.ts | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/extensions/ql-vscode/src/databases-ui.ts b/extensions/ql-vscode/src/databases-ui.ts index 80da6803a98..3fff38e7129 100644 --- a/extensions/ql-vscode/src/databases-ui.ts +++ b/extensions/ql-vscode/src/databases-ui.ts @@ -12,6 +12,7 @@ import { CancellationToken, ThemeIcon, ThemeColor, + workspace, } from "vscode"; import { pathExists, stat, readdir, remove } from "fs-extra"; @@ -218,6 +219,15 @@ export class DatabaseUI extends DisposableObject { }, ), ); + this.push( + commandRunnerWithProgress( + "codeQL.setDefaultTourDatabase", + this.handleSetDefaultTourDatabase, + { + title: "Set Default Database for Codespace CodeQL Tour", + }, + ), + ); this.push( commandRunnerWithProgress( "codeQL.upgradeCurrentDatabase", @@ -348,6 +358,36 @@ export class DatabaseUI extends DisposableObject { } }; + private handleSetDefaultTourDatabase = async ( + progress: ProgressCallback, + token: CancellationToken, + ): Promise => { + try { + if (workspace.workspaceFolders === undefined) { + throw new Error("No workspace folder is open."); + } else { + const codespaceRootFolderUri = workspace.workspaceFolders[0].uri; + + const uri = Uri.parse( + `${codespaceRootFolderUri}/codeql-tutorial-database`, + ); + + let databaseItem = this.databaseManager.findDatabaseItem(uri); + if (databaseItem === undefined) { + databaseItem = await this.databaseManager.openDatabase( + progress, + token, + uri, + ); + } + await this.databaseManager.setCurrentDatabaseItem(databaseItem); + } + } catch (e) { + // rethrow and let this be handled by default error handling. + throw new Error(`Could not set database: ${getErrorMessage(e)}`); + } + }; + handleRemoveOrphanedDatabases = async (): Promise => { void extLogger.log("Removing orphaned databases from workspace storage."); let dbDirs = undefined; From 6257608433f5034137a60a2be98f34e00b6e0a87 Mon Sep 17 00:00:00 2001 From: Elena Tanasoiu Date: Tue, 31 Jan 2023 17:01:15 +0000 Subject: [PATCH 2/4] Add comment to indicate which folder we're pointing to --- extensions/ql-vscode/src/databases-ui.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/ql-vscode/src/databases-ui.ts b/extensions/ql-vscode/src/databases-ui.ts index 3fff38e7129..fa67de4186a 100644 --- a/extensions/ql-vscode/src/databases-ui.ts +++ b/extensions/ql-vscode/src/databases-ui.ts @@ -366,10 +366,10 @@ export class DatabaseUI extends DisposableObject { if (workspace.workspaceFolders === undefined) { throw new Error("No workspace folder is open."); } else { - const codespaceRootFolderUri = workspace.workspaceFolders[0].uri; - + // This specifically refers to the database folder in + // https://github.com/github/codespaces-codeql const uri = Uri.parse( - `${codespaceRootFolderUri}/codeql-tutorial-database`, + `${workspace.workspaceFolders[0].uri}/codeql-tutorial-database`, ); let databaseItem = this.databaseManager.findDatabaseItem(uri); From b6aa41d19b39d74ac3d5c58282e47930698d645c Mon Sep 17 00:00:00 2001 From: Elena Tanasoiu Date: Tue, 31 Jan 2023 17:02:41 +0000 Subject: [PATCH 3/4] Replace undefined check with length check --- extensions/ql-vscode/src/databases-ui.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/databases-ui.ts b/extensions/ql-vscode/src/databases-ui.ts index fa67de4186a..18f1cf190a0 100644 --- a/extensions/ql-vscode/src/databases-ui.ts +++ b/extensions/ql-vscode/src/databases-ui.ts @@ -363,7 +363,7 @@ export class DatabaseUI extends DisposableObject { token: CancellationToken, ): Promise => { try { - if (workspace.workspaceFolders === undefined) { + if (!workspace.workspaceFolders?.length) { throw new Error("No workspace folder is open."); } else { // This specifically refers to the database folder in From 99d794c2a844f16c876b828ad5ddfc881717ee10 Mon Sep 17 00:00:00 2001 From: Elena Tanasoiu Date: Wed, 1 Feb 2023 16:59:22 +0000 Subject: [PATCH 4/4] Update error message Co-authored-by: Andrew Eisenberg --- extensions/ql-vscode/src/databases-ui.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/databases-ui.ts b/extensions/ql-vscode/src/databases-ui.ts index 18f1cf190a0..d0498156ac2 100644 --- a/extensions/ql-vscode/src/databases-ui.ts +++ b/extensions/ql-vscode/src/databases-ui.ts @@ -384,7 +384,11 @@ export class DatabaseUI extends DisposableObject { } } catch (e) { // rethrow and let this be handled by default error handling. - throw new Error(`Could not set database: ${getErrorMessage(e)}`); + throw new Error( + `Could not set the database for the Code Tour. Please make sure you are using the default workspace in your codespace: ${getErrorMessage( + e, + )}`, + ); } };