@@ -153,74 +153,6 @@ export async function promptImportGithubDatabase(
153153 return ;
154154}
155155
156- /**
157- * Prompts a user to fetch a database from lgtm.
158- * User enters a project url and then the user is asked which language
159- * to download (if there is more than one)
160- *
161- * @param databaseManager the DatabaseManager
162- * @param storagePath where to store the unzipped database.
163- */
164- export async function promptImportLgtmDatabase (
165- databaseManager : DatabaseManager ,
166- storagePath : string ,
167- progress : ProgressCallback ,
168- token : CancellationToken ,
169- cli ?: CodeQLCliServer ,
170- ) : Promise < DatabaseItem | undefined > {
171- progress ( {
172- message : "Choose project" ,
173- step : 1 ,
174- maxStep : 2 ,
175- } ) ;
176- const lgtmUrl = await window . showInputBox ( {
177- prompt :
178- "Enter the project slug or URL on LGTM (e.g., g/github/codeql or https://lgtm.com/projects/g/github/codeql)" ,
179- } ) ;
180- if ( ! lgtmUrl ) {
181- return ;
182- }
183-
184- if ( looksLikeLgtmUrl ( lgtmUrl ) ) {
185- const databaseUrl = await convertLgtmUrlToDatabaseUrl ( lgtmUrl , progress ) ;
186- if ( databaseUrl ) {
187- const item = await databaseArchiveFetcher (
188- databaseUrl ,
189- { } ,
190- databaseManager ,
191- storagePath ,
192- undefined ,
193- progress ,
194- token ,
195- cli ,
196- ) ;
197- if ( item ) {
198- await commands . executeCommand ( "codeQLDatabases.focus" ) ;
199- void showAndLogInformationMessage (
200- "Database downloaded and imported successfully." ,
201- ) ;
202- }
203- return item ;
204- }
205- } else {
206- throw new Error ( `Invalid LGTM URL: ${ lgtmUrl } ` ) ;
207- }
208- return ;
209- }
210-
211- export async function retrieveCanonicalRepoName ( lgtmUrl : string ) {
212- const givenRepoName = extractProjectSlug ( lgtmUrl ) ;
213- const response = await checkForFailingResponse (
214- await fetch ( `https://api.github.com/repos/${ givenRepoName } ` ) ,
215- "Failed to locate the repository on github" ,
216- ) ;
217- const repo = await response . json ( ) ;
218- if ( ! repo || ! repo . full_name ) {
219- return ;
220- }
221- return repo . full_name ;
222- }
223-
224156/**
225157 * Imports a database from a local archive.
226158 *
@@ -552,127 +484,6 @@ export async function convertGithubNwoToDatabaseUrl(
552484 }
553485}
554486
555- /**
556- * The URL pattern is https://lgtm.com/projects/{provider}/{org}/{name}/{irrelevant-subpages}.
557- * There are several possibilities for the provider: in addition to GitHub.com (g),
558- * LGTM currently hosts projects from Bitbucket (b), GitLab (gl) and plain git (git).
559- *
560- * This function accepts any url that matches the pattern above. It also accepts the
561- * raw project slug, e.g., `g/myorg/myproject`
562- *
563- * After the `{provider}/{org}/{name}` path components, there may be the components
564- * related to sub pages.
565- *
566- * @param lgtmUrl The URL to the lgtm project
567- *
568- * @return true if this looks like an LGTM project url
569- */
570- // exported for testing
571- export function looksLikeLgtmUrl (
572- lgtmUrl : string | undefined ,
573- ) : lgtmUrl is string {
574- if ( ! lgtmUrl ) {
575- return false ;
576- }
577-
578- if ( convertRawLgtmSlug ( lgtmUrl ) ) {
579- return true ;
580- }
581-
582- try {
583- const uri = Uri . parse ( lgtmUrl , true ) ;
584- if ( uri . scheme !== "https" ) {
585- return false ;
586- }
587-
588- if ( uri . authority !== "lgtm.com" && uri . authority !== "www.lgtm.com" ) {
589- return false ;
590- }
591-
592- const paths = uri . path . split ( "/" ) . filter ( ( segment : string ) => segment ) ;
593- return paths . length >= 4 && paths [ 0 ] === "projects" ;
594- } catch ( e ) {
595- return false ;
596- }
597- }
598-
599- function convertRawLgtmSlug ( maybeSlug : string ) : string | undefined {
600- if ( ! maybeSlug ) {
601- return ;
602- }
603- const segments = maybeSlug . split ( "/" ) ;
604- const providers = [ "g" , "gl" , "b" , "git" ] ;
605- if ( segments . length === 3 && providers . includes ( segments [ 0 ] ) ) {
606- return `https://lgtm.com/projects/${ maybeSlug } ` ;
607- }
608- return ;
609- }
610-
611- function extractProjectSlug ( lgtmUrl : string ) : string | undefined {
612- // Only matches the '/g/' provider (github)
613- const re = new RegExp ( "https://lgtm.com/projects/g/(.*[^/])" ) ;
614- const match = lgtmUrl . match ( re ) ;
615- if ( ! match ) {
616- return ;
617- }
618- return match [ 1 ] ;
619- }
620-
621- // exported for testing
622- export async function convertLgtmUrlToDatabaseUrl (
623- lgtmUrl : string ,
624- progress : ProgressCallback ,
625- ) {
626- try {
627- lgtmUrl = convertRawLgtmSlug ( lgtmUrl ) || lgtmUrl ;
628- let projectJson = await downloadLgtmProjectMetadata ( lgtmUrl ) ;
629-
630- if ( projectJson . code === 404 ) {
631- // fallback check for github repositories with same name but different case
632- // will fail for other providers
633- let canonicalName = await retrieveCanonicalRepoName ( lgtmUrl ) ;
634- if ( ! canonicalName ) {
635- throw new Error ( `Project was not found at ${ lgtmUrl } .` ) ;
636- }
637- canonicalName = convertRawLgtmSlug ( `g/${ canonicalName } ` ) ;
638- projectJson = await downloadLgtmProjectMetadata ( canonicalName ) ;
639- if ( projectJson . code === 404 ) {
640- throw new Error ( "Failed to download project from LGTM." ) ;
641- }
642- }
643-
644- const languages =
645- projectJson ?. languages ?. map (
646- ( lang : { language : string } ) => lang . language ,
647- ) || [ ] ;
648-
649- const language = await promptForLanguage ( languages , progress ) ;
650- if ( ! language ) {
651- return ;
652- }
653- return `https://lgtm.com/${ [
654- "api" ,
655- "v1.0" ,
656- "snapshots" ,
657- projectJson . id ,
658- language ,
659- ] . join ( "/" ) } `;
660- } catch ( e ) {
661- void extLogger . log ( `Error: ${ getErrorMessage ( e ) } ` ) ;
662- throw new Error ( `Invalid LGTM URL: ${ lgtmUrl } ` ) ;
663- }
664- }
665-
666- async function downloadLgtmProjectMetadata ( lgtmUrl : string ) : Promise < any > {
667- const uri = Uri . parse ( lgtmUrl , true ) ;
668- const paths = [ "api" , "v1.0" ]
669- . concat ( uri . path . split ( "/" ) . filter ( ( segment : string ) => segment ) )
670- . slice ( 0 , 6 ) ;
671- const projectUrl = `https://lgtm.com/${ paths . join ( "/" ) } ` ;
672- const projectResponse = await fetch ( projectUrl ) ;
673- return projectResponse . json ( ) ;
674- }
675-
676487async function promptForLanguage (
677488 languages : string [ ] ,
678489 progress : ProgressCallback ,
0 commit comments