-
Notifications
You must be signed in to change notification settings - Fork 189
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
Conversation
There was a problem hiding this 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()); |
There was a problem hiding this comment.
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)/(.*)'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
throw new Error(); | ||
} | ||
canonicalName = convertRawLgtmSlug(`g/${canonicalName}`); | ||
projectJson = await pullLgtmProject(canonicalName); |
There was a problem hiding this comment.
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
.
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
There was a problem hiding this 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.
|
||
function parseLgtmUrl(lgtmUrl: string): string | undefined { | ||
// Only matches the '/g/' provider (github) | ||
const re = new RegExp('https://lgtm.com/projects/g/(.*)'); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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.'); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.'); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Co-authored-by: Andrew Eisenberg <[email protected]>
Co-authored-by: Andrew Eisenberg <[email protected]>
Co-authored-by: Andrew Eisenberg <[email protected]>
Co-authored-by: Andrew Eisenberg <[email protected]>
Unrelated changes in the PR; reopening to clean up. |
Made suggested changes; comments can be found at github#961
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 #892Checklist