Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow import all database subfolders by selecting a folder #3797

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [UNRELEASED]

- Add a palette command that allow user to select a folder and import all database subfolders.
- Add a palette command that allows importing of all databases inside of a parent folder. [3797](https://github.com/github/vscode-codeql/pull/3797)

## 1.16.1 - 6 November 2024

Expand Down
4 changes: 2 additions & 2 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,8 @@
"title": "CodeQL: Choose Database from Folder"
},
{
"command": "codeQL.chooseMultipleDatabaseFolder",
"title": "CodeQL: Choose Folder contains all Database Folders to import"
"command": "codeQL.chooseDatabaseFoldersParent",
"title": "CodeQL: Choose Folder to import all databases contained in it"
reitowo marked this conversation as resolved.
Show resolved Hide resolved
},
{
"command": "codeQL.chooseDatabaseArchive",
Expand Down
2 changes: 1 addition & 1 deletion extensions/ql-vscode/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export type LanguageSelectionCommands = {
export type LocalDatabasesCommands = {
// Command palette commands
"codeQL.chooseDatabaseFolder": () => Promise<void>;
"codeQL.chooseMultipleDatabaseFolder": () => Promise<void>;
"codeQL.chooseDatabaseFoldersParent": () => Promise<void>;
"codeQL.chooseDatabaseArchive": () => Promise<void>;
"codeQL.chooseDatabaseInternet": () => Promise<void>;
"codeQL.chooseDatabaseGithub": () => Promise<void>;
Expand Down
135 changes: 76 additions & 59 deletions extensions/ql-vscode/src/databases/local-databases-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +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.chooseDatabaseFoldersParent":
this.handleChooseDatabaseFoldersParentFromPalette.bind(this),
"codeQL.chooseDatabaseArchive":
this.handleChooseDatabaseArchiveFromPalette.bind(this),
"codeQL.chooseDatabaseInternet":
Expand Down Expand Up @@ -326,11 +326,10 @@ export class DatabaseUI extends DisposableObject {
}

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

private async handleChooseMultipleDatabaseFolderFromPalette(): Promise<void> {
private async handleChooseDatabaseFoldersParentFromPalette(): Promise<void> {
return withProgress(
async (progress) => {
await this.chooseDatabaseFolder(true, progress);
await this.chooseDatabasesParentFolder(progress);
},
{
title: "Choose a Folder contains all Database Folders",
Expand Down Expand Up @@ -510,7 +509,7 @@ export class DatabaseUI extends DisposableObject {
progress: ProgressCallback,
): Promise<void> {
try {
await this.chooseAndSetDatabase(false, false, progress);
await this.chooseAndSetDatabase(false, progress);
} catch (e: unknown) {
void showAndLogExceptionWithTelemetry(
this.app.logger,
Expand Down Expand Up @@ -978,68 +977,86 @@ export class DatabaseUI extends DisposableObject {
*/
private async chooseAndSetDatabase(
byFolder: boolean,
subFolder: boolean,
progress: ProgressCallback,
): Promise<DatabaseItem[] | DatabaseItem | undefined> {
): Promise<DatabaseItem | undefined> {
const uri = await chooseDatabaseDir(byFolder);
if (!uri) {
return undefined;
}

if (subFolder) {
if (!byFolder) {
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",
});
} 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,
);
}
}

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]);
}
}
}
/**
* Ask the user for a parent directory that contains all databases.
* Returns all valid databases, or `undefined` if the operation was canceled.
*/
private async chooseDatabasesParentFolder(
progress: ProgressCallback,
): Promise<DatabaseItem[] | undefined> {
const uri = await chooseDatabaseDir(true);
if (!uri) {
return undefined;
}

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).`,
);
const databases: DatabaseItem[] = [];
const failures: string[] = [];
const entries = await workspace.fs.readDirectory(uri);

for (const [index, entry] of entries.entries()) {
progress({
step: index + 1,
maxStep: entries.length,
message: `Importing ${entry[0]}`,
});

if (entry[1] === FileType.Directory) {
reitowo marked this conversation as resolved.
Show resolved Hide resolved
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]);
}
}
}

return databases;
if (failures.length) {
void showAndLogErrorMessage(
this.app.logger,
`Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s).`,
{ fullMessage: `Failed folders to import:\n${failures.join("\n")}` },
reitowo marked this conversation as resolved.
Show resolved Hide resolved
);
} else if (databases.length === 0) {
void showAndLogErrorMessage(
this.app.logger,
`No database folder to import.`,
);
return undefined;
} else {
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,
);
}
void showAndLogInformationMessage(
this.app.logger,
`Successfully imported ${databases.length} database(s).`,
);
}

return databases;
}

/**
Expand Down