Skip to content

Commit 18f1a46

Browse files
Naively convert DatabaseFetcher to a class
1 parent 66de756 commit 18f1a46

15 files changed

Lines changed: 647 additions & 590 deletions

File tree

extensions/ql-vscode/src/databases/database-fetcher.ts

Lines changed: 549 additions & 531 deletions
Large diffs are not rendered by default.

extensions/ql-vscode/src/databases/github-databases/download.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { window } from "vscode";
22
import type { Octokit } from "@octokit/rest";
33
import { showNeverAskAgainDialog } from "../../common/vscode/dialog";
44
import { getLanguageDisplayName } from "../../common/query-language";
5-
import { downloadGitHubDatabaseFromUrl } from "../database-fetcher";
5+
import type { DatabaseFetcher } from "../database-fetcher";
66
import { withProgress } from "../../common/vscode/progress";
77
import type { DatabaseManager } from "../local-databases";
88
import type { CodeQLCliServer } from "../../codeql-cli/cli";
@@ -59,6 +59,7 @@ export async function downloadDatabaseFromGitHub(
5959
repo: string,
6060
databases: CodeqlDatabase[],
6161
databaseManager: DatabaseManager,
62+
databaseFetcher: DatabaseFetcher,
6263
storagePath: string,
6364
cliServer: CodeQLCliServer,
6465
commandManager: AppCommandManager,
@@ -72,7 +73,7 @@ export async function downloadDatabaseFromGitHub(
7273
selectedDatabases.map((database) =>
7374
withProgress(
7475
async (progress) => {
75-
await downloadGitHubDatabaseFromUrl(
76+
await databaseFetcher.downloadGitHubDatabaseFromUrl(
7677
database.url,
7778
database.id,
7879
database.created_at,

extensions/ql-vscode/src/databases/github-databases/github-databases-module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
isNewerDatabaseAvailable,
2525
} from "./updates";
2626
import type { Octokit } from "@octokit/rest";
27+
import type { DatabaseFetcher } from "../database-fetcher";
2728

2829
export class GitHubDatabasesModule extends DisposableObject {
2930
/**
@@ -33,6 +34,7 @@ export class GitHubDatabasesModule extends DisposableObject {
3334
constructor(
3435
private readonly app: App,
3536
private readonly databaseManager: DatabaseManager,
37+
private readonly databaseFetcher: DatabaseFetcher,
3638
private readonly databaseStoragePath: string,
3739
private readonly cliServer: CodeQLCliServer,
3840
private readonly config: GitHubDatabaseConfig,
@@ -43,13 +45,15 @@ export class GitHubDatabasesModule extends DisposableObject {
4345
public static async initialize(
4446
app: App,
4547
databaseManager: DatabaseManager,
48+
databaseFetcher: DatabaseFetcher,
4649
databaseStoragePath: string,
4750
cliServer: CodeQLCliServer,
4851
config: GitHubDatabaseConfig,
4952
): Promise<GitHubDatabasesModule> {
5053
const githubDatabasesModule = new GitHubDatabasesModule(
5154
app,
5255
databaseManager,
56+
databaseFetcher,
5357
databaseStoragePath,
5458
cliServer,
5559
config,
@@ -186,6 +190,7 @@ export class GitHubDatabasesModule extends DisposableObject {
186190
repo,
187191
databases,
188192
this.databaseManager,
193+
this.databaseFetcher,
189194
this.databaseStoragePath,
190195
this.cliServer,
191196
this.app.commands,
@@ -212,6 +217,7 @@ export class GitHubDatabasesModule extends DisposableObject {
212217
repo,
213218
databaseUpdates,
214219
this.databaseManager,
220+
this.databaseFetcher,
215221
this.databaseStoragePath,
216222
this.cliServer,
217223
this.app.commands,

extensions/ql-vscode/src/databases/github-databases/updates.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CodeQLCliServer } from "../../codeql-cli/cli";
55
import type { AppCommandManager } from "../../common/commands";
66
import { getLanguageDisplayName } from "../../common/query-language";
77
import { showNeverAskAgainDialog } from "../../common/vscode/dialog";
8-
import { downloadGitHubDatabaseFromUrl } from "../database-fetcher";
8+
import type { DatabaseFetcher } from "../database-fetcher";
99
import { withProgress } from "../../common/vscode/progress";
1010
import { window } from "vscode";
1111
import type { GitHubDatabaseConfig } from "../../config";
@@ -156,6 +156,7 @@ export async function downloadDatabaseUpdateFromGitHub(
156156
repo: string,
157157
updates: DatabaseUpdate[],
158158
databaseManager: DatabaseManager,
159+
databaseFetcher: DatabaseFetcher,
159160
storagePath: string,
160161
cliServer: CodeQLCliServer,
161162
commandManager: AppCommandManager,
@@ -179,21 +180,22 @@ export async function downloadDatabaseUpdateFromGitHub(
179180

180181
return withProgress(
181182
async (progress) => {
182-
const newDatabase = await downloadGitHubDatabaseFromUrl(
183-
database.url,
184-
database.id,
185-
database.created_at,
186-
database.commit_oid ?? null,
187-
owner,
188-
repo,
189-
octokit,
190-
progress,
191-
databaseManager,
192-
storagePath,
193-
cliServer,
194-
databaseManager.currentDatabaseItem === update.databaseItem,
195-
update.databaseItem.hasSourceArchiveInExplorer(),
196-
);
183+
const newDatabase =
184+
await databaseFetcher.downloadGitHubDatabaseFromUrl(
185+
database.url,
186+
database.id,
187+
database.created_at,
188+
database.commit_oid ?? null,
189+
owner,
190+
repo,
191+
octokit,
192+
progress,
193+
databaseManager,
194+
storagePath,
195+
cliServer,
196+
databaseManager.currentDatabaseItem === update.databaseItem,
197+
update.databaseItem.hasSourceArchiveInExplorer(),
198+
);
197199
if (newDatabase === undefined) {
198200
return;
199201
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ import {
4242
showAndLogExceptionWithTelemetry,
4343
showAndLogErrorMessage,
4444
} from "../common/logging";
45-
import {
46-
importArchiveDatabase,
47-
promptImportGithubDatabase,
48-
promptImportInternetDatabase,
49-
} from "./database-fetcher";
45+
import { DatabaseFetcher } from "./database-fetcher";
5046
import { asError, asyncFilter, getErrorMessage } from "../common/helpers-pure";
5147
import type { QueryRunner } from "../query-server";
5248
import type { App } from "../common/app";
@@ -540,7 +536,7 @@ export class DatabaseUI extends DisposableObject {
540536
private async handleChooseDatabaseInternet(): Promise<void> {
541537
return withProgress(
542538
async (progress) => {
543-
await promptImportInternetDatabase(
539+
await new DatabaseFetcher().promptImportInternetDatabase(
544540
this.app.commands,
545541
this.databaseManager,
546542
this.storagePath,
@@ -557,7 +553,7 @@ export class DatabaseUI extends DisposableObject {
557553
private async handleChooseDatabaseGithub(): Promise<void> {
558554
return withProgress(
559555
async (progress) => {
560-
await promptImportGithubDatabase(
556+
await new DatabaseFetcher().promptImportGithubDatabase(
561557
this.app,
562558
this.databaseManager,
563559
this.storagePath,
@@ -712,7 +708,7 @@ export class DatabaseUI extends DisposableObject {
712708
try {
713709
// Assume user has selected an archive if the file has a .zip extension
714710
if (uri.path.endsWith(".zip")) {
715-
await importArchiveDatabase(
711+
await new DatabaseFetcher().importArchiveDatabase(
716712
this.app.commands,
717713
uri.toString(true),
718714
this.databaseManager,
@@ -959,7 +955,7 @@ export class DatabaseUI extends DisposableObject {
959955
} else {
960956
// we are selecting a database archive. Must unzip into a workspace-controlled area
961957
// before importing.
962-
return await importArchiveDatabase(
958+
return await new DatabaseFetcher().importArchiveDatabase(
963959
this.app.commands,
964960
uri.toString(true),
965961
this.databaseManager,

extensions/ql-vscode/src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import { OpenReferencedFileCodeLensProvider } from "./local-queries/open-referen
133133
import { LanguageContextStore } from "./language-context-store";
134134
import { LanguageSelectionPanel } from "./language-selection-panel/language-selection-panel";
135135
import { GitHubDatabasesModule } from "./databases/github-databases";
136+
import { DatabaseFetcher } from "./databases/database-fetcher";
136137

137138
/**
138139
* extension.ts
@@ -881,6 +882,7 @@ async function activateWithInstalledDistribution(
881882
await GitHubDatabasesModule.initialize(
882883
app,
883884
dbm,
885+
new DatabaseFetcher(),
884886
getContextStoragePath(ctx),
885887
cliServer,
886888
githubDatabaseConfigListener,

extensions/ql-vscode/src/local-queries/local-queries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import type { QueryTreeViewItem } from "../queries-panel/query-tree-view-item";
5151
import { tryGetQueryLanguage } from "../common/query-language";
5252
import type { LanguageContextStore } from "../language-context-store";
5353
import type { ExtensionApp } from "../common/vscode/vscode-app";
54+
import { DatabaseFetcher } from "../databases/database-fetcher";
5455

5556
export enum QuickEvalType {
5657
None,
@@ -330,6 +331,7 @@ export class LocalQueries extends DisposableObject {
330331
progress,
331332
this.app,
332333
this.databaseManager,
334+
new DatabaseFetcher(),
333335
contextStoragePath,
334336
this.selectedQueryTreeViewItems,
335337
language,

extensions/ql-vscode/src/local-queries/skeleton-query-wizard.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import {
1919
UserCancellationException,
2020
withProgress,
2121
} from "../common/vscode/progress";
22-
import {
23-
askForGitHubRepo,
24-
downloadGitHubDatabase,
25-
} from "../databases/database-fetcher";
22+
import type { DatabaseFetcher } from "../databases/database-fetcher";
2623
import {
2724
getQlPackLocation,
2825
isCodespacesTemplate,
@@ -62,6 +59,7 @@ export class SkeletonQueryWizard {
6259
private readonly progress: ProgressCallback,
6360
private readonly app: App,
6461
private readonly databaseManager: DatabaseManager,
62+
private readonly databaseFetcher: DatabaseFetcher,
6563
private readonly databaseStoragePath: string | undefined,
6664
private readonly selectedItems: readonly QueryTreeViewItem[],
6765
private language: QueryLanguage | undefined = undefined,
@@ -378,13 +376,16 @@ export class SkeletonQueryWizard {
378376
});
379377

380378
const githubRepoNwo = QUERY_LANGUAGE_TO_DATABASE_REPO[this.language];
381-
const chosenRepo = await askForGitHubRepo(undefined, githubRepoNwo);
379+
const chosenRepo = await this.databaseFetcher.askForGitHubRepo(
380+
undefined,
381+
githubRepoNwo,
382+
);
382383

383384
if (!chosenRepo) {
384385
throw new UserCancellationException("No GitHub repository provided");
385386
}
386387

387-
await downloadGitHubDatabase(
388+
await this.databaseFetcher.downloadGitHubDatabase(
388389
chosenRepo,
389390
this.app,
390391
this.databaseManager,

extensions/ql-vscode/src/model-editor/model-editor-view.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type {
2929
} from "../databases/local-databases";
3030
import type { CodeQLCliServer } from "../codeql-cli/cli";
3131
import { asError, assertNever, getErrorMessage } from "../common/helpers-pure";
32-
import { promptImportGithubDatabase } from "../databases/database-fetcher";
32+
import { DatabaseFetcher } from "../databases/database-fetcher";
3333
import type { App } from "../common/app";
3434
import { redactableError } from "../common/errors";
3535
import {
@@ -916,16 +916,17 @@ export class ModelEditorView extends AbstractWebview<
916916
// the user to import the library database. We need to have the database
917917
// imported to the query server, so we need to register it to our workspace.
918918
const makeSelected = false;
919-
const addedDatabase = await promptImportGithubDatabase(
920-
this.app,
921-
this.databaseManager,
922-
this.app.workspaceStoragePath ?? this.app.globalStoragePath,
923-
progress,
924-
this.cliServer,
925-
this.databaseItem.language,
926-
makeSelected,
927-
false,
928-
);
919+
const addedDatabase =
920+
await new DatabaseFetcher().promptImportGithubDatabase(
921+
this.app,
922+
this.databaseManager,
923+
this.app.workspaceStoragePath ?? this.app.globalStoragePath,
924+
progress,
925+
this.cliServer,
926+
this.databaseItem.language,
927+
makeSelected,
928+
false,
929+
);
929930
if (!addedDatabase) {
930931
void this.app.logger.log("No database chosen");
931932
return;

extensions/ql-vscode/test/vscode-tests/cli-integration/databases/database-fetcher.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { Uri, window } from "vscode";
33

44
import type { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
55
import type { DatabaseManager } from "../../../../src/databases/local-databases";
6-
import {
7-
importArchiveDatabase,
8-
promptImportInternetDatabase,
9-
} from "../../../../src/databases/database-fetcher";
6+
import { DatabaseFetcher } from "../../../../src/databases/database-fetcher";
107
import {
118
cleanDatabases,
129
dbLoc,
@@ -49,7 +46,7 @@ describe("database-fetcher", () => {
4946
describe("importArchiveDatabase", () => {
5047
it("should add a database from a folder", async () => {
5148
const uri = Uri.file(dbLoc);
52-
let dbItem = await importArchiveDatabase(
49+
let dbItem = await new DatabaseFetcher().importArchiveDatabase(
5350
createMockCommandManager(),
5451
uri.toString(true),
5552
databaseManager,
@@ -71,7 +68,7 @@ describe("database-fetcher", () => {
7168
// Provide a database URL when prompted
7269
inputBoxStub.mockResolvedValue(DB_URL);
7370

74-
let dbItem = await promptImportInternetDatabase(
71+
let dbItem = await new DatabaseFetcher().promptImportInternetDatabase(
7572
createMockCommandManager(),
7673
databaseManager,
7774
storagePath,

0 commit comments

Comments
 (0)