Skip to content

Commit 16a8289

Browse files
committed
Shorten logic for deciding when to ask for language
Let's only ask for the language when the language is not provided OR it's invalid. Let's also add tests for these cases.
1 parent 59909e2 commit 16a8289

2 files changed

Lines changed: 90 additions & 56 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,18 @@ export async function convertGithubNwoToDatabaseUrl(
512512

513513
const languages = response.data.map((db: any) => db.language);
514514

515-
if (language && languages.includes(language)) {
516-
return {
517-
databaseUrl: `https://api.github.com/repos/${owner}/${repo}/code-scanning/codeql/databases/${language}`,
518-
owner,
519-
name: repo,
520-
};
521-
} else {
522-
const language = await promptForLanguage(languages, progress);
515+
if (!language || !languages.includes(language)) {
516+
language = await promptForLanguage(languages, progress);
523517
if (!language) {
524518
return;
525519
}
526-
527-
return {
528-
databaseUrl: `https://api.github.com/repos/${owner}/${repo}/code-scanning/codeql/databases/${language}`,
529-
owner,
530-
name: repo,
531-
};
532520
}
521+
522+
return {
523+
databaseUrl: `https://api.github.com/repos/${owner}/${repo}/code-scanning/codeql/databases/${language}`,
524+
owner,
525+
name: repo,
526+
};
533527
} catch (e) {
534528
void extLogger.log(`Error: ${getErrorMessage(e)}`);
535529
throw new Error(`Unable to get database for '${nwo}'`);

extensions/ql-vscode/test/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,56 @@ describe("databaseFetcher", () => {
2323
request: mockRequest,
2424
} as unknown as Octokit.Octokit;
2525

26+
// We can't make the real octokit request (since we need credentials), so we mock the response.
27+
const successfullMockApiResponse = {
28+
data: [
29+
{
30+
id: 1495869,
31+
name: "csharp-database",
32+
language: "csharp",
33+
uploader: {},
34+
content_type: "application/zip",
35+
state: "uploaded",
36+
size: 55599715,
37+
created_at: "2022-03-24T10:46:24Z",
38+
updated_at: "2022-03-24T10:46:27Z",
39+
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/csharp",
40+
},
41+
{
42+
id: 1100671,
43+
name: "database.zip",
44+
language: "javascript",
45+
uploader: {},
46+
content_type: "application/zip",
47+
state: "uploaded",
48+
size: 29294434,
49+
created_at: "2022-03-01T16:00:04Z",
50+
updated_at: "2022-03-01T16:00:06Z",
51+
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/javascript",
52+
},
53+
{
54+
id: 648738,
55+
name: "ql-database",
56+
language: "ql",
57+
uploader: {},
58+
content_type: "application/json; charset=utf-8",
59+
state: "uploaded",
60+
size: 39735500,
61+
created_at: "2022-02-02T09:38:50Z",
62+
updated_at: "2022-02-02T09:38:51Z",
63+
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/ql",
64+
},
65+
],
66+
};
67+
2668
beforeEach(() => {
2769
quickPickSpy = jest
2870
.spyOn(window, "showQuickPick")
2971
.mockResolvedValue(undefined);
3072
});
3173

3274
it("should convert a GitHub nwo to a database url", async () => {
33-
// We can't make the real octokit request (since we need credentials), so we mock the response.
34-
const mockApiResponse = {
35-
data: [
36-
{
37-
id: 1495869,
38-
name: "csharp-database",
39-
language: "csharp",
40-
uploader: {},
41-
content_type: "application/zip",
42-
state: "uploaded",
43-
size: 55599715,
44-
created_at: "2022-03-24T10:46:24Z",
45-
updated_at: "2022-03-24T10:46:27Z",
46-
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/csharp",
47-
},
48-
{
49-
id: 1100671,
50-
name: "database.zip",
51-
language: "javascript",
52-
uploader: {},
53-
content_type: "application/zip",
54-
state: "uploaded",
55-
size: 29294434,
56-
created_at: "2022-03-01T16:00:04Z",
57-
updated_at: "2022-03-01T16:00:06Z",
58-
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/javascript",
59-
},
60-
{
61-
id: 648738,
62-
name: "ql-database",
63-
language: "ql",
64-
uploader: {},
65-
content_type: "application/json; charset=utf-8",
66-
state: "uploaded",
67-
size: 39735500,
68-
created_at: "2022-02-02T09:38:50Z",
69-
updated_at: "2022-02-02T09:38:51Z",
70-
url: "https://api.github.com/repositories/143040428/code-scanning/codeql/databases/ql",
71-
},
72-
],
73-
};
74-
mockRequest.mockResolvedValue(mockApiResponse);
75+
mockRequest.mockResolvedValue(successfullMockApiResponse);
7576
quickPickSpy.mockResolvedValue(mockedQuickPickItem("javascript"));
7677
const githubRepo = "github/codeql";
7778
const result = await convertGithubNwoToDatabaseUrl(
@@ -127,6 +128,45 @@ describe("databaseFetcher", () => {
127128
).rejects.toThrow(/Unable to get database/);
128129
expect(progressSpy).toBeCalledTimes(1);
129130
});
131+
132+
describe("when language is already provided", () => {
133+
describe("when language is valid", () => {
134+
it("should not prompt the user", async () => {
135+
mockRequest.mockResolvedValue(successfullMockApiResponse);
136+
const githubRepo = "github/codeql";
137+
await convertGithubNwoToDatabaseUrl(
138+
githubRepo,
139+
octokit,
140+
progressSpy,
141+
"javascript",
142+
);
143+
expect(quickPickSpy).not.toHaveBeenCalled();
144+
});
145+
});
146+
147+
describe("when language is invalid", () => {
148+
it("should prompt for language", async () => {
149+
mockRequest.mockResolvedValue(successfullMockApiResponse);
150+
const githubRepo = "github/codeql";
151+
await convertGithubNwoToDatabaseUrl(
152+
githubRepo,
153+
octokit,
154+
progressSpy,
155+
"invalid-language",
156+
);
157+
expect(quickPickSpy).toHaveBeenCalled();
158+
});
159+
});
160+
});
161+
162+
describe("when language is not provided", () => {
163+
it("should prompt for language", async () => {
164+
mockRequest.mockResolvedValue(successfullMockApiResponse);
165+
const githubRepo = "github/codeql";
166+
await convertGithubNwoToDatabaseUrl(githubRepo, octokit, progressSpy);
167+
expect(quickPickSpy).toHaveBeenCalled();
168+
});
169+
});
130170
});
131171

132172
describe("findDirWithFile", () => {

0 commit comments

Comments
 (0)