Skip to content

Commit 1a2e5d9

Browse files
committed
Extract functionality to download GitHub database
This was nested in a method that included prompting the user for a github repo. We'd like to re-use this to download a database of our choice from GitHub, based on which language a user chooses.
1 parent 957f71c commit 1a2e5d9

1 file changed

Lines changed: 46 additions & 9 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export async function promptImportInternetDatabase(
7878
*
7979
* @param databaseManager the DatabaseManager
8080
* @param storagePath where to store the unzipped database.
81+
* @param credentials the credentials to use to authenticate with GitHub
82+
* @param progress the progress callback
83+
* @param token the cancellation token
84+
* @param cli the CodeQL CLI server
8185
*/
8286
export async function promptImportGithubDatabase(
8387
commandManager: AppCommandManager,
@@ -103,6 +107,47 @@ export async function promptImportGithubDatabase(
103107
return;
104108
}
105109

110+
const databaseItem = await downloadGitHubDatabase(
111+
githubRepo,
112+
databaseManager,
113+
storagePath,
114+
credentials,
115+
progress,
116+
token,
117+
cli,
118+
);
119+
120+
if (databaseItem) {
121+
await commandManager.execute("codeQLDatabases.focus");
122+
void showAndLogInformationMessage(
123+
"Database downloaded and imported successfully.",
124+
);
125+
return databaseItem;
126+
}
127+
128+
return;
129+
}
130+
131+
/**
132+
* Downloads a database from GitHub
133+
*
134+
* @param githubRepo the GitHub repository to download the database from
135+
* @param databaseManager the DatabaseManager
136+
* @param storagePath where to store the unzipped database.
137+
* @param credentials the credentials to use to authenticate with GitHub
138+
* @param progress the progress callback
139+
* @param token the cancellation token
140+
* @param cli the CodeQL CLI server
141+
**/
142+
export async function downloadGitHubDatabase(
143+
githubRepo: string,
144+
databaseManager: DatabaseManager,
145+
storagePath: string,
146+
credentials: Credentials | undefined,
147+
progress: ProgressCallback,
148+
token: CancellationToken,
149+
cli?: CodeQLCliServer,
150+
): Promise<DatabaseItem | undefined> {
106151
const nwo = getNwoFromGitHubUrl(githubRepo) || githubRepo;
107152
if (!isValidGitHubNwo(nwo)) {
108153
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
@@ -130,7 +175,7 @@ export async function promptImportGithubDatabase(
130175
* We only need the actual token string.
131176
*/
132177
const octokitToken = ((await octokit.auth()) as { token: string })?.token;
133-
const item = await databaseArchiveFetcher(
178+
return await databaseArchiveFetcher(
134179
databaseUrl,
135180
{
136181
Accept: "application/zip",
@@ -143,14 +188,6 @@ export async function promptImportGithubDatabase(
143188
token,
144189
cli,
145190
);
146-
if (item) {
147-
await commandManager.execute("codeQLDatabases.focus");
148-
void showAndLogInformationMessage(
149-
"Database downloaded and imported successfully.",
150-
);
151-
return item;
152-
}
153-
return;
154191
}
155192

156193
/**

0 commit comments

Comments
 (0)