@@ -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 */
8286export async function promptImportGithubDatabase (
8387 commandManager : AppCommandManager ,
@@ -103,6 +107,49 @@ 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+ * @param language the language to download. If undefined, the user will be prompted to choose a language.
142+ **/
143+ export async function downloadGitHubDatabase (
144+ githubRepo : string ,
145+ databaseManager : DatabaseManager ,
146+ storagePath : string ,
147+ credentials : Credentials | undefined ,
148+ progress : ProgressCallback ,
149+ token : CancellationToken ,
150+ cli ?: CodeQLCliServer ,
151+ language ?: string ,
152+ ) : Promise < DatabaseItem | undefined > {
106153 const nwo = getNwoFromGitHubUrl ( githubRepo ) || githubRepo ;
107154 if ( ! isValidGitHubNwo ( nwo ) ) {
108155 throw new Error ( `Invalid GitHub repository: ${ githubRepo } ` ) ;
@@ -112,7 +159,12 @@ export async function promptImportGithubDatabase(
112159 ? await credentials . getOctokit ( )
113160 : new Octokit . Octokit ( { retry } ) ;
114161
115- const result = await convertGithubNwoToDatabaseUrl ( nwo , octokit , progress ) ;
162+ const result = await convertGithubNwoToDatabaseUrl (
163+ nwo ,
164+ octokit ,
165+ progress ,
166+ language ,
167+ ) ;
116168 if ( ! result ) {
117169 return ;
118170 }
@@ -130,7 +182,7 @@ export async function promptImportGithubDatabase(
130182 * We only need the actual token string.
131183 */
132184 const octokitToken = ( ( await octokit . auth ( ) ) as { token : string } ) ?. token ;
133- const item = await databaseArchiveFetcher (
185+ return await databaseArchiveFetcher (
134186 databaseUrl ,
135187 {
136188 Accept : "application/zip" ,
@@ -143,14 +195,6 @@ export async function promptImportGithubDatabase(
143195 token ,
144196 cli ,
145197 ) ;
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 ;
154198}
155199
156200/**
@@ -450,6 +494,7 @@ export async function convertGithubNwoToDatabaseUrl(
450494 nwo : string ,
451495 octokit : Octokit . Octokit ,
452496 progress : ProgressCallback ,
497+ language ?: string ,
453498) : Promise <
454499 | {
455500 databaseUrl : string ;
@@ -468,9 +513,11 @@ export async function convertGithubNwoToDatabaseUrl(
468513
469514 const languages = response . data . map ( ( db : any ) => db . language ) ;
470515
471- const language = await promptForLanguage ( languages , progress ) ;
472- if ( ! language ) {
473- return ;
516+ if ( ! language || ! languages . includes ( language ) ) {
517+ language = await promptForLanguage ( languages , progress ) ;
518+ if ( ! language ) {
519+ return ;
520+ }
474521 }
475522
476523 return {
@@ -484,7 +531,7 @@ export async function convertGithubNwoToDatabaseUrl(
484531 }
485532}
486533
487- async function promptForLanguage (
534+ export async function promptForLanguage (
488535 languages : string [ ] ,
489536 progress : ProgressCallback ,
490537) : Promise < string | undefined > {
0 commit comments