Skip to content

Commit dbdb4ba

Browse files
authored
Stop user from adding a db or owner with the same name (#1893)
1 parent 55869e8 commit dbdb4ba

3 files changed

Lines changed: 94 additions & 4 deletions

File tree

extensions/ql-vscode/src/databases/config/db-config-store.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export class DbConfigStore extends DisposableObject {
9999
throw Error("Cannot add remote repo if config is not loaded");
100100
}
101101

102+
if (this.doesRemoteDbExist(repoNwo)) {
103+
throw Error(
104+
`A remote repository with the name '${repoNwo}' already exists`,
105+
);
106+
}
107+
102108
const config: DbConfig = cloneDbConfig(this.config);
103109
config.databases.remote.repositories.push(repoNwo);
104110

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

119+
if (this.doesRemoteOwnerExist(owner)) {
120+
throw Error(`A remote owner with the name '${owner}' already exists`);
121+
}
122+
113123
const config: DbConfig = cloneDbConfig(this.config);
114124
config.databases.remote.owners.push(owner);
115125

@@ -144,6 +154,32 @@ export class DbConfigStore extends DisposableObject {
144154
);
145155
}
146156

157+
public doesRemoteDbExist(dbName: string, listName?: string): boolean {
158+
if (!this.config) {
159+
throw Error(
160+
"Cannot check remote database existence if config is not loaded",
161+
);
162+
}
163+
164+
if (listName) {
165+
return this.config.databases.remote.repositoryLists.some(
166+
(l) => l.name === listName && l.repositories.includes(dbName),
167+
);
168+
}
169+
170+
return this.config.databases.remote.repositories.includes(dbName);
171+
}
172+
173+
public doesRemoteOwnerExist(owner: string): boolean {
174+
if (!this.config) {
175+
throw Error(
176+
"Cannot check remote onwer existence if config is not loaded",
177+
);
178+
}
179+
180+
return this.config.databases.remote.owners.includes(owner);
181+
}
182+
147183
private async writeConfig(config: DbConfig): Promise<void> {
148184
await outputJSON(this.configPath, config, {
149185
spaces: 2,

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,24 @@ export class DbManager {
7676
}
7777

7878
public async addNewRemoteRepo(nwo: string): Promise<void> {
79+
if (nwo === "") {
80+
throw new Error("Repository name cannot be empty");
81+
}
82+
if (this.dbConfigStore.doesRemoteDbExist(nwo)) {
83+
throw new Error(`The repository '${nwo}' already exists`);
84+
}
85+
7986
await this.dbConfigStore.addRemoteRepo(nwo);
8087
}
8188

8289
public async addNewRemoteOwner(owner: string): Promise<void> {
90+
if (owner === "") {
91+
throw Error("Owner name cannot be empty");
92+
}
93+
if (this.dbConfigStore.doesRemoteOwnerExist(owner)) {
94+
throw Error(`The owner '${owner}' already exists`);
95+
}
96+
8397
await this.dbConfigStore.addRemoteOwner(owner);
8498
}
8599

@@ -97,8 +111,4 @@ export class DbManager {
97111
throw Error("Cannot add a local list");
98112
}
99113
}
100-
101-
public doesRemoteListExist(listName: string): boolean {
102-
return this.dbConfigStore.doesRemoteListExist(listName);
103-
}
104114
}

extensions/ql-vscode/src/vscode-tests/minimal-workspace/databases/db-panel.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,50 @@ describe("db panel", () => {
589589
new Error("A list with the name 'my-list-1' already exists"),
590590
);
591591
});
592+
593+
it("should not allow adding a new remote db with empty name", async () => {
594+
const dbConfig = createDbConfig();
595+
596+
await saveDbConfig(dbConfig);
597+
598+
await expect(dbManager.addNewRemoteRepo("")).rejects.toThrow(
599+
new Error("Repository name cannot be empty"),
600+
);
601+
});
602+
603+
it("should not allow adding a remote db with duplicate name", async () => {
604+
const dbConfig = createDbConfig({
605+
remoteRepos: ["owner1/repo1"],
606+
});
607+
608+
await saveDbConfig(dbConfig);
609+
610+
await expect(dbManager.addNewRemoteRepo("owner1/repo1")).rejects.toThrow(
611+
new Error("The repository 'owner1/repo1' already exists"),
612+
);
613+
});
614+
615+
it("should not allow adding a new remote owner with empty name", async () => {
616+
const dbConfig = createDbConfig();
617+
618+
await saveDbConfig(dbConfig);
619+
620+
await expect(dbManager.addNewRemoteOwner("")).rejects.toThrow(
621+
new Error("Owner name cannot be empty"),
622+
);
623+
});
624+
625+
it("should not allow adding a remote owner with duplicate name", async () => {
626+
const dbConfig = createDbConfig({
627+
remoteOwners: ["owner1"],
628+
});
629+
630+
await saveDbConfig(dbConfig);
631+
632+
await expect(dbManager.addNewRemoteOwner("owner1")).rejects.toThrow(
633+
new Error("The owner 'owner1' already exists"),
634+
);
635+
});
592636
});
593637

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

0 commit comments

Comments
 (0)