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

[GradlePluginPortal] add gradle plugin portal #6449

Merged
merged 12 commits into from
Jun 7, 2021
81 changes: 81 additions & 0 deletions services/gradle-plugin-portal/gradle-plugin-portal.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict'

const Joi = require('joi')
const { renderVersionBadge } = require('../version')
const { BaseXmlService, NotFound } = require('..')

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 GradlePluginPortal extends BaseXmlService {
static category = 'version'

static route = {
base: 'gradle-plugin-portal/v',
pattern: ':pluginId/:versionPrefix?',
}

static examples = [
{
title: 'Gradle Plugin Portal',
pattern: ':pluginId',
namedParams: {
pluginId: 'com.gradle.plugin-publish',
},
staticPreview: {
label: 'plugin portal',
message: 'v0.14.0',
color: 'blue',
},
},
{
title: 'Gradle Plugin Portal with version prefix filter',
Copy link
Member

@PyvesB PyvesB Jun 5, 2021

Choose a reason for hiding this comment

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

Please remove this commented example for now. When and if we get round to adding a version prefix attribute to maven-metadata, it's easy enough to re-add at that point, without the risk of the commented code having gone stale in the meantime. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this comment.

pattern: ':pluginId/:versionPrefix',
namedParams: {
pluginId: 'com.gradle.plugin-publish',
versionPrefix: '0.10',
},
staticPreview: {
label: 'plugin portal',
message: 'v0.10.1',
color: 'blue',
},
},
]

static defaultBadgeData = {
label: 'plugin portal',
}

async fetch({ pluginId }) {
const plugin = encodeURIComponent(pluginId)
const group = plugin.replace(/\./g, '/')
const artifact = `${plugin}.gradle.plugin`
const url = `https://plugins.gradle.org/m2/${group}/${artifact}/maven-metadata.xml`
return this._requestXml({
schema,
url,
parserOptions: { parseNodeValue: false },
})
}

async handle({ pluginId, versionPrefix }) {
const data = await this.fetch({ pluginId })
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' })
}
return renderVersionBadge({ version })
}
}
55 changes: 55 additions & 0 deletions services/gradle-plugin-portal/gradle-plugin-portal.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

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

// https://plugins.gradle.org/m2/com/gradle/plugin-publish/com.gradle.plugin-publish.gradle.plugin/
t.create('latest version').get('/com.gradle.plugin-publish.json').expectBadge({
label: 'plugin portal',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

// https://plugins.gradle.org/m2/com/gradle/plugin-publish/com.gradle.plugin-publish.gradle.plugin/
t.create('latest 0.10 version')
.get('/com.gradle.plugin-publish/0.10.json')
.expectBadge({
label: 'plugin portal',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

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

t.create('inexistent version prefix')
.get('/com.gradle.plugin-publish/1000.json')
.expectBadge({ label: 'plugin portal', message: 'version prefix not found' })

t.create('version ending with zero')
.get('/mocked-plugin-id.json')
.intercept(nock =>
nock('https://plugins.gradle.org/m2')
.get(
'/mocked-plugin-id/mocked-plugin-id.gradle.plugin/maven-metadata.xml'
)
.reply(
200,
`
<metadata>
<groupId>mocked-plugin-id</groupId>
<artifactId>mocked-plugin-id.gradle.plugin</artifactId>
<versioning>
<latest>1.30</latest>
<release>1.30</release>
<versions>
<version>1.30</version>
</versions>
<lastUpdated>20190902002617</lastUpdated>
</versioning>
</metadata>
`
)
)
.expectBadge({ label: 'plugin portal', message: 'v1.30' })