Add repo to highlighted list

This commit is contained in:
Nora
2022-12-21 14:01:06 +00:00
parent 4bb3be9fd1
commit 758c182a33
4 changed files with 151 additions and 37 deletions

View File

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

View File

@@ -75,8 +75,18 @@ export class DbManager {
await this.dbConfigStore.updateExpandedState(newExpandedItems);
}
public async addNewRemoteRepo(nwo: string): Promise<void> {
await this.dbConfigStore.addRemoteRepo(nwo);
public async addNewRemoteRepo(
nwo: string,
parentList?: string,
): Promise<void> {
if (nwo === "") {
throw new Error("Repository name cannot be empty");
}
if (this.dbConfigStore.doesRemoteDbExist(nwo)) {
throw new Error(`The repository '${nwo}' already exists`);
}
await this.dbConfigStore.addRemoteRepo(nwo, parentList);
}
public async addNewRemoteOwner(owner: string): Promise<void> {

View File

@@ -82,40 +82,49 @@ export class DbPanel extends DisposableObject {
}
private async addNewRemoteDatabase(): Promise<void> {
const quickPickItems = [
{
label: "$(repo) From a GitHub repository",
detail: "Add a remote repository from GitHub",
alwaysShow: true,
kind: "repo",
},
{
label: "$(organization) All repositories of a GitHub org or owner",
detail:
"Add a remote list of repositories from a GitHub organization/owner",
alwaysShow: true,
kind: "owner",
},
];
const databaseKind =
await window.showQuickPick<RemoteDatabaseQuickPickItem>(quickPickItems, {
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();
const highlightedItem = await this.getHighlightedDbItem();
if (highlightedItem?.kind === DbItemKind.RemoteUserDefinedList) {
await this.addNewRemoteRepo(highlightedItem.listName);
} else {
const quickPickItems = [
{
label: "$(repo) From a GitHub repository",
detail: "Add a remote repository from GitHub",
alwaysShow: true,
kind: "repo",
},
{
label: "$(organization) All repositories of a GitHub org or owner",
detail:
"Add a remote list of repositories from a GitHub organization/owner",
alwaysShow: true,
kind: "owner",
},
];
const databaseKind =
await window.showQuickPick<RemoteDatabaseQuickPickItem>(
quickPickItems,
{
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({
title: "Add a remote repository",
prompt: "Insert a GitHub repository URL or name with owner",
@@ -136,7 +145,7 @@ export class DbPanel extends DisposableObject {
return;
}
await this.dbManager.addNewRemoteRepo(nwo);
await this.dbManager.addNewRemoteRepo(nwo, parentList);
}
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", () => {
it("should add a new remote list", async () => {
const dbConfig: DbConfig = createDbConfig({