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

improve [MavenCentral], [MavenMetadata], and [GradlePluginPortal] #6628

Merged
merged 24 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6b4f111
implement version prefix and suffix
anatawa12 Jun 12, 2021
33cf8bd
add tests for maven-metadata
anatawa12 Jun 12, 2021
dd47a0c
use maven-metadata in maven-central
anatawa12 Jun 12, 2021
d8c71c3
add tests of maven-central for redirecting
anatawa12 Jun 12, 2021
3729734
add tests of maven-central for redirecting
anatawa12 Jun 12, 2021
1ec3ebe
fix: maven-central: don't force color as blue
anatawa12 Jun 12, 2021
059fcdb
add versionPrefix for gradle-plugin-portal
anatawa12 Jun 12, 2021
b888cf9
add example for gradle-plugin-portal
anatawa12 Jun 12, 2021
cfff903
fix: gradle-plugin-portal: don't force color as blue
anatawa12 Jun 12, 2021
3754db5
fix test
anatawa12 Jun 12, 2021
2c35b3e
fix test
anatawa12 Jun 12, 2021
c9f7b7f
fix test
anatawa12 Jun 12, 2021
166a0df
remove expectBadge tests for maven central
anatawa12 Jun 13, 2021
559eb5f
fix test for maven-metadata
anatawa12 Jun 13, 2021
ba51afa
remove :versionPrefix from gradle-plugin-portal
anatawa12 Jun 23, 2021
f2adc83
use query parameter versionPrefix in maven-central example
anatawa12 Jun 23, 2021
32368dc
fix redundant pattern
anatawa12 Jun 23, 2021
c3927d4
remove some examples
anatawa12 Jun 23, 2021
37d3476
fix static preview of gradle-plugin-portal
anatawa12 Jun 26, 2021
845f645
fix tests
anatawa12 Jun 26, 2021
ee9a94d
add documentation
anatawa12 Jun 28, 2021
22284b8
fix static preview of gradle-plugin-portal
anatawa12 Jun 28, 2021
5cb4f36
fix: version prefix or suffix not found if either versionPrefix or ve…
anatawa12 Jun 28, 2021
1426170
Merge branch 'master' into maven-central-and-metadata
PyvesB Jun 30, 2021
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
10 changes: 7 additions & 3 deletions services/gradle-plugin-portal/gradle-plugin-portal.service.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
'use strict'

const { redirector } = require('..')
const { documentation } = require('../maven-metadata/maven-metadata')

