Skip to content

Commit 691c9af

Browse files
authored
Merge pull request #370 from aeisenberg/aeisenberg/import-notify
Refactor how we import database archives
2 parents bb3aa79 + a137a72 commit 691c9af

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

+35-6
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import { Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode
44
import * as fs from "fs-extra";
55
import * as path from "path";
66
import { DatabaseManager, DatabaseItem } from "./databases";
7-
import { ProgressCallback, showAndLogErrorMessage, withProgress } from "./helpers";
7+
import { ProgressCallback, showAndLogErrorMessage, withProgress, showAndLogInformationMessage } from "./helpers";
88

99
/**
1010
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
1111
*
1212
* @param databasesManager the DatabaseManager
1313
* @param storagePath where to store the unzipped database.
1414
*/
15-
export default async function promptFetchDatabase(databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
15+
export async function promptImportInternetDatabase(databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
1616
let item: DatabaseItem | undefined = undefined;
1717

1818
try {
1919
const databaseUrl = await window.showInputBox({
2020
prompt: 'Enter URL of zipfile of database to download'
2121
});
2222
if (databaseUrl) {
23-
validateUrl(databaseUrl);
23+
validateHttpsUrl(databaseUrl);
2424

2525
const progressOptions: ProgressOptions = {
2626
location: ProgressLocation.Notification,
@@ -30,13 +30,41 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
3030
await withProgress(progressOptions, async progress => (item = await databaseArchiveFetcher(databaseUrl, databasesManager, storagePath, progress)));
3131
commands.executeCommand('codeQLDatabases.focus');
3232
}
33+
showAndLogInformationMessage('Database downloaded and imported successfully.');
3334
} catch (e) {
3435
showAndLogErrorMessage(e.message);
3536
}
3637

3738
return item;
3839
}
3940

41+
42+
/**
43+
* Imports a database from a local archive.
44+
*
45+
* @param databaseUrl the file url of the archive to import
46+
* @param databasesManager the DatabaseManager
47+
* @param storagePath where to store the unzipped database.
48+
*/
49+
export async function importArchiveDatabase(databaseUrl: string, databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
50+
let item: DatabaseItem | undefined = undefined;
51+
try {
52+
const progressOptions: ProgressOptions = {
53+
location: ProgressLocation.Notification,
54+
title: 'Importing database from archive',
55+
cancellable: false,
56+
};
57+
await withProgress(progressOptions, async progress => (item = await databaseArchiveFetcher(databaseUrl, databasesManager, storagePath, progress)));
58+
commands.executeCommand('codeQLDatabases.focus');
59+
60+
showAndLogInformationMessage('Database unzipped and imported successfully.');
61+
} catch (e) {
62+
showAndLogErrorMessage(e.message);
63+
}
64+
return item;
65+
}
66+
67+
4068
/**
4169
* Fetches an archive database. The database might be on the internet
4270
* or in the local filesystem.
@@ -46,15 +74,15 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
4674
* @param storagePath where to store the unzipped database.
4775
* @param progressCallback optional callback to send progress messages to
4876
*/
49-
export async function databaseArchiveFetcher(
77+
async function databaseArchiveFetcher(
5078
databaseUrl: string,
5179
databasesManager: DatabaseManager,
5280
storagePath: string,
5381
progressCallback?: ProgressCallback
5482
): Promise<DatabaseItem> {
5583
progressCallback?.({
5684
maxStep: 3,
57-
message: 'Downloading database',
85+
message: 'Getting database',
5886
step: 1
5987
});
6088
if (!storagePath) {
@@ -75,6 +103,7 @@ export async function databaseArchiveFetcher(
75103
step: 3
76104
});
77105

106+
// find the path to the database. The actual database might be in a sub-folder
78107
const dbPath = await findDirWithFile(unzipPath, '.dbinfo', 'codeql-database.yml');
79108
if (dbPath) {
80109
const item = await databasesManager.openDatabase(Uri.parse(dbPath));
@@ -106,7 +135,7 @@ async function getStorageFolder(storagePath: string, urlStr: string) {
106135
}
107136

108137

109-
function validateUrl(databaseUrl: string) {
138+
function validateHttpsUrl(databaseUrl: string) {
110139
let uri;
111140
try {
112141
uri = Uri.parse(databaseUrl, true);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { logger } from './logging';
88
import { clearCacheInDatabase, UserCancellationException } from './run-queries';
99
import * as qsClient from './queryserver-client';
1010
import { upgradeDatabase } from './upgrades';
11-
import promptFetchDatabase, { databaseArchiveFetcher } from './databaseFetcher';
11+
import { importArchiveDatabase, promptImportInternetDatabase } from './databaseFetcher';
1212

1313
type ThemableIconPath = { light: string; dark: string } | string;
1414

@@ -212,7 +212,7 @@ export class DatabaseUI extends DisposableObject {
212212
}
213213

214214
private handleChooseDatabaseInternet = async (): Promise<DatabaseItem | undefined> => {
215-
return await promptFetchDatabase(this.databaseManager, this.storagePath);
215+
return await promptImportInternetDatabase(this.databaseManager, this.storagePath);
216216
}
217217

218218
private handleSortByName = async () => {
@@ -292,7 +292,7 @@ export class DatabaseUI extends DisposableObject {
292292
private handleSetCurrentDatabase = async (uri: Uri): Promise<DatabaseItem | undefined> => {
293293
// Assume user has selected an archive if the file has a .zip extension
294294
if (uri.path.endsWith('.zip')) {
295-
return await databaseArchiveFetcher(uri.toString(), this.databaseManager, this.storagePath);
295+
return await importArchiveDatabase(uri.toString(), this.databaseManager, this.storagePath);
296296
}
297297

298298
return await this.setCurrentDatabase(uri);
@@ -366,7 +366,7 @@ export class DatabaseUI extends DisposableObject {
366366
else {
367367
// we are selecting a database archive. Must unzip into a workspace-controlled area
368368
// before importing.
369-
return await databaseArchiveFetcher(uri.toString(), this.databaseManager, this.storagePath);
369+
return await importArchiveDatabase(uri.toString(), this.databaseManager, this.storagePath);
370370
}
371371
}
372372
}

extensions/ql-vscode/src/extension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { displayQuickQuery } from './quick-query';
2020
import { compileAndRunQueryAgainstDatabase, tmpDirDisposal, UserCancellationException } from './run-queries';
2121
import { QLTestAdapterFactory } from './test-adapter';
2222
import { TestUIService } from './test-ui';
23-
import promptFetchDatabase from './databaseFetcher';
23+
import { promptImportInternetDatabase } from './databaseFetcher';
2424

2525
/**
2626
* extension.ts
@@ -335,7 +335,7 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
335335
await qs.restartQueryServer();
336336
helpers.showAndLogInformationMessage('CodeQL Query Server restarted.', { outputLogger: queryServerLogger });
337337
}));
338-
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptFetchDatabase(dbm, getContextStoragePath(ctx))));
338+
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptImportInternetDatabase(dbm, getContextStoragePath(ctx))));
339339

340340
ctx.subscriptions.push(client.start());
341341

0 commit comments

Comments
 (0)