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

Conversation

Doc94
Copy link

@Doc94 Doc94 commented Apr 5, 2025

This PR close #10894 making a replicate of the current services for maven-central but for the snapshots feature added recently.

Copy link
Contributor

github-actions bot commented Apr 5, 2025

Messages
📖 ✨ Thanks for your contribution to Shields, @Doc94!

Generated by 🚫 dangerJS against 3127d77

@chris48s chris48s added the service-badge New or updated service badge label Apr 5, 2025
Doc94 added 2 commits April 13, 2025 09:44
maven central remove old snapshots then this test can cause issues if try to check a removed version
@Doc94 Doc94 force-pushed the feature/10894-maven-central-snapshots branch from 9f708f1 to 4b400ac Compare April 13, 2025 13:44
@portlek
Copy link

portlek commented Apr 24, 2025

looking forward to this!

Comment on lines +28 to +29
{ name: 'groupId', example: 'com.google.guava' },
{ name: 'artifactId', example: 'guava' },
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

@chris48s
Copy link
Member

OK, so we've got 2 things going on here.

First lets take the last updated badge. This one should be fairly simple to land.
Fundamanetally, MavenCentralLastUpdate and MavenCentralSnapshotsLastUpdate are quite similar and there's a lot of copy/paste going on here. I'd like us to restructure both services and DRY this up so the common parts are not duplicated. Here's a suggested implementation:

import Joi from 'joi'
import { pathParams, BaseXmlService } from '../index.js'
import { parseDate, renderDateBadge } from '../date.js'
import { nonNegativeInteger } from '../validators.js'

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

class MavenCentralLastUpdateBase extends BaseXmlService {
  static category = 'activity'

  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)
  }

  static encodeParams({ groupId, artifactId }) {
    return {
      group: encodeURIComponent(groupId).replace(/\./g, '/'),
      artifact: encodeURIComponent(artifactId),
    }
  }
}

class MavenCentralLastUpdate extends MavenCentralLastUpdateBase {
  static route = {
    base: 'maven-central/last-update',
    pattern: ':groupId/:artifactId',
  }

  static openApi = {
    '/maven-central/last-update/{groupId}/{artifactId}': {
      get: {
        summary: 'Maven Central Last Update',
        parameters: pathParams(
          { name: 'groupId', example: 'com.google.guava' },
          { name: 'artifactId', example: 'guava' },
        ),
      },
    },
  }

  async fetch({ groupId, artifactId, schema }) {
    const { group, artifact } = this.constructor.encodeParams({
      groupId,
      artifactId,
    })
    return this._requestXml({
      schema,
      url: `https://repo1.maven.org/maven2/${group}/${artifact}/maven-metadata.xml`,
      httpErrors: { 404: 'artifact not found' },
    })
  }
}

class MavenCentralSnapshotsLastUpdate extends MavenCentralLastUpdateBase {
  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: 'me.mrdoc.minecraft' },
          { name: 'artifactId', example: 'dlibcustomextensions' },
        ),
      },
    },
  }

  async fetch({ groupId, artifactId, schema }) {
    const { group, artifact } = this.constructor.encodeParams({
      groupId,
      artifactId,
    })
    return this._requestXml({
      schema,
      url: `https://central.sonatype.com/repository/maven-snapshots/${group}/${artifact}/maven-metadata.xml`,
      httpErrors: { 404: 'artifact not found' },
    })
  }
}

export default [MavenCentralLastUpdate, MavenCentralSnapshotsLastUpdate]

(that can all go in one file)

Comment on lines +19 to +28
queryParam({
name: 'versionPrefix',
example: '29',
description: 'Filter only versions with this prefix.',
}),
queryParam({
name: 'versionSuffix',
example: '-android',
description: 'Filter only versions with this suffix.',
}),
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.

Comment on lines +17 to +18
pathParam({ name: 'groupId', example: 'com.google.guava' }),
pathParam({ name: 'artifactId', example: 'guava' }),
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maven central snapshot repository
3 participants