Skip to content

Commit 02c8df0

Browse files
committed
Fix missing error message on repository selection
The repository selection was structured such that you would get in the `else` case if there was nothing selected, but this case would also be used if for some other reason the selected item was not valid. This restructures the conditions to first check whether the user cancelled out of the operation and will silently return in that case. In other cases where it cannot determine the repositories, it will now show a user-visible error.
1 parent b91e31c commit 02c8df0

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

extensions/ql-vscode/src/remote-queries/repository-selection.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,21 @@ export async function getRepositorySelection(): Promise<RepositorySelection> {
4949
options,
5050
);
5151

52-
if (quickpick?.repositories?.length) {
52+
if (!quickpick) {
53+
// We don't need to display a warning pop-up in this case, since the user just escaped out of the operation.
54+
// We set 'true' to make this a silent exception.
55+
throw new UserCancellationException("No repositories selected", true);
56+
}
57+
58+
if (quickpick.repositories?.length) {
5359
void extLogger.log(
5460
`Selected repositories: ${quickpick.repositories.join(", ")}`,
5561
);
5662
return { repositories: quickpick.repositories };
57-
} else if (quickpick?.repositoryList) {
63+
} else if (quickpick.repositoryList) {
5864
void extLogger.log(`Selected repository list: ${quickpick.repositoryList}`);
5965
return { repositoryLists: [quickpick.repositoryList] };
60-
} else if (quickpick?.useCustomRepo) {
66+
} else if (quickpick.useCustomRepo) {
6167
const customRepo = await getCustomRepo();
6268
if (customRepo === undefined) {
6369
// The user cancelled, do nothing.
@@ -70,7 +76,7 @@ export async function getRepositorySelection(): Promise<RepositorySelection> {
7076
}
7177
void extLogger.log(`Entered repository: ${customRepo}`);
7278
return { repositories: [customRepo] };
73-
} else if (quickpick?.useAllReposOfOwner) {
79+
} else if (quickpick.useAllReposOfOwner) {
7480
const owner = await getOwner();
7581
if (owner === undefined) {
7682
// The user cancelled, do nothing.
@@ -82,9 +88,9 @@ export async function getRepositorySelection(): Promise<RepositorySelection> {
8288
void extLogger.log(`Entered owner: ${owner}`);
8389
return { owners: [owner] };
8490
} else {
85-
// We don't need to display a warning pop-up in this case, since the user just escaped out of the operation.
86-
// We set 'true' to make this a silent exception.
87-
throw new UserCancellationException("No repositories selected", true);
91+
// This means the user has selected something, but there is nothing actually linked to this item. We want to show
92+
// this to the user.
93+
throw new UserCancellationException("No repositories selected", false);
8894
}
8995
}
9096

extensions/ql-vscode/src/vscode-tests/no-workspace/remote-queries/repository-selection.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ describe("repository selection", () => {
6565
expect(repoSelection.owners).toBeUndefined();
6666
expect(repoSelection.repositories).toEqual(["foo/bar", "foo/baz"]);
6767
});
68+
69+
it("should return an error for an empty repository list", async () => {
70+
// Fake return values
71+
quickPickSpy.mockResolvedValue({
72+
repositories: [],
73+
} as unknown as QuickPickItem);
74+
getRemoteRepositoryListsSpy.mockReturnValue({
75+
list1: ["foo/bar", "foo/baz"],
76+
list2: [],
77+
});
78+
79+
await expect(getRepositorySelection()).rejects.toThrow(
80+
"No repositories selected",
81+
);
82+
await expect(getRepositorySelection()).rejects.toThrow(
83+
UserCancellationException,
84+
);
85+
await expect(getRepositorySelection()).rejects.toHaveProperty(
86+
"silent",
87+
false,
88+
);
89+
});
6890
});
6991

7092
describe("system level repo lists", () => {
@@ -149,6 +171,10 @@ describe("repository selection", () => {
149171
await expect(getRepositorySelection()).rejects.toThrow(
150172
UserCancellationException,
151173
);
174+
await expect(getRepositorySelection()).rejects.toHaveProperty(
175+
"silent",
176+
true,
177+
);
152178
});
153179
});
154180

@@ -219,6 +245,10 @@ describe("repository selection", () => {
219245
await expect(getRepositorySelection()).rejects.toThrow(
220246
UserCancellationException,
221247
);
248+
await expect(getRepositorySelection()).rejects.toHaveProperty(
249+
"silent",
250+
true,
251+
);
222252
});
223253
});
224254

@@ -313,4 +343,20 @@ describe("repository selection", () => {
313343
expect(repoSelection.repositories).toEqual(["owner3/repo3"]);
314344
});
315345
});
346+
347+
it("should allow the user to cancel", async () => {
348+
// Fake return values
349+
quickPickSpy.mockResolvedValue(undefined);
350+
351+
await expect(getRepositorySelection()).rejects.toThrow(
352+
"No repositories selected",
353+
);
354+
await expect(getRepositorySelection()).rejects.toThrow(
355+
UserCancellationException,
356+
);
357+
await expect(getRepositorySelection()).rejects.toHaveProperty(
358+
"silent",
359+
true,
360+
);
361+
});
316362
});

0 commit comments

Comments
 (0)