Skip to content

Commit

Permalink
[Maven] Added badge for Maven-Cenral last-update (#10301) (#10585)
Browse files Browse the repository at this point in the history
* Added badge for Maven-Cenral last update.

* Update services/maven-central/maven-central-last-update.service.js

Co-authored-by: chris48s <chris48s@users.noreply.github.com>

* updated according to the review comments.

---------

Co-authored-by: chris48s <chris48s@users.noreply.github.com>
  • Loading branch information
MohanKumarAmbati and chris48s authored Oct 12, 2024
1 parent f3e0cc0 commit e3808c1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions services/maven-central/maven-central-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseXmlService } from '../index.js'

export default class MavenCentralBase extends BaseXmlService {
async fetch({ groupId, artifactId, schema }) {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
return this._requestXml({
schema,
url: `https://repo1.maven.org/maven2/${group}/${artifact}/maven-metadata.xml`,
httpErrors: { 404: 'artifact not found' },
})
}
}
66 changes: 66 additions & 0 deletions services/maven-central/maven-central-last-update.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Joi from 'joi'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import dayjs from 'dayjs'
import { InvalidResponse, pathParams } from '../index.js'
import { nonNegativeInteger } from '../validators.js'
import { formatDate } from '../text-formatters.js'
import { age as ageColor } from '../color-formatters.js'
import MavenCentralBase from './maven-central-base.js'
dayjs.extend(customParseFormat)

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

export default class MavenCentralLastUpdate extends MavenCentralBase {
static category = 'activity'

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

static defaultBadgeData = { label: 'last updated' }

static render({ date }) {
return {
message: formatDate(date),
color: ageColor(date),
}
}

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

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

if (!date.isValid) {
throw new InvalidResponse({ prettyMessage: 'invalid date' })
}

return this.constructor.render({ date })
}
}
15 changes: 15 additions & 0 deletions services/maven-central/maven-central-last-update.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isFormattedDate } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('last update date').get('/com.google.guava/guava.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',
})

0 comments on commit e3808c1

Please sign in to comment.