Merge pull request #1900 from github/nora/add-repo-to-list

Add new repositories to a highlighted user defined list
This commit is contained in:
Nora
2022-12-22 13:41:27 +01:00
committed by GitHub
4 changed files with 152 additions and 37 deletions

View File

@@ -94,7 +94,10 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(config); await this.writeConfig(config);
} }
public async addRemoteRepo(repoNwo: string): Promise<void> { public async addRemoteRepo(
repoNwo: string,
parentList?: string,
): Promise<void> {
if (!this.config) { if (!this.config) {
throw Error("Cannot add remote repo if config is not loaded"); throw Error("Cannot add remote repo if config is not loaded");
} }
@@ -110,8 +113,18 @@ export class DbConfigStore extends DisposableObject {
} }
const config: DbConfig = cloneDbConfig(this.config); const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.repositories.push(repoNwo); if (parentList) {
const parent = config.databases.remote.repositoryLists.find(
(list) => list.name === parentList,
);
if (!parent) {
throw Error(`Cannot find parent list '${parentList}'`);
} else {
parent.repositories.push(repoNwo);
}
} else {
config.databases.remote.repositories.push(repoNwo);
}
await this.writeConfig(config); await this.writeConfig(config);
} }

View File

@@ -75,8 +75,11 @@ export class DbManager {
await this.dbConfigStore.updateExpandedState(newExpandedItems); await this.dbConfigStore.updateExpandedState(newExpandedItems);
} }
public async addNewRemoteRepo(nwo: string): Promise<void> { public async addNewRemoteRepo(
await this.dbConfigStore.addRemoteRepo(nwo); nwo: string,
parentList?: string,
): Promise<void> {
await this.dbConfigStore.addRemoteRepo(nwo, parentList);
} }
public async addNewRemoteOwner(owner: string): Promise<void> { public async addNewRemoteOwner(owner: string): Promise<void> {

View File

@@ -82,40 +82,54 @@ export class DbPanel extends DisposableObject {
} }
private async addNewRemoteDatabase(): Promise<void> { private async addNewRemoteDatabase(): Promise<void> {
const quickPickItems = [ const highlightedItem = await this.getHighlightedDbItem();
{
label: "$(repo) From a GitHub repository", if (highlightedItem?.kind === DbItemKind.RemoteUserDefinedList) {
detail: "Add a remote repository from GitHub", await this.addNewRemoteRepo(highlightedItem.listName);
alwaysShow: true, } else if (
kind: "repo", highlightedItem?.kind === DbItemKind.RemoteRepo &&
}, highlightedItem.parentListName
{ ) {
label: "$(organization) All repositories of a GitHub org or owner", await this.addNewRemoteRepo(highlightedItem.parentListName);
detail: } else {
"Add a remote list of repositories from a GitHub organization/owner", const quickPickItems = [
alwaysShow: true, {
kind: "owner", label: "$(repo) From a GitHub repository",
}, detail: "Add a remote repository from GitHub",
]; alwaysShow: true,
const databaseKind = kind: "repo",
await window.showQuickPick<RemoteDatabaseQuickPickItem>(quickPickItems, { },
title: "Add a remote repository", {
placeHolder: "Select an option", label: "$(organization) All repositories of a GitHub org or owner",
ignoreFocusOut: true, detail:
}); "Add a remote list of repositories from a GitHub organization/owner",
if (!databaseKind) { alwaysShow: true,
// We don't need to display a warning pop-up in this case, since the user just escaped out of the operation. kind: "owner",
// We set 'true' to make this a silent exception. },
throw new UserCancellationException("No repository selected", true); ];
} const databaseKind =
if (databaseKind.kind === "repo") { await window.showQuickPick<RemoteDatabaseQuickPickItem>(
await this.addNewRemoteRepo(); quickPickItems,
} else if (databaseKind.kind === "owner") { {
await this.addNewRemoteOwner(); title: "Add a remote repository",
placeHolder: "Select an option",
ignoreFocusOut: true,
},
);
if (!databaseKind) {
// 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 repository selected", true);
}
if (databaseKind.kind === "repo") {
await this.addNewRemoteRepo();
} else if (databaseKind.kind === "owner") {
await this.addNewRemoteOwner();
}
} }
} }
private async addNewRemoteRepo(): Promise<void> { private async addNewRemoteRepo(parentList?: string): Promise<void> {
const repoName = await window.showInputBox({ const repoName = await window.showInputBox({
title: "Add a remote repository", title: "Add a remote repository",
prompt: "Insert a GitHub repository URL or name with owner", prompt: "Insert a GitHub repository URL or name with owner",
@@ -136,7 +150,7 @@ export class DbPanel extends DisposableObject {
return; return;
} }
await this.dbManager.addNewRemoteRepo(nwo); await this.dbManager.addNewRemoteRepo(nwo, parentList);
} }
private async addNewRemoteOwner(): Promise<void> { private async addNewRemoteOwner(): Promise<void> {

View File

@@ -447,6 +447,91 @@ describe("db panel", () => {
} }
}); });
describe("addNewRemoteRepo", () => {
it("should add a new remote repo", async () => {
const dbConfig: DbConfig = createDbConfig({
remoteRepos: ["owner1/repo1"],
});
await saveDbConfig(dbConfig);
const dbTreeItems = await dbTreeDataProvider.getChildren();
expect(dbTreeItems).toBeTruthy();
const items = dbTreeItems!;
const remoteRootNode = items[0];
const remoteRepos = remoteRootNode.children.filter(
(c) => c.dbItem?.kind === DbItemKind.RemoteRepo,
);
const repo1 = remoteRootNode.children.find(
(c) =>
c.dbItem?.kind === DbItemKind.RemoteRepo &&
c.dbItem?.repoFullName === "owner1/repo1",
);
expect(remoteRepos.length).toBe(1);
expect(remoteRepos[0]).toBe(repo1);
await dbManager.addNewRemoteRepo("owner2/repo2");
// Read the workspace databases JSON file directly to check that the new repo has been added.
// We can't use the dbConfigStore's `read` function here because it depends on the file watcher
// picking up changes, and we don't control the timing of that.
const dbConfigFileContents = await readJSON(dbConfigFilePath);
expect(dbConfigFileContents.databases.remote.repositories.length).toBe(2);
expect(dbConfigFileContents.databases.remote.repositories[1]).toEqual(
"owner2/repo2",
);
});
it("should add a new remote repo to a user defined list", async () => {
const dbConfig: DbConfig = createDbConfig({
remoteLists: [
{
name: "my-list-1",
repositories: ["owner1/repo1"],
},
],
});
await saveDbConfig(dbConfig);
const dbTreeItems = await dbTreeDataProvider.getChildren();
expect(dbTreeItems).toBeTruthy();
const items = dbTreeItems!;
const remoteRootNode = items[0];
const remoteUserDefinedLists = remoteRootNode.children.filter(
(c) => c.dbItem?.kind === DbItemKind.RemoteUserDefinedList,
);
const list1 = remoteRootNode.children.find(
(c) =>
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
c.dbItem?.listName === "my-list-1",
);
expect(remoteUserDefinedLists.length).toBe(1);
expect(remoteUserDefinedLists[0]).toBe(list1);
await dbManager.addNewRemoteRepo("owner2/repo2", "my-list-1");
// Read the workspace databases JSON file directly to check that the new repo has been added.
// We can't use the dbConfigStore's `read` function here because it depends on the file watcher
// picking up changes, and we don't control the timing of that.
const dbConfigFileContents = await readJSON(dbConfigFilePath);
expect(dbConfigFileContents.databases.remote.repositoryLists.length).toBe(
1,
);
expect(dbConfigFileContents.databases.remote.repositoryLists[0]).toEqual({
name: "my-list-1",
repositories: ["owner1/repo1", "owner2/repo2"],
});
});
});
describe("addNewList", () => { describe("addNewList", () => {
it("should add a new remote list", async () => { it("should add a new remote list", async () => {
const dbConfig: DbConfig = createDbConfig({ const dbConfig: DbConfig = createDbConfig({