Skip to content

Commit 2b4e302

Browse files
committed
Surface truncated repos when a list hits 1000 items
1 parent d07b7c8 commit 2b4e302

File tree

5 files changed

+119
-20
lines changed

5 files changed

+119
-20
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class DbConfigStore extends DisposableObject {
148148
public async addRemoteReposToList(
149149
repoNwoList: string[],
150150
parentList: string,
151-
): Promise<void> {
151+
): Promise<string[]> {
152152
if (!this.config) {
153153
throw Error("Cannot add variant analysis repos if config is not loaded");
154154
}
@@ -161,23 +161,22 @@ export class DbConfigStore extends DisposableObject {
161161
throw Error(`Cannot find parent list '${parentList}'`);
162162
}
163163

164-
const newRepositoriesList = new Set([
165-
...new Set(parent.repositories),
166-
...new Set(repoNwoList),
167-
]);
164+
// Remove duplicates from the list of repositories.
165+
const newRepositoriesList = [
166+
...new Set([...new Set(parent.repositories), ...new Set(repoNwoList)]),
167+
];
168+
169+
parent.repositories = newRepositoriesList.slice(0, 1000);
170+
const truncatedRepositories = newRepositoriesList.slice(1000);
168171

169-
if (newRepositoriesList.size > 1000) {
170-
parent.repositories = [...Array.from(newRepositoriesList).slice(0, 1000)];
171-
} else {
172-
parent.repositories = [...newRepositoriesList];
173-
}
174172
await this.writeConfig(config);
173+
return truncatedRepositories;
175174
}
176175

177176
public async addRemoteRepo(
178177
repoNwo: string,
179178
parentList?: string,
180-
): Promise<void> {
179+
): Promise<string[]> {
181180
if (!this.config) {
182181
throw Error("Cannot add variant analysis repo if config is not loaded");
183182
}
@@ -192,6 +191,7 @@ export class DbConfigStore extends DisposableObject {
192191
);
193192
}
194193

194+
const truncatedRepositories = [];
195195
const config = cloneDbConfig(this.config);
196196
if (parentList) {
197197
const parent = config.databases.variantAnalysis.repositoryLists.find(
@@ -200,12 +200,15 @@ export class DbConfigStore extends DisposableObject {
200200
if (!parent) {
201201
throw Error(`Cannot find parent list '${parentList}'`);
202202
} else {
203-
parent.repositories.push(repoNwo);
203+
const newRepositories = [...parent.repositories, repoNwo];
204+
parent.repositories = newRepositories.slice(0, 1000);
205+
truncatedRepositories.push(...newRepositories.slice(1000));
204206
}
205207
} else {
206208
config.databases.variantAnalysis.repositories.push(repoNwo);
207209
}
208210
await this.writeConfig(config);
211+
return truncatedRepositories;
209212
}
210213

211214
public async addRemoteOwner(owner: string): Promise<void> {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ export class DbManager {
9696
public async addNewRemoteRepo(
9797
nwo: string,
9898
parentList?: string,
99-
): Promise<void> {
100-
await this.dbConfigStore.addRemoteRepo(nwo, parentList);
99+
): Promise<string[]> {
100+
return await this.dbConfigStore.addRemoteRepo(nwo, parentList);
101101
}
102102

103103
public async addNewRemoteReposToList(
104104
nwoList: string[],
105105
parentList: string,
106-
): Promise<void> {
107-
await this.dbConfigStore.addRemoteReposToList(nwoList, parentList);
106+
): Promise<string[]> {
107+
return await this.dbConfigStore.addRemoteReposToList(nwoList, parentList);
108108
}
109109

110110
public async addNewRemoteOwner(owner: string): Promise<void> {

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,14 @@ export class DbPanel extends DisposableObject {
179179
return;
180180
}
181181

182-
await this.dbManager.addNewRemoteRepo(nwo, parentList);
182+
const truncatedRepositories = await this.dbManager.addNewRemoteRepo(
183+
nwo,
184+
parentList,
185+
);
186+
187+
if (parentList) {
188+
this.truncatedReposNote(truncatedRepositories, parentList);
189+
}
183190
}
184191

185192
private async addNewRemoteOwner(): Promise<void> {
@@ -373,10 +380,27 @@ export class DbPanel extends DisposableObject {
373380
`${codeSearchQuery} language:${codeSearchLanguage.language}`,
374381
);
375382

376-
await this.dbManager.addNewRemoteReposToList(
383+
const truncatedRepositories = await this.dbManager.addNewRemoteReposToList(
377384
repositories,
378385
treeViewItem.dbItem.listName,
379386
);
387+
this.truncatedReposNote(
388+
truncatedRepositories,
389+
treeViewItem.dbItem.listName,
390+
);
391+
}
392+
393+
private truncatedReposNote(
394+
truncatedRepositories: string[],
395+
listName: string,
396+
) {
397+
if (truncatedRepositories.length > 0) {
398+
void showAndLogErrorMessage(
399+
`Some repositories were not added to '${listName}' because a list can only have 1000 entries. Excluded repositories: ${truncatedRepositories.join(
400+
", ",
401+
)}`,
402+
);
403+
}
380404
}
381405

382406
private async onDidCollapseElement(

extensions/ql-vscode/test/unit-tests/databases/config/db-config-store.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ describe("db config store", () => {
282282
configStore.dispose();
283283
});
284284

285-
it("should add no more than 1000 repositories to a list", async () => {
285+
it("should add no more than 1000 repositories to a remote list using #addRemoteReposToList", async () => {
286286
// Initial set up
287287
const dbConfig = createDbConfig({
288288
remoteLists: [
@@ -296,7 +296,7 @@ describe("db config store", () => {
296296
const configStore = await initializeConfig(dbConfig, configPath, app);
297297

298298
// Add
299-
await configStore.addRemoteReposToList(
299+
const reponse = await configStore.addRemoteReposToList(
300300
[...Array(1001).keys()].map((i) => `owner/db${i}`),
301301
"list1",
302302
);
@@ -311,6 +311,38 @@ describe("db config store", () => {
311311
expect(updatedRemoteDbs.repositoryLists[0].repositories).toHaveLength(
312312
1000,
313313
);
314+
expect(reponse).toEqual(["owner/db1000"]);
315+
316+
configStore.dispose();
317+
});
318+
319+
it("should add no more than 1000 repositories to a remote list using #addRemoteRepo", async () => {
320+
// Initial set up
321+
const dbConfig = createDbConfig({
322+
remoteLists: [
323+
{
324+
name: "list1",
325+
repositories: [...Array(1000).keys()].map((i) => `owner/db${i}`),
326+
},
327+
],
328+
});
329+
330+
const configStore = await initializeConfig(dbConfig, configPath, app);
331+
332+
// Add
333+
const reponse = await configStore.addRemoteRepo("owner/db1000", "list1");
334+
335+
// Read the config file
336+
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
337+
338+
// Check that the config file has been updated
339+
const updatedRemoteDbs = updatedDbConfig.databases.variantAnalysis;
340+
expect(updatedRemoteDbs.repositories).toHaveLength(0);
341+
expect(updatedRemoteDbs.repositoryLists).toHaveLength(1);
342+
expect(updatedRemoteDbs.repositoryLists[0].repositories).toHaveLength(
343+
1000,
344+
);
345+
expect(reponse).toEqual(["owner/db1000"]);
314346

315347
configStore.dispose();
316348
});

extensions/ql-vscode/test/unit-tests/databases/db-manager.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,46 @@ describe("db manager", () => {
115115
});
116116
});
117117

118+
it("should return truncated repos when adding to a user defined list using #addNewRemoteReposToList", async () => {
119+
const dbConfig: DbConfig = createDbConfig({
120+
remoteLists: [
121+
{
122+
name: "my-list-1",
123+
repositories: [...Array(1000).keys()].map((i) => `owner/db${i}`),
124+
},
125+
],
126+
});
127+
128+
await saveDbConfig(dbConfig);
129+
130+
const response = await dbManager.addNewRemoteReposToList(
131+
["owner2/repo2"],
132+
"my-list-1",
133+
);
134+
135+
expect(response).toEqual(["owner2/repo2"]);
136+
});
137+
138+
it("should return truncated repos when adding to a user defined list using #addNewRemoteRepo", async () => {
139+
const dbConfig: DbConfig = createDbConfig({
140+
remoteLists: [
141+
{
142+
name: "my-list-1",
143+
repositories: [...Array(1000).keys()].map((i) => `owner/db${i}`),
144+
},
145+
],
146+
});
147+
148+
await saveDbConfig(dbConfig);
149+
150+
const response = await dbManager.addNewRemoteRepo(
151+
"owner2/repo2",
152+
"my-list-1",
153+
);
154+
155+
expect(response).toEqual(["owner2/repo2"]);
156+
});
157+
118158
it("should add a new remote repo to a user defined list", async () => {
119159
const dbConfig: DbConfig = createDbConfig({
120160
remoteLists: [

0 commit comments

Comments
 (0)