Convert extensions/ql-vscode/src/databaseFetcher.ts to call typed commands

This commit is contained in:
Robert
2023-03-22 15:50:06 +00:00
parent 13c3c8efe4
commit ddf5b3aac2
6 changed files with 20 additions and 4 deletions

View File

@@ -35,6 +35,7 @@ export type SingleSelectionCommandFunction<Item> = (
// Builtin commands where the implementation is provided by VS Code and not by this extension.
// See https://code.visualstudio.com/api/references/commands
export type BuiltInVsCodeCommands = {
"codeQLDatabases.focus": () => Promise<void>;
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
setContext: (
key: `${"codeql" | "codeQL"}${string}`,

View File

@@ -1,7 +1,7 @@
import fetch, { Response } from "node-fetch";
import { zip } from "zip-a-folder";
import { Open } from "unzipper";
import { Uri, CancellationToken, commands, window } from "vscode";
import { Uri, CancellationToken, window } from "vscode";
import { CodeQLCliServer } from "./cli";
import {
ensureDir,
@@ -26,6 +26,7 @@ import {
isValidGitHubNwo,
} from "./common/github-url-identifier-helper";
import { Credentials } from "./common/authentication";
import { AppCommandManager } from "./common/commands";
/**
* 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";
* @param storagePath where to store the unzipped database.
*/
export async function promptImportInternetDatabase(
commandManager: AppCommandManager,
databaseManager: DatabaseManager,
storagePath: string,
progress: ProgressCallback,
@@ -61,7 +63,7 @@ export async function promptImportInternetDatabase(
);
if (item) {
await commands.executeCommand("codeQLDatabases.focus");
await commandManager.execute("codeQLDatabases.focus");
void showAndLogInformationMessage(
"Database downloaded and imported successfully.",
);
@@ -78,6 +80,7 @@ export async function promptImportInternetDatabase(
* @param storagePath where to store the unzipped database.
*/
export async function promptImportGithubDatabase(
commandManager: AppCommandManager,
databaseManager: DatabaseManager,
storagePath: string,
credentials: Credentials | undefined,
@@ -141,7 +144,7 @@ export async function promptImportGithubDatabase(
cli,
);
if (item) {
await commands.executeCommand("codeQLDatabases.focus");
await commandManager.execute("codeQLDatabases.focus");
void showAndLogInformationMessage(
"Database downloaded and imported successfully.",
);
@@ -158,6 +161,7 @@ export async function promptImportGithubDatabase(
* @param storagePath where to store the unzipped database.
*/
export async function importArchiveDatabase(
commandManager: AppCommandManager,
databaseUrl: string,
databaseManager: DatabaseManager,
storagePath: string,
@@ -177,7 +181,7 @@ export async function importArchiveDatabase(
cli,
);
if (item) {
await commands.executeCommand("codeQLDatabases.focus");
await commandManager.execute("codeQLDatabases.focus");
void showAndLogInformationMessage(
"Database unzipped and imported successfully.",
);

View File

@@ -451,6 +451,7 @@ export class DatabaseUI extends DisposableObject {
return withProgress(
async (progress, token) => {
await promptImportInternetDatabase(
this.app.commands,
this.databaseManager,
this.storagePath,
progress,
@@ -470,6 +471,7 @@ export class DatabaseUI extends DisposableObject {
const credentials = isCanary() ? this.app.credentials : undefined;
await promptImportGithubDatabase(
this.app.commands,
this.databaseManager,
this.storagePath,
credentials,
@@ -607,6 +609,7 @@ export class DatabaseUI extends DisposableObject {
// Assume user has selected an archive if the file has a .zip extension
if (uri.path.endsWith(".zip")) {
await importArchiveDatabase(
this.app.commands,
uri.toString(true),
this.databaseManager,
this.storagePath,
@@ -762,6 +765,7 @@ export class DatabaseUI extends DisposableObject {
// we are selecting a database archive. Must unzip into a workspace-controlled area
// before importing.
return await importArchiveDatabase(
this.app.commands,
uri.toString(true),
this.databaseManager,
this.storagePath,

View File

@@ -9,6 +9,7 @@ import {
promptImportInternetDatabase,
} from "../../../src/databaseFetcher";
import { cleanDatabases, dbLoc, DB_URL, storagePath } from "../global.helper";
import { createMockCommandManager } from "../../__mocks__/commandsMock";
jest.setTimeout(60_000);
@@ -53,6 +54,7 @@ describe("DatabaseFetcher", () => {
it("should add a database from a folder", async () => {
const uri = Uri.file(dbLoc);
let dbItem = await importArchiveDatabase(
createMockCommandManager(),
uri.toString(true),
databaseManager,
storagePath,
@@ -75,6 +77,7 @@ describe("DatabaseFetcher", () => {
inputBoxStub.mockResolvedValue(DB_URL);
let dbItem = await promptImportInternetDatabase(
createMockCommandManager(),
databaseManager,
storagePath,
progressCallback,

View File

@@ -13,6 +13,7 @@ import { extLogger, ProgressReporter } from "../../../src/common";
import { QueryResultType } from "../../../src/pure/new-messages";
import { cleanDatabases, dbLoc, storagePath } from "../global.helper";
import { importArchiveDatabase } from "../../../src/databaseFetcher";
import { createMockCommandManager } from "../../__mocks__/commandsMock";
const baseDir = join(__dirname, "../../../test/data");
@@ -147,6 +148,7 @@ describeWithCodeQL()("using the new query server", () => {
await cleanDatabases(extension.databaseManager);
const uri = Uri.file(dbLoc);
const maybeDbItem = await importArchiveDatabase(
createMockCommandManager(),
uri.toString(true),
extension.databaseManager,
storagePath,

View File

@@ -26,6 +26,7 @@ import { createInitialQueryInfo } from "../../../src/run-queries-shared";
import { QueryRunner } from "../../../src/queryRunner";
import { CompletedQueryInfo } from "../../../src/query-results";
import { SELECT_QUERY_NAME } from "../../../src/contextual/locationFinder";
import { createMockCommandManager } from "../../__mocks__/commandsMock";
jest.setTimeout(20_000);
@@ -78,6 +79,7 @@ describeWithCodeQL()("Queries", () => {
await cleanDatabases(databaseManager);
const uri = Uri.file(dbLoc);
const maybeDbItem = await importArchiveDatabase(
createMockCommandManager(),
uri.toString(true),
databaseManager,
storagePath,