diff --git a/extensions/ql-vscode/CHANGELOG.md b/extensions/ql-vscode/CHANGELOG.md index ccbf40ccc4b..e7e9168f5f8 100644 --- a/extensions/ql-vscode/CHANGELOG.md +++ b/extensions/ql-vscode/CHANGELOG.md @@ -2,6 +2,8 @@ ## [UNRELEASED] +- Allow using raw LGTM project slugs for fetching LGTM databases. [#769](https://github.com/github/vscode-codeql/pull/769) + ## 1.4.3 - 22 February 2021 - Avoid displaying an error when removing orphaned databases and the storage folder does not exist. [#748](https://github.com/github/vscode-codeql/pull/748) diff --git a/extensions/ql-vscode/src/databaseFetcher.ts b/extensions/ql-vscode/src/databaseFetcher.ts index 49d8ff8d2af..8328fc309a3 100644 --- a/extensions/ql-vscode/src/databaseFetcher.ts +++ b/extensions/ql-vscode/src/databaseFetcher.ts @@ -74,7 +74,7 @@ export async function promptImportLgtmDatabase( ): Promise { const lgtmUrl = await window.showInputBox({ prompt: - 'Enter the project URL on LGTM (e.g., https://lgtm.com/projects/g/github/codeql)', + 'Enter the project slug or URL on LGTM (e.g., g/github/codeql or https://lgtm.com/projects/g/github/codeql)', }); if (!lgtmUrl) { return; @@ -352,13 +352,14 @@ export async function findDirWithFile( /** * The URL pattern is https://lgtm.com/projects/{provider}/{org}/{name}/{irrelevant-subpages}. - * There are several possibilities for the provider: in addition to GitHub.com(g), + * There are several possibilities for the provider: in addition to GitHub.com (g), * LGTM currently hosts projects from Bitbucket (b), GitLab (gl) and plain git (git). * - * After the {provider}/{org}/{name} path components, there may be the components - * related to sub pages. + * This function accepts any url that matches the pattern above. It also accepts the + * raw project slug, e.g., `g/myorg/myproject` * - * This function accepts any url that matches the patter above + * After the `{provider}/{org}/{name}` path components, there may be the components + * related to sub pages. * * @param lgtmUrl The URL to the lgtm project * @@ -370,6 +371,10 @@ export function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string return false; } + if (convertRawLgtmSlug(lgtmUrl)) { + return true; + } + try { const uri = Uri.parse(lgtmUrl, true); if (uri.scheme !== 'https') { @@ -387,9 +392,23 @@ export function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string } } +function convertRawLgtmSlug(maybeSlug: string): string | undefined { + if (!maybeSlug) { + return; + } + const segments = maybeSlug.split('/'); + const providers = ['g', 'gl', 'b', 'git']; + if (segments.length === 3 && providers.includes(segments[0])) { + return `https://lgtm.com/projects/${maybeSlug}`; + } + return; +} + // exported for testing export async function convertToDatabaseUrl(lgtmUrl: string) { try { + lgtmUrl = convertRawLgtmSlug(lgtmUrl) || lgtmUrl; + const uri = Uri.parse(lgtmUrl, true); const paths = ['api', 'v1.0'].concat( uri.path.split('/').filter((segment) => segment) diff --git a/extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts b/extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts index 8cd78276805..a0d11eaa833 100644 --- a/extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts @@ -55,6 +55,17 @@ describe('databaseFetcher', function() { ); }); + it('should convert a raw slug to a database url with extra path segments', async () => { + quickPickSpy.resolves('python'); + const lgtmUrl = + 'g/github/codeql'; + const dbUrl = await convertToDatabaseUrl(lgtmUrl); + + expect(dbUrl).to.equal( + 'https://lgtm.com/api/v1.0/snapshots/1506465042581/python' + ); + }); + it('should fail on a nonexistant prohect', async () => { quickPickSpy.resolves('javascript'); const lgtmUrl = 'https://lgtm.com/projects/g/github/hucairz'; @@ -71,6 +82,10 @@ describe('databaseFetcher', function() { .to.be.false; expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github')).to.be .false; + expect(looksLikeLgtmUrl('g/github')).to.be + .false; + expect(looksLikeLgtmUrl('ggg/github/myproj')).to.be + .false; }); it('should handle valid urls', () => { @@ -86,6 +101,10 @@ describe('databaseFetcher', function() { 'https://lgtm.com/projects/g/github/codeql/sub/pages?query=string' ) ).to.be.true; + expect(looksLikeLgtmUrl('g/github/myproj')).to.be + .true; + expect(looksLikeLgtmUrl('git/github/myproj')).to.be + .true; }); });