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 2 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' },
),
},
},
}

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' }),
queryParam({
name: 'versionPrefix',
example: '29',
description: 'Filter only versions with this prefix.',
}),
queryParam({
name: 'versionSuffix',
example: '-android',
description: 'Filter only versions with this suffix.',
}),
],
},
},
},
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