Skip to content

Commit 1fa3bda

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 2ee07bb commit 1fa3bda

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-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
@@ -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: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ describe("SkeletonQueryWizard", () => {
2727
let downloadGitHubDatabaseSpy: jest.SpiedFunction<
2828
typeof databaseFetcher.downloadGitHubDatabase
2929
>;
30+
let askForGitHubRepoSpy: jest.SpiedFunction<
31+
typeof databaseFetcher.askForGitHubRepo
32+
>;
3033
let openTextDocumentSpy: jest.SpiedFunction<
3134
typeof workspace.openTextDocument
3235
>;
@@ -54,6 +57,8 @@ describe("SkeletonQueryWizard", () => {
5457
getSupportedLanguages: jest.fn(),
5558
});
5659

60+
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
61+
5762
beforeEach(async () => {
5863
dir = tmp.dirSync({
5964
prefix: "skeleton_query_wizard_",
@@ -97,6 +102,12 @@ describe("SkeletonQueryWizard", () => {
97102
mockDatabaseManager,
98103
token,
99104
);
105+
106+
askForGitHubRepoSpy = jest
107+
.spyOn(databaseFetcher, "askForGitHubRepo")
108+
.mockResolvedValue(
109+
wizard.QUERY_LANGUAGE_TO_DATABASE_REPO[chosenLanguage],
110+
);
100111
});
101112

102113
afterEach(async () => {
@@ -236,10 +247,30 @@ describe("SkeletonQueryWizard", () => {
236247
.mockResolvedValue(undefined);
237248
});
238249

239-
it("should download a new database for language", async () => {
240-
await wizard.execute();
250+
describe("if the user choses to downloaded the suggested database from GitHub", () => {
251+
it("should download a new database for language", async () => {
252+
await wizard.execute();
253+
254+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
255+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
256+
});
257+
});
258+
259+
describe("if the user choses to download a different database from GitHub than the one suggested", () => {
260+
beforeEach(() => {
261+
const chosenGitHubRepo = "pickles-owner/pickles-repo";
262+
263+
askForGitHubRepoSpy = jest
264+
.spyOn(databaseFetcher, "askForGitHubRepo")
265+
.mockResolvedValue(chosenGitHubRepo);
266+
});
267+
268+
it("should download the newly chosen database", async () => {
269+
await wizard.execute();
241270

242-
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
271+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
272+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
273+
});
243274
});
244275
});
245276
});

0 commit comments

Comments
 (0)