Skip to content

Commit 15a8655

Browse files
committed
Do not prompt for database download by default
This changes the skeleton query wizard to not prompt for database download after creating a query by default. Instead, it will show a message with a button to download a database which will launch the same prompt.
1 parent fb33879 commit 15a8655

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

extensions/ql-vscode/src/local-queries/skeleton-query-wizard.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { join } from "path";
2-
import { Uri, workspace, window as Window } from "vscode";
2+
import { Uri, window as Window, window, workspace } from "vscode";
33
import { CodeQLCliServer } from "../codeql-cli/cli";
44
import { BaseLogger } from "../common/logging";
55
import { Credentials } from "../common/authentication";
@@ -14,6 +14,7 @@ import { DatabaseItem, DatabaseManager } from "../databases/local-databases";
1414
import {
1515
ProgressCallback,
1616
UserCancellationException,
17+
withProgress,
1718
} from "../common/vscode/progress";
1819
import {
1920
askForGitHubRepo,
@@ -26,6 +27,7 @@ import {
2627
} from "../config";
2728
import { existsSync } from "fs-extra";
2829
import { askForLanguage } from "../codeql-cli/query-language";
30+
import { showInformationMessageWithAction } from "../common/vscode/dialog";
2931

3032
type QueryLanguagesToDatabaseMap = Record<string, string>;
3133

@@ -105,7 +107,9 @@ export class SkeletonQueryWizard {
105107
);
106108

107109
void workspace.openTextDocument(queryFileUri).then((doc) => {
108-
void Window.showTextDocument(doc);
110+
void Window.showTextDocument(doc, {
111+
preview: false,
112+
});
109113
});
110114
}
111115

@@ -227,7 +231,24 @@ export class SkeletonQueryWizard {
227231
return `example${qlFiles.length + 1}.ql`;
228232
}
229233

230-
private async downloadDatabase() {
234+
private async promptDownloadDatabase() {
235+
if (this.qlPackStoragePath === undefined) {
236+
throw new Error("QL Pack storage path is undefined");
237+
}
238+
239+
const openFileLink = this.openFileMarkdownLink;
240+
241+
const action = await showInformationMessageWithAction(
242+
`New CodeQL query for ${this.language} ${openFileLink} created, but no CodeQL databases for ${this.language} were detected in your workspace. Would you like to download a CodeQL database for ${this.language} to analyze with ${openFileLink}?`,
243+
"Download database",
244+
);
245+
246+
if (action) {
247+
void withProgress((progress) => this.downloadDatabase(progress));
248+
}
249+
}
250+
251+
private async downloadDatabase(progress: ProgressCallback) {
231252
if (this.qlPackStoragePath === undefined) {
232253
throw new Error("QL Pack storage path is undefined");
233254
}
@@ -240,10 +261,10 @@ export class SkeletonQueryWizard {
240261
throw new Error("Language is undefined");
241262
}
242263

243-
this.progress({
264+
progress({
244265
message: "Downloading database",
245-
step: 3,
246-
maxStep: 3,
266+
step: 1,
267+
maxStep: 2,
247268
});
248269

249270
const githubRepoNwo = QUERY_LANGUAGE_TO_DATABASE_REPO[this.language];
@@ -258,7 +279,7 @@ export class SkeletonQueryWizard {
258279
this.databaseManager,
259280
this.databaseStoragePath,
260281
this.credentials,
261-
this.progress,
282+
progress,
262283
this.cliServer,
263284
this.language,
264285
);
@@ -280,12 +301,32 @@ export class SkeletonQueryWizard {
280301
);
281302

282303
if (existingDatabaseItem) {
283-
// select the found database
284-
await this.databaseManager.setCurrentDatabaseItem(existingDatabaseItem);
304+
const openFileLink = this.openFileMarkdownLink;
305+
306+
if (this.databaseManager.currentDatabaseItem !== existingDatabaseItem) {
307+
// select the found database
308+
await this.databaseManager.setCurrentDatabaseItem(existingDatabaseItem);
309+
310+
void window.showInformationMessage(
311+
`New CodeQL query for ${this.language} ${openFileLink} created. We have automatically selected your existing CodeQL ${this.language} database ${existingDatabaseItem.name} for you to analyze with ${openFileLink}.`,
312+
);
313+
}
285314
} else {
286315
// download new database and select it
287-
await this.downloadDatabase();
316+
void this.promptDownloadDatabase();
317+
}
318+
}
319+
320+
private get openFileMarkdownLink() {
321+
if (this.qlPackStoragePath === undefined) {
322+
throw new Error("QL Pack storage path is undefined");
288323
}
324+
325+
const openFileArgs = [
326+
join(this.qlPackStoragePath, this.folderName, this.fileName),
327+
];
328+
const queryString = encodeURI(JSON.stringify(openFileArgs));
329+
return `[${this.fileName}](command:vscode.open?${queryString})`;
289330
}
290331

291332
public static async findDatabaseItemByNwo(

0 commit comments

Comments
 (0)