Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case insensitive fallback check for github repositories #961

Closed
wants to merge 19 commits into from

Conversation

mgsium
Copy link
Contributor

@mgsium mgsium commented Oct 4, 2021

Implements a case-insensitive fallback check for the /g/ provider where a repository is not found on LGTM using a case-sensitive search. Partially fixes #892

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.

@mgsium mgsium requested a review from a team as a code owner October 4, 2021 22:36
Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Some comments inline.

@@ -107,6 +107,15 @@ export async function promptImportLgtmDatabase(
return;
}

export async function retrieveCanonicalRepoName(lgtmUrl: string) {
const givenRepoName = parseLgtmUrl(lgtmUrl);
const repo = await fetch(`https://api.github.com/repos/${givenRepoName}`).then(res => res.json());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is an error here and something besides 200 is returned? Can you take a look at checkForFailingResponse in this file? It would be best to to generalize this function to handle this case as well. You could add a new parameter that would replace the error message Error downloading database..

@@ -409,23 +418,31 @@ function convertRawLgtmSlug(maybeSlug: string): string | undefined {
return;
}

function parseLgtmUrl(lgtmUrl: string): string | undefined {
const re = new RegExp('https://lgtm.com/projects/(g|gl|b|git)/(.*)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only project slugs that begin with a g are from github (they are the vast majority of projects on lgtm. It may not be safe to match on other slugs. Eg- if two people put up the different projects with the same project name with different capitalization on github and bitbucket, you would get a mismatch. It's a rare case, but it's probably safer to protect against it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please add a comment that this only supports github slugs.

extensions/ql-vscode/src/databaseFetcher.ts Outdated Show resolved Hide resolved
throw new Error();
}
canonicalName = convertRawLgtmSlug(`g/${canonicalName}`);
projectJson = await pullLgtmProject(canonicalName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check again if projectJson.code === 404.

extensions/ql-vscode/src/databaseFetcher.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there...

@@ -408,24 +418,36 @@ function convertRawLgtmSlug(maybeSlug: string): string | undefined {
}
return;
}

function parseLgtmUrl(lgtmUrl: string): string | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is misleading. This function does not parse a url. It really just extracts the project slug. Could you rename it to something like: extractProjectSlug

// fallback check for github repos with same name but different case
let canonicalName = await retrieveCanonicalRepoName(lgtmUrl);
if (!canonicalName) {
throw new Error();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include an error message here.

canonicalName = convertRawLgtmSlug(`g/${canonicalName}`);
projectJson = await downloadLgtmProjectMetadata(canonicalName);
if (projectJson.code === 404) {
throw new Error();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried this out locally and found a handful of more issues.

extensions/ql-vscode/src/databaseFetcher.ts Outdated Show resolved Hide resolved

function parseLgtmUrl(lgtmUrl: string): string | undefined {
// Only matches the '/g/' provider (github)
const re = new RegExp('https://lgtm.com/projects/g/(.*)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to handle the case when there is a trailing /. You can either change the regex so that it will ignore capturing the / or add a check after and remove it.

Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just some smaller suggestions.

Comment on lines 501 to 506
let resolved;
try {
resolved = await cliServer.resolveQlref(selectedQuery.path.replace(/^\/([a-zA-Z]:)/, '$1'));
} catch {
throw new Error('Referenced .ql file could not be found.');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part of the same PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is related to #970, I must have mixed up the commits for these.


## 1.5.6 - 07 October 2021

- Add progress messages to LGTM download option. This makes the two-step process (selecting a project, then selecting a language) more clear. [#960](https://github.com/github/vscode-codeql/pull/960)
- Remove line about selecting a language from the dropdown when downloading database from LGTM. This makes the download progress visible when the popup is not expanded. [#957](https://github.com/github/vscode-codeql/pull/957)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should have been deleted.

const uri = Uri.file(resolved.resolvedPath);
// Something wrong with the uri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment can be removed.

const uri = Uri.parse(lgtmUrl, true);
const paths = ['api', 'v1.0'].concat(
uri.path.split('/').filter((segment) => segment)
).slice(0, 6);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 0, 6? Could you add a comment to explain?

// fallback check for github repos with same name but different case
let canonicalName = await retrieveCanonicalRepoName(lgtmUrl);
if (!canonicalName) {
throw new Error('Repository not found.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the lgtmUrl in the error message.


if (projectJson.code === 404) {
throw new Error();
// fallback check for github repos with same name but different case
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention that this will fail for all non-github providers.

@mgsium
Copy link
Contributor Author

mgsium commented Oct 19, 2021

Unrelated changes in the PR; reopening to clean up.

@mgsium mgsium closed this Oct 19, 2021
mgsium added a commit to mgsium/vscode-codeql that referenced this pull request Oct 20, 2021
Made suggested changes; comments can be found at github#961
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Download from LGTM command requires a case-sensitive slug
4 participants