Skip to content

Commit 919219c

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 3e3eb0d commit 919219c

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
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
@@ -207,9 +207,17 @@ export class SkeletonQueryWizard {
207207
});
208208

209209
const githubRepoNwo = QUERY_LANGUAGE_TO_DATABASE_REPO[this.language];
210+
const chosenRepo = await databaseFetcher.askForGitHubRepo(
211+
this.progress,
212+
githubRepoNwo,
213+
);
214+
215+
if (!chosenRepo) {
216+
throw new Error("No GitHub repository provided");
217+
}
210218

211219
await databaseFetcher.downloadGitHubDatabase(
212-
githubRepoNwo,
220+
chosenRepo,
213221
this.databaseManager,
214222
this.storagePath,
215223
this.credentials,

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ describe("SkeletonQueryWizard", () => {
3232
let downloadGitHubDatabaseSpy: jest.SpiedFunction<
3333
typeof databaseFetcher.downloadGitHubDatabase
3434
>;
35+
let askForGitHubRepoSpy: jest.SpiedFunction<
36+
typeof databaseFetcher.askForGitHubRepo
37+
>;
3538
let openTextDocumentSpy: jest.SpiedFunction<
3639
typeof workspace.openTextDocument
3740
>;
@@ -59,6 +62,8 @@ describe("SkeletonQueryWizard", () => {
5962
getSupportedLanguages: jest.fn(),
6063
});
6164

65+
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
66+
6267
beforeEach(async () => {
6368
dir = tmp.dirSync({
6469
prefix: "skeleton_query_wizard_",
@@ -102,6 +107,10 @@ describe("SkeletonQueryWizard", () => {
102107
mockDatabaseManager,
103108
token,
104109
);
110+
111+
askForGitHubRepoSpy = jest
112+
.spyOn(databaseFetcher, "askForGitHubRepo")
113+
.mockResolvedValue(QUERY_LANGUAGE_TO_DATABASE_REPO[chosenLanguage]);
105114
});
106115

107116
afterEach(async () => {
@@ -251,10 +260,30 @@ describe("SkeletonQueryWizard", () => {
251260
.mockResolvedValue(undefined);
252261
});
253262

254-
it("should download a new database for language", async () => {
255-
await wizard.execute();
263+
describe("if the user choses to downloaded the suggested database from GitHub", () => {
264+
it("should download a new database for language", async () => {
265+
await wizard.execute();
266+
267+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
268+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
269+
});
270+
});
271+
272+
describe("if the user choses to download a different database from GitHub than the one suggested", () => {
273+
beforeEach(() => {
274+
const chosenGitHubRepo = "pickles-owner/pickles-repo";
275+
276+
askForGitHubRepoSpy = jest
277+
.spyOn(databaseFetcher, "askForGitHubRepo")
278+
.mockResolvedValue(chosenGitHubRepo);
279+
});
280+
281+
it("should download the newly chosen database", async () => {
282+
await wizard.execute();
256283

257-
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
284+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
285+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
286+
});
258287
});
259288
});
260289
});

0 commit comments

Comments
 (0)