Skip to content

Commit a541b11

Browse files
committed
Add more flexibility with Uri parsing for LGTM
Ensure that providers other than `g` are accepted and that subpages are ignored.
1 parent e2771a8 commit a541b11

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export async function promptImportLgtmDatabase(
8282
prompt:
8383
"Enter the project URL on LGTM (e.g., https://lgtm.com/projects/g/github/codeql)",
8484
});
85+
if (!lgtmUrl) {
86+
return;
87+
}
8588
if (looksLikeLgtmUrl(lgtmUrl)) {
8689
const databaseUrl = await convertToDatabaseUrl(lgtmUrl);
8790
if (databaseUrl) {
@@ -323,7 +326,20 @@ async function findDirWithFile(
323326
return;
324327
}
325328

326-
function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string {
329+
/**
330+
* The URL pattern is https://lgtm.com/projects/{provider}/{org}/{name}/{irrelevant-subpages}.
331+
* There are several possibilities for the provider: in addition to GitHub.com(g),
332+
* LGTM currently hosts projects from Bitbucket (b), GitLab (gl) and plain git (git).
333+
*
334+
* After the {provider}/{org}/{name} path components, there may be the components
335+
* related to sub pages.
336+
*
337+
* This function accepts any url that matches the patter above
338+
*
339+
* @param lgtmUrl The URL to the lgtm project
340+
*
341+
* @return true if this looks like an LGTM project url
342+
*/
327343
if (!lgtmUrl) {
328344
return false;
329345
}
@@ -339,7 +355,7 @@ function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string {
339355
}
340356

341357
const paths = uri.path.split("/").filter((segment) => segment);
342-
return paths.length === 4 && paths[0] === "projects" && paths[1] === "g";
358+
return paths.length >= 4 && paths[0] === "projects";
343359
} catch (e) {
344360
return false;
345361
}
@@ -350,7 +366,7 @@ async function convertToDatabaseUrl(lgtmUrl: string) {
350366
const uri = Uri.parse(lgtmUrl, true);
351367
const paths = ["api", "v1.0"].concat(
352368
uri.path.split("/").filter((segment) => segment)
353-
);
369+
).slice(0, 6);
354370
const projectUrl = `https://lgtm.com/${paths.join("/")}`;
355371
const projectResponse = await fetch(projectUrl);
356372
const projectJson = await projectResponse.json();
@@ -371,6 +387,7 @@ async function convertToDatabaseUrl(lgtmUrl: string) {
371387
language,
372388
].join("/")}`;
373389
} catch (e) {
390+
logger.log(`Error: ${e.message}`);
374391
throw new Error(`Invalid LGTM URL: ${lgtmUrl}`);
375392
}
376393
}

0 commit comments

Comments
 (0)