-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
f3e0cc0
commit e3808c1
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
services/maven-central/maven-central-last-update.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
services/maven-central/maven-central-last-update.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |