@@ -10,7 +10,6 @@ import {
1010 pathExists ,
1111 createWriteStream ,
1212 remove ,
13- stat ,
1413 readdir ,
1514} from "fs-extra" ;
1615import { basename , join } from "path" ;
@@ -36,11 +35,12 @@ import {
3635} from "../config" ;
3736import { showAndLogInformationMessage } from "../common/logging" ;
3837import { AppOctokit } from "../common/octokit" ;
39- import { getLanguageDisplayName } from "../common/query-language" ;
4038import type { DatabaseOrigin } from "./local-databases/database-origin" ;
4139import { createTimeoutSignal } from "../common/fetch-stream" ;
4240import type { App } from "../common/app" ;
4341import { createFilenameFromString } from "../common/filenames" ;
42+ import { findDirWithFile } from "../common/files" ;
43+ import { convertGithubNwoToDatabaseUrl } from "./github-databases/api" ;
4444
4545/**
4646 * Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
@@ -406,6 +406,7 @@ async function databaseArchiveFetcher(
406406 nameOverride ,
407407 {
408408 addSourceArchiveFolder,
409+ extensionManagedLocation : unzipPath ,
409410 } ,
410411 ) ;
411412 return item ;
@@ -617,125 +618,6 @@ function isFile(databaseUrl: string) {
617618 return Uri . parse ( databaseUrl ) . scheme === "file" ;
618619}
619620
620- /**
621- * Recursively looks for a file in a directory. If the file exists, then returns the directory containing the file.
622- *
623- * @param dir The directory to search
624- * @param toFind The file to recursively look for in this directory
625- *
626- * @returns the directory containing the file, or undefined if not found.
627- */
628- // exported for testing
629- export async function findDirWithFile (
630- dir : string ,
631- ...toFind : string [ ]
632- ) : Promise < string | undefined > {
633- if ( ! ( await stat ( dir ) ) . isDirectory ( ) ) {
634- return ;
635- }
636- const files = await readdir ( dir ) ;
637- if ( toFind . some ( ( file ) => files . includes ( file ) ) ) {
638- return dir ;
639- }
640- for ( const file of files ) {
641- const newPath = join ( dir , file ) ;
642- const result = await findDirWithFile ( newPath , ...toFind ) ;
643- if ( result ) {
644- return result ;
645- }
646- }
647- return ;
648- }
649-
650- export async function convertGithubNwoToDatabaseUrl (
651- nwo : string ,
652- octokit : Octokit ,
653- progress : ProgressCallback ,
654- language ?: string ,
655- ) : Promise <
656- | {
657- databaseUrl : string ;
658- owner : string ;
659- name : string ;
660- databaseId : number ;
661- databaseCreatedAt : string ;
662- commitOid : string | null ;
663- }
664- | undefined
665- > {
666- try {
667- const [ owner , repo ] = nwo . split ( "/" ) ;
668-
669- const response = await octokit . rest . codeScanning . listCodeqlDatabases ( {
670- owner,
671- repo,
672- } ) ;
673-
674- const languages = response . data . map ( ( db ) => db . language ) ;
675-
676- if ( ! language || ! languages . includes ( language ) ) {
677- language = await promptForLanguage ( languages , progress ) ;
678- if ( ! language ) {
679- return ;
680- }
681- }
682-
683- const databaseForLanguage = response . data . find (
684- ( db ) => db . language === language ,
685- ) ;
686- if ( ! databaseForLanguage ) {
687- throw new Error ( `No database found for language '${ language } '` ) ;
688- }
689-
690- return {
691- databaseUrl : databaseForLanguage . url ,
692- owner,
693- name : repo ,
694- databaseId : databaseForLanguage . id ,
695- databaseCreatedAt : databaseForLanguage . created_at ,
696- commitOid : databaseForLanguage . commit_oid ?? null ,
697- } ;
698- } catch ( e ) {
699- void extLogger . log ( `Error: ${ getErrorMessage ( e ) } ` ) ;
700- throw new Error ( `Unable to get database for '${ nwo } '` ) ;
701- }
702- }
703-
704- export async function promptForLanguage (
705- languages : string [ ] ,
706- progress : ProgressCallback | undefined ,
707- ) : Promise < string | undefined > {
708- progress ?.( {
709- message : "Choose language" ,
710- step : 2 ,
711- maxStep : 2 ,
712- } ) ;
713- if ( ! languages . length ) {
714- throw new Error ( "No databases found" ) ;
715- }
716- if ( languages . length === 1 ) {
717- return languages [ 0 ] ;
718- }
719-
720- const items = languages
721- . map ( ( language ) => ( {
722- label : getLanguageDisplayName ( language ) ,
723- description : language ,
724- language,
725- } ) )
726- . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ;
727-
728- const selectedItem = await window . showQuickPick ( items , {
729- placeHolder : "Select the database language to download:" ,
730- ignoreFocusOut : true ,
731- } ) ;
732- if ( ! selectedItem ) {
733- return undefined ;
734- }
735-
736- return selectedItem . language ;
737- }
738-
739621/**
740622 * Databases created by the old odasa tool will not have a zipped
741623 * source location. However, this extension works better if sources
0 commit comments