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

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 732efea

@chris48s chris48s added the service-badge New or updated service badge label Apr 5, 2025
@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

Copy link
Contributor

@StefMa StefMa May 21, 2025

Choose a reason for hiding this comment

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

The "problem" with an example here is, that thery remove snapshots after a 90 days.
See https://central.sonatype.org/publish/publish-portal-snapshots/

Specifically, -SNAPSHOT releases are cleaned up after a period of time (currently 90 days). We believe that this should be sufficient, as projects under active development will be pushing new versions of the same -SNAPSHOT component with some frequency.

So even if we go with an SNAPSHOT release today, it might break in 90 days 🫠
So we might need an really active project to have some kind of a stable example...

Copy link
Member

Choose a reason for hiding this comment

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

OK, sure. But if you look at the example used in the service tests, it does actually exist
https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml
the versions might expire over time, but the repo is there
https://central.sonatype.com/repository/maven-snapshots/com/google/guava/guava/maven-metadata.xml
is never going to return anything.

@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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused with the URL you posted here.
Right know, version 1.0.0-SNAPSHOT is released.
See https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml

However, if I open the URL, it gives me 0.0.3-SNAPSHOT back.

Screenshot 2025-05-21 at 12 18 35 PM

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, see #6188 At the moment we're making an assumption about the ordering of versions which doesn't hold across every registry.

Tbh, I think we probably need to get #6188 resolved (see #11077 - I've not had a chance to follow up on that yet) and then circle back to this one having done that.

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

@StefMa
Copy link
Contributor

StefMa commented May 21, 2025

Hey guys,

what is the progress on that? 😇
@Doc94 still interested in fixing the stuff?
I would love to use shields for snapshot releases.

Doc94 added 2 commits May 22, 2025 08:53
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 3127d77 to 732efea Compare May 22, 2025 12:53
@Doc94
Copy link
Author

Doc94 commented May 22, 2025

Hey guys,

what is the progress on that? 😇 @Doc94 still interested in fixing the stuff? I would love to use shields for snapshot releases.

not sure if wait to the PR for the order of versions based in how this can change how need handle all here for refactor

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
4 participants