Skip to content

Commit c577bee

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 e3acb99 commit c577bee

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
@@ -35,6 +35,9 @@ describe("SkeletonQueryWizard", () => {
3535
let downloadGitHubDatabaseSpy: jest.SpiedFunction<
3636
typeof databaseFetcher.downloadGitHubDatabase
3737
>;
38+
let askForGitHubRepoSpy: jest.SpiedFunction<
39+
typeof databaseFetcher.askForGitHubRepo
40+
>;
3841
let openTextDocumentSpy: jest.SpiedFunction<
3942
typeof workspace.openTextDocument
4043
>;
@@ -43,6 +46,8 @@ describe("SkeletonQueryWizard", () => {
4346
const credentials = testCredentialsWithStub();
4447
const chosenLanguage = "ruby";
4548

49+
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
50+
4651
beforeEach(async () => {
4752
mockCli = mockedObject<CodeQLCliServer>({
4853
resolveLanguages: jest
@@ -107,6 +112,10 @@ describe("SkeletonQueryWizard", () => {
107112
mockDatabaseManager,
108113
token,
109114
);
115+
116+
askForGitHubRepoSpy = jest
117+
.spyOn(databaseFetcher, "askForGitHubRepo")
118+
.mockResolvedValue(QUERY_LANGUAGE_TO_DATABASE_REPO[chosenLanguage]);
110119
});
111120

112121
afterEach(async () => {
@@ -261,10 +270,30 @@ describe("SkeletonQueryWizard", () => {
261270
});
262271

263272
describe("if database is missing", () => {
264-
it("should download a new database for language", async () => {
265-
await wizard.execute();
273+
describe("if the user choses to downloaded the suggested database from GitHub", () => {
274+
it("should download a new database for language", async () => {
275+
await wizard.execute();
276+
277+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
278+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
279+
});
280+
});
281+
282+
describe("if the user choses to download a different database from GitHub than the one suggested", () => {
283+
beforeEach(() => {
284+
const chosenGitHubRepo = "pickles-owner/pickles-repo";
285+
286+
askForGitHubRepoSpy = jest
287+
.spyOn(databaseFetcher, "askForGitHubRepo")
288+
.mockResolvedValue(chosenGitHubRepo);
289+
});
290+
291+
it("should download the newly chosen database", async () => {
292+
await wizard.execute();
266293

267-
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
294+
expect(askForGitHubRepoSpy).toHaveBeenCalled();
295+
expect(downloadGitHubDatabaseSpy).toHaveBeenCalled();
296+
});
268297
});
269298
});
270299
});

0 commit comments

Comments
 (0)