module.exports = redirector({
category: 'version',
isDeprecated: false,
route: {
base: 'gradle-plugin-portal/v',
// TODO: add /:versionPrefix? when maven-metadata have versionPrefix attribute.
pattern: ':pluginId',
},
examples: [
{
title: 'Gradle Plugin Portal',
queryParams: {
versionSuffix: '.1',
versionPrefix: '0.10',
},
namedParams: {
pluginId: 'com.gradle.plugin-publish',
},
staticPreview: {
label: 'plugin portal',
message: 'v0.14.0',
message: 'v0.10.1',
color: 'blue',
},
documentation,
},
],
transformPath: () => `/maven-metadata/v`,
Expand All @@ -31,7 +36,6 @@ module.exports = redirector({
return {
metadataUrl,
label: 'plugin portal',
color: 'blue',
}
},
overrideTransformedQueryParams: true,
Expand Down
4 changes: 2 additions & 2 deletions services/gradle-plugin-portal/gradle-plugin-portal.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const t = (module.exports = require('../tester').createServiceTester())
t.create('gradle plugin portal')
.get('/com.gradle.plugin-publish')
.expectRedirect(
`/maven-metadata/v.svg?color=blue&label=plugin%20portal&metadataUrl=${encodeURIComponent(
`/maven-metadata/v.svg?label=plugin%20portal&metadataUrl=${encodeURIComponent(
'https://plugins.gradle.org/m2/com/gradle/plugin-publish/com.gradle.plugin-publish.gradle.plugin/maven-metadata.xml'
)}`
)

t.create('gradle plugin portal with custom labels')
.get('/com.gradle.plugin-publish?label=custom%20label')
.expectRedirect(
`/maven-metadata/v.svg?color=blue&label=custom%20label&metadataUrl=${encodeURIComponent(
`/maven-metadata/v.svg?label=custom%20label&metadataUrl=${encodeURIComponent(
'https://plugins.gradle.org/m2/com/gradle/plugin-publish/com.gradle.plugin-publish.gradle.plugin/maven-metadata.xml'
)}`
)
Expand Down
90 changes: 27 additions & 63 deletions services/maven-central/maven-central.service.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,46 @@
'use strict'

const Joi = require('joi')
const { renderVersionBadge } = require('../version')
const { BaseXmlService, NotFound } = require('..')
const { redirector } = require('..')
const { documentation } = require('../maven-metadata/maven-metadata')

const schema = Joi.object({
metadata: Joi.object({
versioning: Joi.object({
versions: Joi.object({
version: Joi.array().items(Joi.string().required()).single().required(),
}).required(),
}).required(),
}).required(),
}).required()

module.exports = class MavenCentral extends BaseXmlService {
static category = 'version'

static route = {
module.exports = redirector({
category: 'version',
isDeprecated: false,
route: {
base: 'maven-central/v',
pattern: ':groupId/:artifactId/:versionPrefix?',
}

static examples = [
},
examples: [
{
title: 'Maven Central',
pattern: ':groupId/:artifactId',
namedParams: {
groupId: 'org.apache.maven',
artifactId: 'apache-maven',
queryParams: {
versionSuffix: '-android',
versionPrefix: '29',
},
staticPreview: {
label: 'maven-central',
message: 'v3.6.0',
color: 'blue',
},
},
{
title: 'Maven Central with version prefix filter',
pattern: ':groupId/:artifactId/:versionPrefix',
namedParams: {
groupId: 'org.apache.maven',
artifactId: 'apache-maven',
versionPrefix: '2',
groupId: 'com.google.guava',
artifactId: 'guava',
},
staticPreview: {
label: 'maven-central',
message: 'v2.2.1',
message: 'v29.0-android',
color: 'blue',
},
documentation,
},
]

static defaultBadgeData = {
label: 'maven-central',
}

async fetch({ groupId, artifactId }) {
],
transformPath: () => `/maven-metadata/v`,
transformQueryParams: ({ groupId, artifactId, versionPrefix }) => {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
const url = `https://repo1.maven.org/maven2/${group}/${artifact}/maven-metadata.xml`
return this._requestXml({
schema,
url,
parserOptions: { parseNodeValue: false },
})
}

async handle({ groupId, artifactId, versionPrefix }) {
const data = await this.fetch({ groupId, artifactId })
const versions = data.metadata.versioning.versions.version.reverse()
let version = versions[0]
if (versionPrefix !== undefined) {
version = versions.filter(v => v.toString().startsWith(versionPrefix))[0]
// if the filter returned no results, throw a NotFound
if (version === undefined)
throw new NotFound({ prettyMessage: 'version prefix not found' })
const metadataUrl = `https://repo1.maven.org/maven2/${group}/${artifact}/maven-metadata.xml`
return {
metadataUrl,
label: 'maven-central',
versionPrefix,
}
return renderVersionBadge({ version })
}
}
},
overrideTransformedQueryParams: true,
dateAdded: new Date('2021-06-12'),
})
56 changes: 11 additions & 45 deletions services/maven-central/maven-central.tester.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,19 @@
'use strict'

const {
isVPlusDottedVersionNClausesWithOptionalSuffix,
} = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('latest version')
t.create('latest version redirection')
.get('/com.github.fabriziocucci/yacl4j.json') // http://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/
.expectBadge({
label: 'maven-central',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})
.expectRedirect(
`/maven-metadata/v.json?label=maven-central&metadataUrl=${encodeURIComponent(
'https://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/maven-metadata.xml'
)}`
)

t.create('latest 0.8 version')
t.create('latest 0.8 version redirection')
.get('/com.github.fabriziocucci/yacl4j/0.8.json') // http://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/
.expectBadge({
label: 'maven-central',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('inexistent artifact')
.get('/inexistent-group-id/inexistent-artifact-id.json')
.expectBadge({ label: 'maven-central', message: 'not found' })

t.create('inexistent version prefix')
.get('/com.github.fabriziocucci/yacl4j/99.json')
.expectBadge({ label: 'maven-central', message: 'version prefix not found' })

t.create('version ending with zero')
.get('/mocked-group-id/mocked-artifact-id.json')
.intercept(nock =>
nock('https://repo1.maven.org/maven2')
.get('/mocked-group-id/mocked-artifact-id/maven-metadata.xml')
.reply(
200,
`
<metadata>
<groupId>mocked-group-id</groupId>
<artifactId>mocked-artifact-id</artifactId>
<versioning>
<latest>1.30</latest>
<release>1.30</release>
<versions>
<version>1.30</version>
</versions>
<lastUpdated>20190902002617</lastUpdated>
</versioning>
</metadata>
`
)
.expectRedirect(
`/maven-metadata/v.json?label=maven-central&metadataUrl=${encodeURIComponent(
'https://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/maven-metadata.xml'
)}&versionPrefix=0.8`
)
.expectBadge({ label: 'maven-central', message: 'v1.30' })
13 changes: 13 additions & 0 deletions services/maven-metadata/maven-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

// the file contains common constants for badges uses maven-metadata

module.exports = {}

module.exports.documentation = `
<p>
<code>versionPrefix</code> and <code>versionSuffix</code> allow narrowing down
the range of versions the badge will take into account,
but they are completely optional.
</p>
`
34 changes: 27 additions & 7 deletions services/maven-metadata/maven-metadata.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
const Joi = require('joi')
const { optionalUrl } = require('../validators')
const { renderVersionBadge } = require('../version')
const { BaseXmlService } = require('..')
const { BaseXmlService, NotFound } = require('..')
const { documentation } = require('./maven-metadata')

const queryParamSchema = Joi.object({
metadataUrl: optionalUrl.required(),
versionPrefix: Joi.string().optional(),
versionSuffix: Joi.string().optional(),
}).required()

const schema = Joi.object({
Expand Down Expand Up @@ -34,9 +37,12 @@ module.exports = class MavenMetadata extends BaseXmlService {
namedParams: {},
queryParams: {
metadataUrl:
'https://repo1.maven.org/maven2/com/google/code/gson/gson/maven-metadata.xml',
'https://repo1.maven.org/maven2/com/google/guava/guava/maven-metadata.xml',
versionPrefix: '29.',
versionSuffix: '-android',
},
staticPreview: renderVersionBadge({ version: '2.8.5' }),
staticPreview: renderVersionBadge({ version: '29.0-android' }),
documentation,
},
]

Expand All @@ -52,10 +58,24 @@ module.exports = class MavenMetadata extends BaseXmlService {
})
}

async handle(_namedParams, { metadataUrl }) {
async handle(_namedParams, { metadataUrl, versionPrefix, versionSuffix }) {
const data = await this.fetch({ metadataUrl })
return renderVersionBadge({
version: data.metadata.versioning.versions.version.slice(-1)[0],
})
let versions = data.metadata.versioning.versions.version.reverse()
if (versionPrefix !== undefined) {
versions = versions.filter(v => v.toString().startsWith(versionPrefix))
}
if (versionSuffix !== undefined) {
versions = versions.filter(v => v.toString().endsWith(versionSuffix))
}
const version = versions[0]
// if the filter returned no results, throw a NotFound
if (
(versionPrefix !== undefined || versionSuffix !== undefined) &&
version === undefined
)
throw new NotFound({
prettyMessage: 'version prefix or suffix not found',
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a test for this case?

})
return renderVersionBadge({ version })
}
}
46 changes: 46 additions & 0 deletions services/maven-metadata/maven-metadata.tester.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const Joi = require('joi')
const t = (module.exports = require('../tester').createServiceTester())
const { isVPlusDottedVersionAtLeastOne } = require('../test-validators')

Expand All @@ -12,6 +13,33 @@ t.create('valid maven-metadata.xml uri')
message: isVPlusDottedVersionAtLeastOne,
})

t.create('with version prefix')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/google/guava/guava/maven-metadata.xml&versionPrefix=27.'
)
.expectBadge({
label: 'maven',
message: 'v27.1-jre',
})

t.create('with version suffix')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/google/guava/guava/maven-metadata.xml&versionSuffix=-android'
)
.expectBadge({
label: 'maven',
message: Joi.string().regex(/-android$/),
})

t.create('with version prefix and suffix')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/google/guava/guava/maven-metadata.xml&versionPrefix=27.&versionSuffix=-android'
)
.expectBadge({
label: 'maven',
message: 'v27.1-android',
})

t.create('version ending with zero')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/mocked-group-id/mocked-artifact-id/maven-metadata.xml'
Expand Down Expand Up @@ -44,3 +72,21 @@ t.create('invalid maven-metadata.xml uri')
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/google/code/gson/gson/foobar.xml'
)
.expectBadge({ label: 'maven', message: 'not found' })

t.create('inexistent version prefix')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/maven-metadata.xml&versionPrefix=99'
)
.expectBadge({
label: 'maven',
message: 'version prefix or suffix not found',
})

t.create('inexistent version suffix')
.get(
'/v.json?metadataUrl=https://repo1.maven.org/maven2/com/github/fabriziocucci/yacl4j/maven-metadata.xml&versionSuffix=test'
)
.expectBadge({
label: 'maven',
message: 'version prefix or suffix not found',
})