Skip to content

[Maven] Support for maven central snapshots #10997

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions services/maven-central/maven-central-snapshots-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseXmlService } from '../index.js'

export default class MavenCentralSnapshotsBase extends BaseXmlService {
async fetch({ groupId, artifactId, schema }) {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
return this._requestXml({
schema,
url: `https://central.sonatype.com/repository/maven-snapshots/${group}/${artifact}/maven-metadata.xml`,
httpErrors: { 404: 'artifact not found' },
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Joi from 'joi'
import { pathParams } from '../index.js'
import { parseDate, renderDateBadge } from '../date.js'
import { nonNegativeInteger } from '../validators.js'
import MavenCentralSnapshotsBase from './maven-central-snapshots-base.js'

const updateResponseSchema = Joi.object({
metadata: Joi.object({
versioning: Joi.object({
lastUpdated: nonNegativeInteger,
}).required(),
}).required(),
}).required()

export default class MavenCentralSnapshotsLastUpdate extends MavenCentralSnapshotsBase {
static category = 'activity'

static route = {
base: 'maven-central-snapshots/last-update',
pattern: ':groupId/:artifactId',
}

static openApi = {
'/maven-central-snapshots/last-update/{groupId}/{artifactId}': {
get: {
summary: 'Maven Central Snapshots Last Update',
parameters: pathParams(
{ name: 'groupId', example: 'com.google.guava' },
{ name: 'artifactId', example: 'guava' },
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

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

Can we replace this with an example that works

),
},
},
}

static defaultBadgeData = { label: 'last updated' }

async handle({ groupId, artifactId }) {
const { metadata } = await this.fetch({
groupId,
artifactId,
schema: updateResponseSchema,
})

const date = parseDate(
String(metadata.versioning.lastUpdated),
'YYYYMMDDHHmmss',
)

return renderDateBadge(date)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isFormattedDate } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('last update date')
.get('/me.mrdoc.minecraft/dlibcustomextensions.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last update when artifact not found')
.get('/com.fail.test/this-does-not-exist.json')
.expectBadge({
label: 'last updated',
message: 'artifact not found',
})
46 changes: 46 additions & 0 deletions services/maven-central/maven-central-snapshots.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { redirector, pathParam, queryParam } from '../index.js'
import { description } from '../maven-metadata/maven-metadata.js'

export default redirector({
category: 'version',
isDeprecated: false,
route: {
base: 'maven-central-snapshots/v',
pattern: ':groupId/:artifactId/:versionPrefix?',
},
openApi: {
'/maven-central-snapshots/v/{groupId}/{artifactId}': {
get: {
summary: 'Maven Central Snapshots Version',
description,
parameters: [
pathParam({ name: 'groupId', example: 'com.google.guava' }),
pathParam({ name: 'artifactId', example: 'guava' }),
Comment on lines +17 to +18
Copy link
Member

Choose a reason for hiding this comment

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

Can we also replace this with an example that works

queryParam({
name: 'versionPrefix',
example: '29',
description: 'Filter only versions with this prefix.',
}),
queryParam({
name: 'versionSuffix',
example: '-android',
description: 'Filter only versions with this suffix.',
}),
Comment on lines +19 to +28
Copy link
Member

Choose a reason for hiding this comment

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

Secondly, lets look at the version badge.

This isn't really your fault, but the maven central version badge has a long-standing issue. #6188
I think it is likely we will remove the versionPrefix and versionSuffix params (see #6188 (comment) )
If we are going to add a specific route for this (although you can already make badges for this service anyway e.g: https://img.shields.io/maven-metadata/v.svg?label=maven-central-snapshots&metadataUrl=https%3A//central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml as the routes are just redirectors for the more general maven-metadata route), then I think we should not expose the versionPrefix and versionSuffix params on the documentation page. Lets not encourage users to start using a feature that is likely to disappear.

],
},
},
},
transformPath: () => '/maven-metadata/v',
transformQueryParams: ({ groupId, artifactId, versionPrefix }) => {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
const metadataUrl = `https://central.sonatype.com/repository/maven-snapshots/${group}/${artifact}/maven-metadata.xml`
return {
metadataUrl,
label: 'maven-central-snapshots',
versionPrefix,
}
},
overrideTransformedQueryParams: true,
dateAdded: new Date('2021-06-12'),
})
10 changes: 10 additions & 0 deletions services/maven-central/maven-central-snapshots.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('latest version redirection')
.get('/me.mrdoc.minecraft/dlibcustomextensions.json') // https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/
.expectRedirect(
`/maven-metadata/v.json?label=maven-central-snapshots&metadataUrl=${encodeURIComponent(
'https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml',
)}`,
)
Loading