Skip to content

Commit

Permalink
Allow import all database subfolders by selecting a folder
Browse files Browse the repository at this point in the history
  • Loading branch information
reitowo committed Nov 7, 2024
1 parent 7b29015 commit 1196902
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
2 changes: 2 additions & 0 deletions extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [UNRELEASED]

- Add a palette command that allow user to select a folder and import all database subfolders.

## 1.16.1 - 6 November 2024

- Support result columns of type `QlBuiltins::BigInt` in quick evaluations. [#3647](https://github.com/github/vscode-codeql/pull/3647)
Expand Down
4 changes: 4 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,10 @@
"command": "codeQL.chooseDatabaseFolder",
"title": "CodeQL: Choose Database from Folder"
},
{
"command": "codeQL.chooseMultipleDatabaseFolder",
"title": "CodeQL: Choose Folder contains all Database Folders to import"
},
{
"command": "codeQL.chooseDatabaseArchive",
"title": "CodeQL: Choose Database from Archive"
Expand Down
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export type LanguageSelectionCommands = {
export type LocalDatabasesCommands = {
// Command palette commands
"codeQL.chooseDatabaseFolder": () => Promise<void>;
"codeQL.chooseMultipleDatabaseFolder": () => Promise<void>;
"codeQL.chooseDatabaseArchive": () => Promise<void>;
"codeQL.chooseDatabaseInternet": () => Promise<void>;
"codeQL.chooseDatabaseGithub": () => Promise<void>;
Expand Down
92 changes: 74 additions & 18 deletions extensions/ql-vscode/src/databases/local-databases-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ThemeIcon,
ThemeColor,
workspace,
FileType,
} from "vscode";
import { pathExists, stat, readdir, remove } from "fs-extra";

Expand All @@ -36,6 +37,7 @@ import {
import {
showAndLogExceptionWithTelemetry,
showAndLogErrorMessage,
showAndLogInformationMessage,
} from "../common/logging";
import type { DatabaseFetcher } from "./database-fetcher";
import { asError, asyncFilter, getErrorMessage } from "../common/helpers-pure";
Expand Down Expand Up @@ -267,6 +269,8 @@ export class DatabaseUI extends DisposableObject {
"codeQL.getCurrentDatabase": this.handleGetCurrentDatabase.bind(this),
"codeQL.chooseDatabaseFolder":
this.handleChooseDatabaseFolderFromPalette.bind(this),
"codeQL.chooseMultipleDatabaseFolder":
this.handleChooseMultipleDatabaseFolderFromPalette.bind(this),
"codeQL.chooseDatabaseArchive":
this.handleChooseDatabaseArchiveFromPalette.bind(this),
"codeQL.chooseDatabaseInternet":
Expand Down Expand Up @@ -322,10 +326,11 @@ export class DatabaseUI extends DisposableObject {
}

private async chooseDatabaseFolder(
subFolder: boolean,
progress: ProgressCallback,
): Promise<void> {
try {
await this.chooseAndSetDatabase(true, progress);
await this.chooseAndSetDatabase(true, subFolder, progress);
} catch (e) {
void showAndLogExceptionWithTelemetry(
this.app.logger,
Expand All @@ -340,7 +345,7 @@ export class DatabaseUI extends DisposableObject {
private async handleChooseDatabaseFolder(): Promise<void> {
return withProgress(
async (progress) => {
await this.chooseDatabaseFolder(progress);
await this.chooseDatabaseFolder(false, progress);
},
{
title: "Adding database from folder",
Expand All @@ -351,14 +356,25 @@ export class DatabaseUI extends DisposableObject {
private async handleChooseDatabaseFolderFromPalette(): Promise<void> {
return withProgress(
async (progress) => {
await this.chooseDatabaseFolder(progress);
await this.chooseDatabaseFolder(false, progress);
},
{
title: "Choose a Database from a Folder",
},
);
}

private async handleChooseMultipleDatabaseFolderFromPalette(): Promise<void> {
return withProgress(
async (progress) => {
await this.chooseDatabaseFolder(true, progress);
},
{
title: "Choose a Folder contains all Database Folders",
},
);
}

private async handleSetDefaultTourDatabase(): Promise<void> {
return withProgress(
async () => {
Expand Down Expand Up @@ -494,7 +510,7 @@ export class DatabaseUI extends DisposableObject {
progress: ProgressCallback,
): Promise<void> {
try {
await this.chooseAndSetDatabase(false, progress);
await this.chooseAndSetDatabase(false, false, progress);
} catch (e: unknown) {
void showAndLogExceptionWithTelemetry(
this.app.logger,
Expand Down Expand Up @@ -962,27 +978,67 @@ export class DatabaseUI extends DisposableObject {
*/
private async chooseAndSetDatabase(
byFolder: boolean,
subFolder: boolean,
progress: ProgressCallback,
): Promise<DatabaseItem | undefined> {
): Promise<DatabaseItem[] | DatabaseItem | undefined> {
const uri = await chooseDatabaseDir(byFolder);
if (!uri) {
return undefined;
}

if (byFolder && !uri.fsPath.endsWith("testproj")) {
const fixedUri = await this.fixDbUri(uri);
// we are selecting a database folder
return await this.databaseManager.openDatabase(fixedUri, {
type: "folder",
});
if (subFolder) {
if (!byFolder) {
return undefined;
}

const databases: DatabaseItem[] = [];
const failures: string[] = [];
const entries = await workspace.fs.readDirectory(uri);
for (const entry of entries) {
if (entry[1] === FileType.Directory) {
try {
const fixedUri = await this.fixDbUri(Uri.joinPath(uri, entry[0]));
const database = await this.databaseManager.openDatabase(fixedUri, {
type: "folder",
});
databases.push(database);
} catch (e) {
failures.push(entry[0]);
}
}
}

if (failures.length) {
void showAndLogErrorMessage(
this.app.logger,
`Failed to import ${failures.length} database(s) (${failures.join(
", ",
)}), successfully imported ${databases.length} database(s).`,
);
} else {
void showAndLogInformationMessage(
this.app.logger,
`Successfully imported ${databases.length} database(s).`,
);
}

return databases;
} else {
// we are selecting a database archive or a testproj.
// Unzip archives (if an archive) and copy into a workspace-controlled area
// before importing.
return await this.databaseFetcher.importLocalDatabase(
uri.toString(true),
progress,
);
if (byFolder && !uri.fsPath.endsWith("testproj")) {
const fixedUri = await this.fixDbUri(uri);
// we are selecting a database folder
return await this.databaseManager.openDatabase(fixedUri, {
type: "folder",
});
} else {
// we are selecting a database archive or a testproj.
// Unzip archives (if an archive) and copy into a workspace-controlled area
// before importing.
return await this.databaseFetcher.importLocalDatabase(
uri.toString(true),
progress,
);
}
}
}

Expand Down

0 comments on commit 1196902

Please sign in to comment.