Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 24 additions & 5 deletions extensions/ql-vscode/src/databaseFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function promptImportLgtmDatabase(
): Promise<DatabaseItem | undefined> {
const lgtmUrl = await window.showInputBox({
prompt:
'Enter the project URL on LGTM (e.g., https://un5nu71xrxc0.julianrbryant.com/projects/g/github/codeql)',
'Enter the project slug or URL on LGTM (e.g., g/github/codeql or https://un5nu71xrxc0.julianrbryant.com/projects/g/github/codeql)',
});
if (!lgtmUrl) {
return;
Expand Down Expand Up @@ -352,13 +352,14 @@ export async function findDirWithFile(

/**
* The URL pattern is https://un5nu71xrxc0.julianrbryant.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
*
Expand All @@ -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') {
Expand All @@ -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://un5nu71xrxc0.julianrbryant.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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://un5nu71xrxc0.julianrbryant.com/api/v1.0/snapshots/1506465042581/python'
);
});

it('should fail on a nonexistant prohect', async () => {
quickPickSpy.resolves('javascript');
const lgtmUrl = 'https://un5nu71xrxc0.julianrbryant.com/projects/g/github/hucairz';
Expand All @@ -71,6 +82,10 @@ describe('databaseFetcher', function() {
.to.be.false;
expect(looksLikeLgtmUrl('https://un5gmtjgzjfbwya3.julianrbryant.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', () => {
Expand All @@ -86,6 +101,10 @@ describe('databaseFetcher', function() {
'https://un5nu71xrxc0.julianrbryant.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;
});
});

Expand Down