Skip to content

Commit ddf5b3a

Browse files
Convert extensions/ql-vscode/src/databaseFetcher.ts to call typed commands
1 parent 13c3c8e commit ddf5b3a

6 files changed

Lines changed: 20 additions & 4 deletions

File tree

extensions/ql-vscode/src/common/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type SingleSelectionCommandFunction<Item> = (
3535
// Builtin commands where the implementation is provided by VS Code and not by this extension.
3636
// See https://code.visualstudio.com/api/references/commands
3737
export type BuiltInVsCodeCommands = {
38+
"codeQLDatabases.focus": () => Promise<void>;
3839
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
3940
setContext: (
4041
key: `${"codeql" | "codeQL"}${string}`,

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 8 additions & 4 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, commands, window } from "vscode";
4+
import { Uri, CancellationToken, window } from "vscode";
55
import { CodeQLCliServer } from "./cli";
66
import {
77
ensureDir,
@@ -26,6 +26,7 @@ import {
2626
isValidGitHubNwo,
2727
} from "./common/github-url-identifier-helper";
2828
import { Credentials } from "./common/authentication";
29+
import { AppCommandManager } from "./common/commands";
2930

3031
/**
3132
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
@@ -34,6 +35,7 @@ import { Credentials } from "./common/authentication";
3435
* @param storagePath where to store the unzipped database.
3536
*/
3637
export async function promptImportInternetDatabase(
38+
commandManager: AppCommandManager,
3739
databaseManager: DatabaseManager,
3840
storagePath: string,
3941
progress: ProgressCallback,
@@ -61,7 +63,7 @@ export async function promptImportInternetDatabase(
6163
);
6264

6365
if (item) {
64-
await commands.executeCommand("codeQLDatabases.focus");
66+
await commandManager.execute("codeQLDatabases.focus");
6567
void showAndLogInformationMessage(
6668
"Database downloaded and imported successfully.",
6769
);
@@ -78,6 +80,7 @@ export async function promptImportInternetDatabase(
7880
* @param storagePath where to store the unzipped database.
7981
*/
8082
export async function promptImportGithubDatabase(
83+
commandManager: AppCommandManager,
8184
databaseManager: DatabaseManager,
8285
storagePath: string,
8386
credentials: Credentials | undefined,
@@ -141,7 +144,7 @@ export async function promptImportGithubDatabase(
141144
cli,
142145
);
143146
if (item) {
144-
await commands.executeCommand("codeQLDatabases.focus");
147+
await commandManager.execute("codeQLDatabases.focus");
145148
void showAndLogInformationMessage(
146149
"Database downloaded and imported successfully.",
147150
);
@@ -158,6 +161,7 @@ export async function promptImportGithubDatabase(
158161
* @param storagePath where to store the unzipped database.
159162
*/
160163
export async function importArchiveDatabase(
164+
commandManager: AppCommandManager,
161165
databaseUrl: string,
162166
databaseManager: DatabaseManager,
163167
storagePath: string,
@@ -177,7 +181,7 @@ export async function importArchiveDatabase(
177181
cli,
178182
);
179183
if (item) {
180-
await commands.executeCommand("codeQLDatabases.focus");
184+
await commandManager.execute("codeQLDatabases.focus");
181185
void showAndLogInformationMessage(
182186
"Database unzipped and imported successfully.",
183187
);

extensions/ql-vscode/src/local-databases-ui.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ export class DatabaseUI extends DisposableObject {
451451
return withProgress(
452452
async (progress, token) => {
453453
await promptImportInternetDatabase(
454+
this.app.commands,
454455
this.databaseManager,
455456
this.storagePath,
456457
progress,
@@ -470,6 +471,7 @@ export class DatabaseUI extends DisposableObject {
470471
const credentials = isCanary() ? this.app.credentials : undefined;
471472

472473
await promptImportGithubDatabase(
474+
this.app.commands,
473475
this.databaseManager,
474476
this.storagePath,
475477
credentials,
@@ -607,6 +609,7 @@ export class DatabaseUI extends DisposableObject {
607609
// Assume user has selected an archive if the file has a .zip extension
608610
if (uri.path.endsWith(".zip")) {
609611
await importArchiveDatabase(
612+
this.app.commands,
610613
uri.toString(true),
611614
this.databaseManager,
612615
this.storagePath,
@@ -762,6 +765,7 @@ export class DatabaseUI extends DisposableObject {
762765
// we are selecting a database archive. Must unzip into a workspace-controlled area
763766
// before importing.
764767
return await importArchiveDatabase(
768+
this.app.commands,
765769
uri.toString(true),
766770
this.databaseManager,
767771
this.storagePath,

extensions/ql-vscode/test/vscode-tests/cli-integration/databaseFetcher.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
promptImportInternetDatabase,
1010
} from "../../../src/databaseFetcher";
1111
import { cleanDatabases, dbLoc, DB_URL, storagePath } from "../global.helper";
12+
import { createMockCommandManager } from "../../__mocks__/commandsMock";
1213

1314
jest.setTimeout(60_000);
1415

@@ -53,6 +54,7 @@ describe("DatabaseFetcher", () => {
5354
it("should add a database from a folder", async () => {
5455
const uri = Uri.file(dbLoc);
5556
let dbItem = await importArchiveDatabase(
57+
createMockCommandManager(),
5658
uri.toString(true),
5759
databaseManager,
5860
storagePath,
@@ -75,6 +77,7 @@ describe("DatabaseFetcher", () => {
7577
inputBoxStub.mockResolvedValue(DB_URL);
7678

7779
let dbItem = await promptImportInternetDatabase(
80+
createMockCommandManager(),
7881
databaseManager,
7982
storagePath,
8083
progressCallback,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { extLogger, ProgressReporter } from "../../../src/common";
1313
import { QueryResultType } from "../../../src/pure/new-messages";
1414
import { cleanDatabases, dbLoc, storagePath } from "../global.helper";
1515
import { importArchiveDatabase } from "../../../src/databaseFetcher";
16+
import { createMockCommandManager } from "../../__mocks__/commandsMock";
1617

1718
const baseDir = join(__dirname, "../../../test/data");
1819

@@ -147,6 +148,7 @@ describeWithCodeQL()("using the new query server", () => {
147148
await cleanDatabases(extension.databaseManager);
148149
const uri = Uri.file(dbLoc);
149150
const maybeDbItem = await importArchiveDatabase(
151+
createMockCommandManager(),
150152
uri.toString(true),
151153
extension.databaseManager,
152154
storagePath,

extensions/ql-vscode/test/vscode-tests/cli-integration/queries.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createInitialQueryInfo } from "../../../src/run-queries-shared";
2626
import { QueryRunner } from "../../../src/queryRunner";
2727
import { CompletedQueryInfo } from "../../../src/query-results";
2828
import { SELECT_QUERY_NAME } from "../../../src/contextual/locationFinder";
29+
import { createMockCommandManager } from "../../__mocks__/commandsMock";
2930

3031
jest.setTimeout(20_000);
3132

@@ -78,6 +79,7 @@ describeWithCodeQL()("Queries", () => {
7879
await cleanDatabases(databaseManager);
7980
const uri = Uri.file(dbLoc);
8081
const maybeDbItem = await importArchiveDatabase(
82+
createMockCommandManager(),
8183
uri.toString(true),
8284
databaseManager,
8385
storagePath,

0 commit comments

Comments
 (0)