Skip to content

Commit 3ee081e

Browse files
Move commonly-used field to DatabaseFetcher constructor
1 parent 18f1a46 commit 3ee081e

File tree

15 files changed

+115
-234
lines changed

15 files changed

+115
-234
lines changed

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

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
getNwoFromGitHubUrl,
2727
isValidGitHubNwo,
2828
} from "../common/github-url-identifier-helper";
29-
import type { AppCommandManager } from "../common/commands";
3029
import {
3130
addDatabaseSourceToWorkspace,
3231
allowHttp,
@@ -48,17 +47,23 @@ const DUPLICATE_FILENAMES_TRIES = 10_000;
4847

4948
export class DatabaseFetcher {
5049
/**
51-
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
52-
*
50+
* @param app the App
5351
* @param databaseManager the DatabaseManager
5452
* @param storagePath where to store the unzipped database.
53+
* @param cli the CodeQL CLI server
54+
**/
55+
constructor(
56+
private readonly app: App,
57+
private readonly databaseManager: DatabaseManager,
58+
private readonly storagePath: string,
59+
private readonly cli: CodeQLCliServer,
60+
) {}
61+
62+
/**
63+
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
5564
*/
5665
public async promptImportInternetDatabase(
57-
commandManager: AppCommandManager,
58-
databaseManager: DatabaseManager,
59-
storagePath: string,
6066
progress: ProgressCallback,
61-
cli: CodeQLCliServer,
6267
): Promise<DatabaseItem | undefined> {
6368
const databaseUrl = await window.showInputBox({
6469
prompt: "Enter URL of zipfile of database to download",
@@ -72,19 +77,16 @@ export class DatabaseFetcher {
7277
const item = await this.databaseArchiveFetcher(
7378
databaseUrl,
7479
{},
75-
databaseManager,
76-
storagePath,
7780
undefined,
7881
{
7982
type: "url",
8083
url: databaseUrl,
8184
},
8285
progress,
83-
cli,
8486
);
8587

8688
if (item) {
87-
await commandManager.execute("codeQLDatabases.focus");
89+
await this.app.commands.execute("codeQLDatabases.focus");
8890
void showAndLogInformationMessage(
8991
extLogger,
9092
"Database downloaded and imported successfully.",
@@ -98,21 +100,13 @@ export class DatabaseFetcher {
98100
* User enters a GitHub repository and then the user is asked which language
99101
* to download (if there is more than one)
100102
*
101-
* @param app the App
102-
* @param databaseManager the DatabaseManager
103-
* @param storagePath where to store the unzipped database.
104103
* @param progress the progress callback
105-
* @param cli the CodeQL CLI server
106104
* @param language the language to download. If undefined, the user will be prompted to choose a language.
107105
* @param makeSelected make the new database selected in the databases panel (default: true)
108106
* @param addSourceArchiveFolder whether to add a workspace folder containing the source archive to the workspace
109107
*/
110108
public async promptImportGithubDatabase(
111-
app: App,
112-
databaseManager: DatabaseManager,
113-
storagePath: string,
114109
progress: ProgressCallback,
115-
cli: CodeQLCliServer,
116110
language?: string,
117111
makeSelected = true,
118112
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
@@ -124,19 +118,15 @@ export class DatabaseFetcher {
124118

125119
const databaseItem = await this.downloadGitHubDatabase(
126120
githubRepo,
127-
app,
128-
databaseManager,
129-
storagePath,
130121
progress,
131-
cli,
132122
language,
133123
makeSelected,
134124
addSourceArchiveFolder,
135125
);
136126

137127
if (databaseItem) {
138128
if (makeSelected) {
139-
await app.commands.execute("codeQLDatabases.focus");
129+
await this.app.commands.execute("codeQLDatabases.focus");
140130
}
141131
void showAndLogInformationMessage(
142132
extLogger,
@@ -176,22 +166,14 @@ export class DatabaseFetcher {
176166
* Downloads a database from GitHub
177167
*
178168
* @param githubRepo the GitHub repository to download the database from
179-
* @param app the App
180-
* @param databaseManager the DatabaseManager
181-
* @param storagePath where to store the unzipped database.
182169
* @param progress the progress callback
183-
* @param cli the CodeQL CLI server
184170
* @param language the language to download. If undefined, the user will be prompted to choose a language.
185171
* @param makeSelected make the new database selected in the databases panel (default: true)
186172
* @param addSourceArchiveFolder whether to add a workspace folder containing the source archive to the workspace
187173
**/
188174
public async downloadGitHubDatabase(
189175
githubRepo: string,
190-
app: App,
191-
databaseManager: DatabaseManager,
192-
storagePath: string,
193176
progress: ProgressCallback,
194-
cli: CodeQLCliServer,
195177
language?: string,
196178
makeSelected = true,
197179
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
@@ -201,7 +183,7 @@ export class DatabaseFetcher {
201183
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
202184
}
203185

204-
const credentials = isCanary() ? app.credentials : undefined;
186+
const credentials = isCanary() ? this.app.credentials : undefined;
205187

206188
const octokit = credentials
207189
? await credentials.getOctokit()
@@ -235,9 +217,6 @@ export class DatabaseFetcher {
235217
name,
236218
octokit,
237219
progress,
238-
databaseManager,
239-
storagePath,
240-
cli,
241220
makeSelected,
242221
addSourceArchiveFolder,
243222
);
@@ -252,9 +231,6 @@ export class DatabaseFetcher {
252231
name: string,
253232
octokit: Octokit,
254233
progress: ProgressCallback,
255-
databaseManager: DatabaseManager,
256-
storagePath: string,
257-
cli: CodeQLCliServer,
258234
makeSelected = true,
259235
addSourceArchiveFolder = true,
260236
): Promise<DatabaseItem | undefined> {
@@ -275,8 +251,6 @@ export class DatabaseFetcher {
275251
Accept: "application/zip",
276252
Authorization: octokitToken ? `Bearer ${octokitToken}` : "",
277253
},
278-
databaseManager,
279-
storagePath,
280254
`${owner}/${name}`,
281255
{
282256
type: "github",
@@ -286,7 +260,6 @@ export class DatabaseFetcher {
286260
commitOid,
287261
},
288262
progress,
289-
cli,
290263
makeSelected,
291264
addSourceArchiveFolder,
292265
);
@@ -296,34 +269,24 @@ export class DatabaseFetcher {
296269
* Imports a database from a local archive.
297270
*
298271
* @param databaseUrl the file url of the archive to import
299-
* @param databaseManager the DatabaseManager
300-
* @param storagePath where to store the unzipped database.
301-
* @param cli the CodeQL CLI server
302272
*/
303273
public async importArchiveDatabase(
304-
commandManager: AppCommandManager,
305274
databaseUrl: string,
306-
databaseManager: DatabaseManager,
307-
storagePath: string,
308275
progress: ProgressCallback,
309-
cli: CodeQLCliServer,
310276
): Promise<DatabaseItem | undefined> {
311277
try {
312278
const item = await this.databaseArchiveFetcher(
313279
databaseUrl,
314280
{},
315-
databaseManager,
316-
storagePath,
317281
undefined,
318282
{
319283
type: "archive",
320284
path: databaseUrl,
321285
},
322286
progress,
323-
cli,
324287
);
325288
if (item) {
326-
await commandManager.execute("codeQLDatabases.focus");
289+
await this.app.commands.execute("codeQLDatabases.focus");
327290
void showAndLogInformationMessage(
328291
extLogger,
329292
"Database unzipped and imported successfully.",
@@ -348,24 +311,18 @@ export class DatabaseFetcher {
348311
*
349312
* @param databaseUrl URL from which to grab the database
350313
* @param requestHeaders Headers to send with the request
351-
* @param databaseManager the DatabaseManager
352-
* @param storagePath where to store the unzipped database.
353314
* @param nameOverride a name for the database that overrides the default
354315
* @param origin the origin of the database
355316
* @param progress callback to send progress messages to
356-
* @param cli the CodeQL CLI server
357317
* @param makeSelected make the new database selected in the databases panel (default: true)
358318
* @param addSourceArchiveFolder whether to add a workspace folder containing the source archive to the workspace
359319
*/
360320
private async databaseArchiveFetcher(
361321
databaseUrl: string,
362322
requestHeaders: { [key: string]: string },
363-
databaseManager: DatabaseManager,
364-
storagePath: string,
365323
nameOverride: string | undefined,
366324
origin: DatabaseOrigin,
367325
progress: ProgressCallback,
368-
cli: CodeQLCliServer,
369326
makeSelected = true,
370327
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
371328
): Promise<DatabaseItem> {
@@ -374,24 +331,19 @@ export class DatabaseFetcher {
374331
step: 1,
375332
maxStep: 4,
376333
});
377-
if (!storagePath) {
334+
if (!this.storagePath) {
378335
throw new Error("No storage path specified.");
379336
}
380-
await ensureDir(storagePath);
381-
const unzipPath = await this.getStorageFolder(
382-
storagePath,
383-
databaseUrl,
384-
nameOverride,
385-
);
337+
await ensureDir(this.storagePath);
338+
const unzipPath = await this.getStorageFolder(databaseUrl, nameOverride);
386339

387340
if (this.isFile(databaseUrl)) {
388-
await this.readAndUnzip(databaseUrl, unzipPath, cli, progress);
341+
await this.readAndUnzip(databaseUrl, unzipPath, progress);
389342
} else {
390343
await this.fetchAndUnzip(
391344
databaseUrl,
392345
requestHeaders,
393346
unzipPath,
394-
cli,
395347
progress,
396348
);
397349
}
@@ -416,7 +368,7 @@ export class DatabaseFetcher {
416368
});
417369
await this.ensureZippedSourceLocation(dbPath);
418370

419-
const item = await databaseManager.openDatabase(
371+
const item = await this.databaseManager.openDatabase(
420372
Uri.file(dbPath),
421373
origin,
422374
makeSelected,
@@ -432,11 +384,7 @@ export class DatabaseFetcher {
432384
}
433385
}
434386

435-
private async getStorageFolder(
436-
storagePath: string,
437-
urlStr: string,
438-
nameOverrride?: string,
439-
) {
387+
private async getStorageFolder(urlStr: string, nameOverrride?: string) {
440388
let lastName: string;
441389

442390
if (nameOverrride) {
@@ -454,7 +402,7 @@ export class DatabaseFetcher {
454402
}
455403
}
456404

457-
const realpath = await fs_realpath(storagePath);
405+
const realpath = await fs_realpath(this.storagePath);
458406
let folderName = lastName;
459407

460408
// get all existing files instead of calling pathExists on every
@@ -500,7 +448,6 @@ export class DatabaseFetcher {
500448
private async readAndUnzip(
501449
zipUrl: string,
502450
unzipPath: string,
503-
cli: CodeQLCliServer,
504451
progress?: ProgressCallback,
505452
) {
506453
const zipFile = Uri.parse(zipUrl).fsPath;
@@ -510,14 +457,13 @@ export class DatabaseFetcher {
510457
message: `Unzipping into ${basename(unzipPath)}`,
511458
});
512459

513-
await cli.databaseUnbundle(zipFile, unzipPath);
460+
await this.cli.databaseUnbundle(zipFile, unzipPath);
514461
}
515462

516463
private async fetchAndUnzip(
517464
databaseUrl: string,
518465
requestHeaders: { [key: string]: string },
519466
unzipPath: string,
520-
cli: CodeQLCliServer,
521467
progress?: ProgressCallback,
522468
) {
523469
// Although it is possible to download and stream directly to an unzipped directory,
@@ -606,7 +552,6 @@ export class DatabaseFetcher {
606552
await this.readAndUnzip(
607553
Uri.file(archivePath).toString(true),
608554
unzipPath,
609-
cli,
610555
progress,
611556
);
612557

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { showNeverAskAgainDialog } from "../../common/vscode/dialog";
44
import { getLanguageDisplayName } from "../../common/query-language";
55
import type { DatabaseFetcher } from "../database-fetcher";
66
import { withProgress } from "../../common/vscode/progress";
7-
import type { DatabaseManager } from "../local-databases";
8-
import type { CodeQLCliServer } from "../../codeql-cli/cli";
97
import type { AppCommandManager } from "../../common/commands";
108
import type { GitHubDatabaseConfig } from "../../config";
119
import type { CodeqlDatabase } from "./api";
@@ -58,10 +56,7 @@ export async function downloadDatabaseFromGitHub(
5856
owner: string,
5957
repo: string,
6058
databases: CodeqlDatabase[],
61-
databaseManager: DatabaseManager,
6259
databaseFetcher: DatabaseFetcher,
63-
storagePath: string,
64-
cliServer: CodeQLCliServer,
6560
commandManager: AppCommandManager,
6661
): Promise<void> {
6762
const selectedDatabases = await promptForDatabases(databases);
@@ -82,9 +77,6 @@ export async function downloadDatabaseFromGitHub(
8277
repo,
8378
octokit,
8479
progress,
85-
databaseManager,
86-
storagePath,
87-
cliServer,
8880
true,
8981
false,
9082
);

0 commit comments

Comments
 (0)