-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[Packagist] Update Packagist service to use v2 api #6508
Changes from all commits
0993750
83935ed
ffaa723
c8692bc
4d3c20b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,11 @@ const { | |
customServerDocumentationFragment, | ||
} = require('./packagist-base') | ||
|
||
const packageSchema = Joi.object() | ||
.pattern( | ||
/^/, | ||
const packageSchema = Joi.array() | ||
.items( | ||
Joi.object({ | ||
'default-branch': Joi.bool(), | ||
license: Joi.array().required(), | ||
version: Joi.string(), | ||
license: Joi.array(), | ||
}).required() | ||
) | ||
.required() | ||
|
@@ -59,17 +58,26 @@ module.exports = class PackagistLicense extends BasePackagistService { | |
} | ||
|
||
transform({ json, user, repo }) { | ||
const branch = this.getDefaultBranch(json, user, repo) | ||
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. We can delete |
||
if (!branch) { | ||
throw new NotFound({ prettyMessage: 'default branch not found' }) | ||
const packageName = this.getPackageName(user, repo) | ||
|
||
const decompressed = this.decompressResponse(json, packageName) | ||
|
||
const version = this.findRelease(decompressed) | ||
|
||
const license = version.license | ||
|
||
if (!license) { | ||
throw new NotFound({ prettyMessage: 'license not found' }) | ||
} | ||
const { license } = branch | ||
|
||
return { license } | ||
} | ||
|
||
async handle({ user, repo }, { server }) { | ||
const json = await this.fetch({ user, repo, schema, server }) | ||
|
||
const { license } = this.transform({ json, user, repo }) | ||
|
||
return renderLicenseBadge({ license }) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,17 +68,68 @@ module.exports = class PackagistPhpVersion extends BasePackagistService { | |
} | ||
} | ||
|
||
transform({ json, user, repo, version = '' }) { | ||
const packageVersion = | ||
version === '' | ||
? this.getDefaultBranch(json, user, repo) | ||
: json.packages[this.getPackageName(user, repo)][version] | ||
findVersionIndex(json, version) { | ||
return json.findIndex(v => v.version === version) | ||
} | ||
|
||
async findSpecifiedVersion(json, user, repo, version, server) { | ||
let release | ||
|
||
if ((release = json[this.findVersionIndex(json, version)])) { | ||
return release | ||
} else { | ||
try { | ||
const allData = await this.fetchDev({ | ||
user, | ||
repo, | ||
schema: allVersionsSchema, | ||
server, | ||
}) | ||
|
||
const decompressed = this.decompressResponse( | ||
allData, | ||
this.getPackageName(user, repo) | ||
) | ||
|
||
return decompressed[this.findVersionIndex(decompressed, version)] | ||
} catch (e) { | ||
return release | ||
} | ||
} | ||
} | ||
|
||
async transform({ json, user, repo, version = '', server }) { | ||
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. The logic path for this badge is a bit muddled
Here we are making an API call in transform, so that's a good indicator we've split the logic up incorrectly or named things poorly. In this situation, calling The "transform" stage (to the extent there is one for this badge) is just extracting |
||
let packageVersion | ||
const decompressed = this.decompressResponse( | ||
json, | ||
this.getPackageName(user, repo) | ||
) | ||
|
||
if (version === '') { | ||
packageVersion = this.findRelease(decompressed) | ||
} else { | ||
try { | ||
packageVersion = await this.findSpecifiedVersion( | ||
decompressed, | ||
user, | ||
repo, | ||
version, | ||
server | ||
) | ||
} catch { | ||
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. stylistically we always use |
||
packageVersion = null | ||
} | ||
} | ||
|
||
if (!packageVersion) { | ||
throw new NotFound({ prettyMessage: 'invalid version' }) | ||
} | ||
|
||
if (!packageVersion.require || !packageVersion.require.php) { | ||
if ( | ||
!packageVersion.require || | ||
!packageVersion.require.php || | ||
packageVersion.require.php === '__unset' | ||
) { | ||
throw new NotFound({ prettyMessage: 'version requirement not found' }) | ||
} | ||
|
||
|
@@ -92,11 +143,12 @@ module.exports = class PackagistPhpVersion extends BasePackagistService { | |
schema: allVersionsSchema, | ||
server, | ||
}) | ||
const { phpVersion } = this.transform({ | ||
const { phpVersion } = await this.transform({ | ||
json: allData, | ||
user, | ||
repo, | ||
version, | ||
server, | ||
}) | ||
return this.constructor.render({ php: phpVersion }) | ||
} | ||
|
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 function is not that clearly named. How about
findLatestRelease()
?Also, we should have one method of determining the latest (stable) version that we use consistently - we shouldn't have 2 different methods for different badges. More commentry on this on the
packagist-version.js
file..I don't think
versions
needs to be a function param here. We never pass anything in when we call it. How about we make this just