Skip to content

Commit be267b1

Browse files
committed
Show the user a selection box before downloading database
At the moment, we're always deciding which database to download for the user for an example query. We'd like to give them a chance to change the database, so here we're adding a step where we're showing the user a selection box with the suggested database pre-filled. They can choose to type in a different database before continuing the skeleton generation process.
1 parent a424ec3 commit be267b1

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fetch, { Response } from "node-fetch";
22
import { zip } from "zip-a-folder";
33
import { Open } from "unzipper";
4-
import { Uri, CancellationToken, window } from "vscode";
4+
import { Uri, CancellationToken, window, InputBoxOptions } from "vscode";
55
import { CodeQLCliServer } from "./cli";
66
import {
77
ensureDir,
@@ -92,17 +92,7 @@ export async function promptImportGithubDatabase(
9292
token: CancellationToken,
9393
cli?: CodeQLCliServer,
9494
): Promise<DatabaseItem | undefined> {
95-
progress({
96-
message: "Choose repository",
97-
step: 1,
98-
maxStep: 2,
99-
});
100-
const githubRepo = await window.showInputBox({
101-
title:
102-
'Enter a GitHub repository URL or "name with owner" (e.g. https://github.com/github/codeql or github/codeql)',
103-
placeHolder: "https://github.com/<owner>/<repo> or <owner>/<repo>",
104-
ignoreFocusOut: true,
105-
});
95+
const githubRepo = await askForGitHubRepo(progress);
10696
if (!githubRepo) {
10797
return;
10898
}
@@ -128,6 +118,30 @@ export async function promptImportGithubDatabase(
128118
return;
129119
}
130120

121+
export async function askForGitHubRepo(
122+
progress: ProgressCallback,
123+
suggestedValue?: string,
124+
): Promise<string | undefined> {
125+
progress({
126+
message: "Choose repository",
127+
step: 1,
128+
maxStep: 2,
129+
});
130+
131+
const options: InputBoxOptions = {
132+
title:
133+
'Enter a GitHub repository URL or "name with owner" (e.g. https://github.com/github/codeql or github/codeql)',
134+
placeHolder: "https://github.com/<owner>/<repo> or <owner>/<repo>",
135+
ignoreFocusOut: true,
136+
};
137+
138+
if (suggestedValue) {
139+
options.value = suggestedValue;
140+
}
141+
142+
return await window.showInputBox(options);
143+
}
144+
131145
/**
132146
* Downloads a database from GitHub
133147
*

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,17 @@ export class SkeletonQueryWizard {
201201
});
202202

203203
const githubRepoNwo = this.QUERY_LANGUAGE_TO_DATABASE_REPO[this.language];
204+
const chosenRepo = await databaseFetcher.askForGitHubRepo(
205+
this.progress,
206+
githubRepoNwo,
207+
);
208+
209+
if (!chosenRepo) {
210+
throw new Error("No GitHub repository provided");
211+
}
204212

205213
await databaseFetcher.downloadGitHubDatabase(
206-
githubRepoNwo,
214+
chosenRepo,
207215
this.databaseManager,
208216
this.storagePath,
209217
this.credentials,

extensions/ql-vscode/test/vscode-tests/cli-integration/skeleton-query-wizard.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ describe("SkeletonQueryWizard", () => {
2929
let createExampleQlFileSpy: jest.SpiedFunction<
3030
typeof QlPackGenerator.prototype.createExampleQlFile
3131
>;
32-
let chosenLanguage: string;
3332
let downloadGitHubDatabaseSpy: jest.SpiedFunction<
3433
typeof databaseFetcher.downloadGitHubDatabase
3534
>;
35+
let askForGitHubRepoSpy: jest.SpiedFunction<
36+
typeof databaseFetcher.askForGitHubRepo
37+
>;
3638
let openTextDocumentSpy: jest.SpiedFunction<
3739
typeof workspace.openTextDocument
3840
>;
41+
let chosenLanguage: string;
42+
let suggestedGitHubRepo: string;
3943

4044
beforeEach(async () => {
4145
dir = tmp.dirSync({
@@ -107,6 +111,13 @@ describe("SkeletonQueryWizard", () => {
107111
mockDatabaseManager,
108112
token,
109113
);
114+
115+
suggestedGitHubRepo =
116+
wizard.QUERY_LANGUAGE_TO_DATABASE_REPO[chosenLanguage];
117+
118+
askForGitHubRepoSpy = jest
119+
.spyOn(databaseFetcher, "askForGitHubRepo")
120+
.mockResolvedValue(suggestedGitHubRepo);
110121
});
111122

112123
afterEach(() => {
@@ -293,10 +304,30 @@ describe("SkeletonQueryWizard", () => {
293304
.mockResolvedValue(undefined);
294305
});
295306

296-
it("should download a new database for language", async () => {
297-
await wizard.execute();
307+
describe("if the user choses to downloaded the suggested database from GitHub", () => {
308+
it("should download a new database for language", async () => {
309+
await wizard.execute();
310+
311+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
312+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
313+
});
314+
});
315+
316+
describe("if the user choses to download a different database from GitHub than the one suggested", () => {
317+
beforeEach(() => {
318+
const chosenGitHubRepo = "pickles-owner/pickles-repo";
319+
320+
askForGitHubRepoSpy = jest
321+
.spyOn(databaseFetcher, "askForGitHubRepo")
322+
.mockResolvedValue(chosenGitHubRepo);
323+
});
324+
325+
it("should download the newly chosen database", async () => {
326+
await wizard.execute();
298327

299-
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
328+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
329+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
330+
});
300331
});
301332
});
302333
});

0 commit comments

Comments
 (0)