Skip to content

Commit

Permalink
Ask for user input and add response to list
Browse files Browse the repository at this point in the history
  • Loading branch information
norascheuch committed May 23, 2023
1 parent fed6158 commit 4d9e8d9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
25 changes: 25 additions & 0 deletions extensions/ql-vscode/src/databases/config/db-config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,31 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(config);
}

public async addRemoteReposToList(
repoNwoList: string[],
parentList: string,
): Promise<void> {
if (!this.config) {
throw Error("Cannot add variant analysis repos if config is not loaded");
}

const config = cloneDbConfig(this.config);
const parent = config.databases.variantAnalysis.repositoryLists.find(
(list) => list.name === parentList,
);
if (!parent) {
throw Error(`Cannot find parent list '${parentList}'`);
}

const newRepositoriesList = new Set([
...new Set(parent.repositories),
...new Set(repoNwoList),
]);
parent.repositories = [...newRepositoriesList];

await this.writeConfig(config);
}

public async addRemoteRepo(
repoNwo: string,
parentList?: string,
Expand Down
7 changes: 7 additions & 0 deletions extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ export class DbManager {
await this.dbConfigStore.addRemoteRepo(nwo, parentList);
}

public async addNewRemoteReposToList(
nwoList: string[],
parentList: string,
): Promise<void> {
await this.dbConfigStore.addRemoteReposToList(nwoList, parentList);
}

public async addNewRemoteOwner(owner: string): Promise<void> {
await this.dbConfigStore.addRemoteOwner(owner);
}
Expand Down
50 changes: 49 additions & 1 deletion extensions/ql-vscode/src/databases/ui/db-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { getControllerRepo } from "../../variant-analysis/run-remote-query";
import { getErrorMessage } from "../../pure/helpers-pure";
import { DatabasePanelCommands } from "../../common/commands";
import { App } from "../../common/app";
import { getCodeSearchRepositories } from "../../variant-analysis/gh-api/gh-api-client";
import { QueryLanguage } from "../../common/query-language";

export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
remoteDatabaseKind: string;
Expand All @@ -41,6 +43,10 @@ export interface AddListQuickPickItem extends QuickPickItem {
databaseKind: DbListKind;
}

export interface CodeSearchQuickPickItem extends QuickPickItem {
language: string;
}

export class DbPanel extends DisposableObject {
private readonly dataProvider: DbTreeDataProvider;
private readonly treeView: TreeView<DbTreeViewItem>;
Expand Down Expand Up @@ -326,9 +332,51 @@ export class DbPanel extends DisposableObject {
}

private async importCodeSearch(treeViewItem: DbTreeViewItem): Promise<void> {
if (treeViewItem.dbItem === undefined) {
if (treeViewItem.dbItem?.kind !== DbItemKind.RemoteUserDefinedList) {
throw new Error("Please select a valid list to add code search results.");
}

const languageQuickPickItems: CodeSearchQuickPickItem[] = Object.values(
QueryLanguage,
).map((language) => ({
label: language.toString(),
alwaysShow: true,
language: language.toString(),
}));

const codeSearchLanguage =
await window.showQuickPick<CodeSearchQuickPickItem>(
languageQuickPickItems,
{
title: "Select the language you want to query",
placeHolder: "Select an option",
ignoreFocusOut: true,
},
);
if (!codeSearchLanguage) {
// We don't need to display a warning pop-up in this case, since the user just escaped out of the operation.
// We set 'true' to make this a silent exception.
throw new UserCancellationException("No language selected", true);
}

const codeSearchQuery = await window.showInputBox({
title: "Code search query",
prompt: "Insert code search query",
placeHolder: "org:github",
});
if (codeSearchQuery === undefined || codeSearchQuery === "") {
return;
}

const repositories = await getCodeSearchRepositories(
this.app.credentials,
`${codeSearchQuery} language:${codeSearchLanguage.language}`,
);

await this.dbManager.addNewRemoteReposToList(
repositories,
treeViewItem.dbItem.listName,
);
}

private async onDidCollapseElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ describe("getDbItemActions", () => {
expect(actions.length).toEqual(0);
});

it("should set canBeSelected, canBeRemoved and canBeRenamed for remote user defined db list", () => {
it("should set canBeSelected, canBeRemoved, canBeRenamed and canImportCodeSearch for remote user defined db list", () => {
const dbItem = createRemoteUserDefinedListDbItem();

const actions = getDbItemActions(dbItem);

expect(actions).toEqual(["canBeSelected", "canBeRemoved", "canBeRenamed"]);
expect(actions).toEqual([
"canBeSelected",
"canBeRemoved",
"canBeRenamed",
"canImportCodeSearch",
]);
});

it("should not set canBeSelected for remote user defined db list that is already selected", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,12 @@ describe("db panel rendering nodes", () => {
expect(item.tooltip).toBeUndefined();
expect(item.iconPath).toBeUndefined();
expect(item.collapsibleState).toBe(TreeItemCollapsibleState.Collapsed);
checkDbItemActions(item, ["canBeSelected", "canBeRenamed", "canBeRemoved"]);
checkDbItemActions(item, [
"canBeSelected",
"canBeRenamed",
"canBeRemoved",
"canImportCodeSearch",
]);
expect(item.children).toBeTruthy();
expect(item.children.length).toBe(repos.length);

Expand Down

0 comments on commit 4d9e8d9

Please sign in to comment.