Skip to content

Commit

Permalink
Merge branch 'master' into refactor/convert-to-renderVersionBadge-3
Browse files Browse the repository at this point in the history
  • Loading branch information
jNullj authored Nov 4, 2024
2 parents 5ee0413 + 00d72da commit a206b7d
Show file tree
Hide file tree
Showing 11 changed files with 904 additions and 282 deletions.
126 changes: 0 additions & 126 deletions services/myget/myget.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import {
isMetric,
isVPlusDottedVersionNClausesWithOptionalSuffix,
} from '../test-validators.js'
import {
queryIndex,
nuGetV3VersionJsonWithDash,
nuGetV3VersionJsonFirstCharZero,
nuGetV3VersionJsonFirstCharNotZero,
} from '../nuget-fixtures.js'
import { invalidJSON } from '../response-fixtures.js'

export const t = new ServiceTester({
Expand Down Expand Up @@ -75,66 +69,6 @@ t.create('version (tenant)')
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('version (yellow badge)')
.get('/myget/mongodb/v/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonWithDash),
)
.expectBadge({
label: 'mongodb',
message: 'v1.2-beta',
color: 'yellow',
})

t.create('version (orange badge)')
.get('/myget/mongodb/v/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonFirstCharZero),
)
.expectBadge({
label: 'mongodb',
message: 'v0.35',
color: 'orange',
})

t.create('version (blue badge)')
.get('/myget/mongodb/v/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonFirstCharNotZero),
)
.expectBadge({
label: 'mongodb',
message: 'v1.2.7',
color: 'blue',
})

t.create('version (not found)')
.get('/myget/foo/v/not-a-real-package.json')
.expectBadge({ label: 'myget', message: 'package not found' })
Expand All @@ -148,66 +82,6 @@ t.create('version (pre) (valid)')
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('version (pre) (yellow badge)')
.get('/myget/mongodb/vpre/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonWithDash),
)
.expectBadge({
label: 'mongodb',
message: 'v1.2-beta',
color: 'yellow',
})

t.create('version (pre) (orange badge)')
.get('/myget/mongodb/vpre/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonFirstCharZero),
)
.expectBadge({
label: 'mongodb',
message: 'v0.35',
color: 'orange',
})

t.create('version (pre) (blue badge)')
.get('/myget/mongodb/vpre/MongoDB.Driver.Core.json')
.intercept(nock =>
nock('https://www.myget.org')
.get('/F/mongodb/api/v3/index.json')
.reply(200, queryIndex),
)
.intercept(nock =>
nock('https://api-v2v3search-0.nuget.org')
.get(
'/query?q=packageid%3Amongodb.driver.core&prerelease=true&semVerLevel=2',
)
.reply(200, nuGetV3VersionJsonFirstCharNotZero),
)
.expectBadge({
label: 'mongodb',
message: 'v1.2.7',
color: 'blue',
})

t.create('version (pre) (not found)')
.get('/myget/foo/vpre/not-a-real-package.json')
.expectBadge({ label: 'myget', message: 'package not found' })
21 changes: 21 additions & 0 deletions services/npm/npm-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,25 @@ export default class NpmBase extends BaseJsonService {

return this.constructor._validate(packageData, packageDataSchema)
}

async fetch({ registryUrl, scope, packageName, schema }) {
registryUrl = registryUrl || this.constructor.defaultRegistryUrl
let url

if (scope === undefined) {
url = `${registryUrl}/${packageName}`
} else {
const scoped = this.constructor.encodeScopedPackage({
scope,
packageName,
})
url = `${registryUrl}/${scoped}`
}

return this._requestJson({
url,
schema,
httpErrors: { 404: 'package not found' },
})
}
}
106 changes: 106 additions & 0 deletions services/npm/npm-last-update.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Joi from 'joi'
import dayjs from 'dayjs'
import { InvalidResponse, NotFound, pathParam, queryParam } from '../index.js'
import { formatDate } from '../text-formatters.js'
import { age as ageColor } from '../color-formatters.js'
import NpmBase, { packageNameDescription } from './npm-base.js'

const updateResponseSchema = Joi.object({
time: Joi.object({
created: Joi.string().required(),
modified: Joi.string().required(),
})
.pattern(Joi.string().required(), Joi.string().required())
.required(),
'dist-tags': Joi.object()
.pattern(Joi.string().required(), Joi.string().required())
.required(),
}).required()

export class NpmLastUpdate extends NpmBase {
static category = 'activity'

static route = this.buildRoute('npm/last-update', { withTag: true })

static openApi = {
'/npm/last-update/{packageName}': {
get: {
summary: 'NPM Last Update',
parameters: [
pathParam({
name: 'packageName',
example: 'verdaccio',
packageNameDescription,
}),
queryParam({
name: 'registry_uri',
example: 'https://registry.npmjs.com',
}),
],
},
},
'/npm/last-update/{packageName}/{tag}': {
get: {
summary: 'NPM Last Update (with dist tag)',
parameters: [
pathParam({
name: 'packageName',
example: 'verdaccio',
packageNameDescription,
}),
pathParam({
name: 'tag',
example: 'next-8',
}),
queryParam({
name: 'registry_uri',
example: 'https://registry.npmjs.com',
}),
],
},
},
}

static defaultBadgeData = { label: 'last updated' }

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

async handle(namedParams, queryParams) {
const { scope, packageName, tag, registryUrl } =
this.constructor.unpackParams(namedParams, queryParams)

const packageData = await this.fetch({
registryUrl,
scope,
packageName,
schema: updateResponseSchema,
})

let date

if (tag) {
const tagVersion = packageData['dist-tags'][tag]

if (!tagVersion) {
throw new NotFound({ prettyMessage: 'tag not found' })
}

date = dayjs(packageData.time[tagVersion])
} else {
const timeKey = packageData.time.modified ? 'modified' : 'created'

date = dayjs(packageData.time[timeKey])
}

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

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

export const t = await createServiceTester()

t.create('last updated date (valid package)')
.get('/verdaccio.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last updated date (invalid package)')
.get('/not-a-package.json')
.expectBadge({
label: 'last updated',
message: 'package not found',
})

t.create('last update from custom repository (valid scenario)')
.get('/verdaccio.json?registry_uri=https://registry.npmjs.com')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last update scoped package (valid scenario)')
.get('/@npm/types.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last update scoped package (invalid scenario)')
.get('/@not-a-scoped-package/not-a-valid-package.json')
.expectBadge({
label: 'last updated',
message: 'package not found',
})

t.create('last updated date with tag (valid scenario)')
.get('/verdaccio/latest.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last updated date (invalid tag)')
.get('/verdaccio/not-a-valid-tag.json')
.expectBadge({
label: 'last updated',
message: 'tag not found',
})
57 changes: 0 additions & 57 deletions services/nuget-fixtures.js

This file was deleted.

Loading

0 comments on commit a206b7d

Please sign in to comment.