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 <shati-patel@github.com>
This commit is contained in:
Elena Tanasoiu
2023-01-31 13:16:27 +00:00
parent 3ba13a32e3
commit 6ebeb2b201

View File

@@ -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<void> => {
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> => {
void extLogger.log("Removing orphaned databases from workspace storage.");
let dbDirs = undefined;