Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 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 @@ -99,6 +99,12 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote repo if config is not loaded");
}

if (this.doesRemoteDbExist(repoNwo)) {
throw Error(
`A remote repository with the name '${repoNwo}' already exists`,
);
}

const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.repositories.push(repoNwo);

Expand All @@ -110,6 +116,10 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote owner if config is not loaded");
}

if (this.doesRemoteOwnerExist(owner)) {
throw Error(`A remote owner with the name '${owner}' already exists`);
}

const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.owners.push(owner);

Expand Down Expand Up @@ -144,6 +154,32 @@ export class DbConfigStore extends DisposableObject {
);
}

public doesRemoteDbExist(dbName: string, listName?: string): boolean {
if (!this.config) {
throw Error(
"Cannot check remote database existence if config is not loaded",
);
}

if (listName) {
return this.config.databases.remote.repositoryLists.some(
(l) => l.name === listName && l.repositories.includes(dbName),
);
}
Comment thread
charisk marked this conversation as resolved.

return this.config.databases.remote.repositories.includes(dbName);
}

public doesRemoteOwnerExist(owner: string): boolean {
if (!this.config) {
throw Error(
"Cannot check remote onwer existence if config is not loaded",
);
}

return this.config.databases.remote.owners.includes(owner);
}

private async writeConfig(config: DbConfig): Promise<void> {
await outputJSON(this.configPath, config, {
spaces: 2,
Expand Down
18 changes: 14 additions & 4 deletions extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ export class DbManager {
}

public async addNewRemoteRepo(nwo: 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`);
}
Comment thread
charisk marked this conversation as resolved.

await this.dbConfigStore.addRemoteRepo(nwo);
}

public async addNewRemoteOwner(owner: string): Promise<void> {
if (owner === "") {
throw Error("Owner name cannot be empty");
}
if (this.dbConfigStore.doesRemoteOwnerExist(owner)) {
throw Error(`The owner '${owner}' already exists`);
}
Comment thread
charisk marked this conversation as resolved.

await this.dbConfigStore.addRemoteOwner(owner);
}

Expand All @@ -97,8 +111,4 @@ export class DbManager {
throw Error("Cannot add a local list");
}
}

public doesRemoteListExist(listName: string): boolean {
return this.dbConfigStore.doesRemoteListExist(listName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,50 @@ describe("db panel", () => {
new Error("A list with the name 'my-list-1' already exists"),
);
});

it("should not allow adding a new remote db with empty name", async () => {
const dbConfig = createDbConfig();

await saveDbConfig(dbConfig);

await expect(dbManager.addNewRemoteRepo("")).rejects.toThrow(
new Error("Repository name cannot be empty"),
);
});

it("should not allow adding a remote db with duplicate name", async () => {
const dbConfig = createDbConfig({
remoteRepos: ["owner1/repo1"],
});

await saveDbConfig(dbConfig);

await expect(dbManager.addNewRemoteRepo("owner1/repo1")).rejects.toThrow(
new Error("The repository 'owner1/repo1' already exists"),
);
});

it("should not allow adding a new remote owner with empty name", async () => {
const dbConfig = createDbConfig();

await saveDbConfig(dbConfig);

await expect(dbManager.addNewRemoteOwner("")).rejects.toThrow(
new Error("Owner name cannot be empty"),
);
});

it("should not allow adding a remote owner with duplicate name", async () => {
const dbConfig = createDbConfig({
remoteOwners: ["owner1"],
});

await saveDbConfig(dbConfig);

await expect(dbManager.addNewRemoteOwner("owner1")).rejects.toThrow(
new Error("The owner 'owner1' already exists"),
);
});
});

async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
Expand Down