-
Notifications
You must be signed in to change notification settings - Fork 226
Case insensitive fallback check for github repositories #961
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
Changes from all commits
f00c7c7
d3f53f9
c9bebe8
3210039
f5c0f3f
81cb789
9f84867
8bdf06b
dc8373e
e855bdd
cfff4ad
dc06601
054eb11
54756d8
4d7d8b5
539a2cd
8112b55
a75278e
feec0e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,16 @@ export async function promptImportLgtmDatabase( | |
| return; | ||
| } | ||
|
|
||
| export async function retrieveCanonicalRepoName(lgtmUrl: string) { | ||
| const givenRepoName = extractProjectSlug(lgtmUrl); | ||
| const response = await checkForFailingResponse(await fetch(`https://api.github.com/repos/${givenRepoName}`), 'Failed to locate the repository on github'); | ||
| const repo = await response.json(); | ||
| if (!repo || !repo.full_name) { | ||
| return; | ||
| } | ||
| return repo.full_name; | ||
| } | ||
|
|
||
| /** | ||
| * Imports a database from a local archive. | ||
| * | ||
|
|
@@ -285,7 +295,7 @@ async function fetchAndUnzip( | |
| step: 1, | ||
| }); | ||
|
|
||
| const response = await checkForFailingResponse(await fetch(databaseUrl)); | ||
| const response = await checkForFailingResponse(await fetch(databaseUrl), 'Error downloading database'); | ||
| const archiveFileStream = fs.createWriteStream(archivePath); | ||
|
|
||
| const contentLength = response.headers.get('content-length'); | ||
|
|
@@ -304,7 +314,7 @@ async function fetchAndUnzip( | |
| await fs.remove(archivePath); | ||
| } | ||
|
|
||
| async function checkForFailingResponse(response: Response): Promise<Response | never> { | ||
| async function checkForFailingResponse(response: Response, errorMessage: string): Promise<Response | never> { | ||
| if (response.ok) { | ||
| return response; | ||
| } | ||
|
|
@@ -318,7 +328,7 @@ async function checkForFailingResponse(response: Response): Promise<Response | n | |
| } catch (e) { | ||
| msg = text; | ||
| } | ||
| throw new Error(`Error downloading database.\n\nReason: ${msg}`); | ||
| throw new Error(`${errorMessage}.\n\nReason: ${msg}`); | ||
| } | ||
|
|
||
| function isFile(databaseUrl: string) { | ||
|
|
@@ -408,24 +418,36 @@ function convertRawLgtmSlug(maybeSlug: string): string | undefined { | |
| } | ||
| return; | ||
| } | ||
|
|
||
| function extractProjectSlug(lgtmUrl: string): string | undefined { | ||
| // Only matches the '/g/' provider (github) | ||
| const re = new RegExp('https://lgtm.com/projects/g/(.*[^/])'); | ||
| const match = lgtmUrl.match(re); | ||
| if (!match) { | ||
| return; | ||
| } | ||
| return match[1]; | ||
| } | ||
|
|
||
| // exported for testing | ||
| export async function convertToDatabaseUrl( | ||
| lgtmUrl: string, | ||
| progress: ProgressCallback) { | ||
| try { | ||
| lgtmUrl = convertRawLgtmSlug(lgtmUrl) || lgtmUrl; | ||
|
|
||
| const uri = Uri.parse(lgtmUrl, true); | ||
| const paths = ['api', 'v1.0'].concat( | ||
| uri.path.split('/').filter((segment) => segment) | ||
| ).slice(0, 6); | ||
| const projectUrl = `https://lgtm.com/${paths.join('/')}`; | ||
| const projectResponse = await fetch(projectUrl); | ||
| const projectJson = await projectResponse.json(); | ||
| let projectJson = await downloadLgtmProjectMetadata(lgtmUrl); | ||
|
|
||
| if (projectJson.code === 404) { | ||
| throw new Error(); | ||
| // fallback check for github repos with same name but different case | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also mention that this will fail for all non-github providers. |
||
| let canonicalName = await retrieveCanonicalRepoName(lgtmUrl); | ||
| if (!canonicalName) { | ||
| throw new Error('Repository not found.'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the |
||
| } | ||
| canonicalName = convertRawLgtmSlug(`g/${canonicalName}`); | ||
| projectJson = await downloadLgtmProjectMetadata(canonicalName); | ||
| if (projectJson.code === 404) { | ||
| throw new Error('Failed to download project from LGTM.'); | ||
| } | ||
| } | ||
|
|
||
| const language = await promptForLanguage(projectJson, progress); | ||
|
|
@@ -445,6 +467,16 @@ export async function convertToDatabaseUrl( | |
| } | ||
| } | ||
|
|
||
| async function downloadLgtmProjectMetadata(lgtmUrl: string): Promise<any> { | ||
| const uri = Uri.parse(lgtmUrl, true); | ||
| const paths = ['api', 'v1.0'].concat( | ||
| uri.path.split('/').filter((segment) => segment) | ||
| ).slice(0, 6); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
| const projectUrl = `https://lgtm.com/${paths.join('/')}`; | ||
| const projectResponse = await fetch(projectUrl); | ||
| return projectResponse.json(); | ||
| } | ||
|
|
||
| async function promptForLanguage( | ||
| projectJson: any, | ||
| progress: ProgressCallback | ||
|
|
||
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.