Skip to content

Commit 0f82875

Browse files
committed
Allow raw project slugs for fetching lgtm dbs
The following is now acceptable for fetching the codeql lgtm database: ``` g/github/codeql ```
1 parent fd52f66 commit 0f82875

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- Allow using raw LGTM project slugs for fetching LGTM databases. [#769](https://github.com/github/vscode-codeql/pull/769)
6+
57
## 1.4.3 - 22 February 2021
68

79
- Avoid displaying an error when removing orphaned databases and the storage folder does not exist. [#748](https://github.com/github/vscode-codeql/pull/748)

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function promptImportLgtmDatabase(
7474
): Promise<DatabaseItem | undefined> {
7575
const lgtmUrl = await window.showInputBox({
7676
prompt:
77-
'Enter the project URL on LGTM (e.g., https://lgtm.com/projects/g/github/codeql)',
77+
'Enter the project slug or URL on LGTM (e.g., g/github/codeql or https://lgtm.com/projects/g/github/codeql)',
7878
});
7979
if (!lgtmUrl) {
8080
return;
@@ -352,13 +352,14 @@ export async function findDirWithFile(
352352

353353
/**
354354
* The URL pattern is https://lgtm.com/projects/{provider}/{org}/{name}/{irrelevant-subpages}.
355-
* There are several possibilities for the provider: in addition to GitHub.com(g),
355+
* There are several possibilities for the provider: in addition to GitHub.com (g),
356356
* LGTM currently hosts projects from Bitbucket (b), GitLab (gl) and plain git (git).
357357
*
358-
* After the {provider}/{org}/{name} path components, there may be the components
359-
* related to sub pages.
358+
* This function accepts any url that matches the pattern above. It also accepts the
359+
* raw project slug, e.g., `g/myorg/myproject`
360360
*
361-
* This function accepts any url that matches the patter above
361+
* After the `{provider}/{org}/{name}` path components, there may be the components
362+
* related to sub pages.
362363
*
363364
* @param lgtmUrl The URL to the lgtm project
364365
*
@@ -370,6 +371,10 @@ export function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string
370371
return false;
371372
}
372373

374+
if (convertRawLgtmSlug(lgtmUrl)) {
375+
return true;
376+
}
377+
373378
try {
374379
const uri = Uri.parse(lgtmUrl, true);
375380
if (uri.scheme !== 'https') {
@@ -387,9 +392,23 @@ export function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string
387392
}
388393
}
389394

395+
function convertRawLgtmSlug(maybeSlug: string): string | undefined {
396+
if (!maybeSlug) {
397+
return;
398+
}
399+
const segments = maybeSlug.split('/');
400+
const providers = ['g', 'gl', 'b', 'git'];
401+
if (segments.length === 3 && providers.includes(segments[0])) {
402+
return `https://lgtm.com/projects/${maybeSlug}`;
403+
}
404+
return;
405+
}
406+
390407
// exported for testing
391408
export async function convertToDatabaseUrl(lgtmUrl: string) {
392409
try {
410+
lgtmUrl = convertRawLgtmSlug(lgtmUrl) || lgtmUrl;
411+
393412
const uri = Uri.parse(lgtmUrl, true);
394413
const paths = ['api', 'v1.0'].concat(
395414
uri.path.split('/').filter((segment) => segment)

extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ describe('databaseFetcher', function() {
5555
);
5656
});
5757

58+
it('should convert a raw slug to a database url with extra path segments', async () => {
59+
quickPickSpy.resolves('python');
60+
const lgtmUrl =
61+
'g/github/codeql';
62+
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
63+
64+
expect(dbUrl).to.equal(
65+
'https://lgtm.com/api/v1.0/snapshots/1506465042581/python'
66+
);
67+
});
68+
5869
it('should fail on a nonexistant prohect', async () => {
5970
quickPickSpy.resolves('javascript');
6071
const lgtmUrl = 'https://lgtm.com/projects/g/github/hucairz';
@@ -71,6 +82,10 @@ describe('databaseFetcher', function() {
7182
.to.be.false;
7283
expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github')).to.be
7384
.false;
85+
expect(looksLikeLgtmUrl('g/github')).to.be
86+
.false;
87+
expect(looksLikeLgtmUrl('ggg/github/myproj')).to.be
88+
.false;
7489
});
7590

7691
it('should handle valid urls', () => {
@@ -86,6 +101,10 @@ describe('databaseFetcher', function() {
86101
'https://lgtm.com/projects/g/github/codeql/sub/pages?query=string'
87102
)
88103
).to.be.true;
104+
expect(looksLikeLgtmUrl('g/github/myproj')).to.be
105+
.true;
106+
expect(looksLikeLgtmUrl('git/github/myproj')).to.be
107+
.true;
89108
});
90109
});
91110

0 commit comments

Comments
 (0)