Skip to content
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

Add optional query parameter (include_prereleases) to [GemVersion] #6451

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions services/gem/gem-version.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Joi = require('joi')
const { renderVersionBadge } = require('../version')
const { renderVersionBadge, latest } = require('../version')
const { BaseJsonService } = require('..')

const schema = Joi.object({
Expand All @@ -10,16 +10,38 @@ const schema = Joi.object({
version: Joi.string().required(),
}).required()

const versionSchema = Joi.array()
.items(
Joi.object({
number: Joi.string().required(),
})
)
.min(1)
.required()

const queryParamSchema = Joi.object({
include_prereleases: Joi.equal(''),
}).required()

module.exports = class GemVersion extends BaseJsonService {
static category = 'version'
static route = { base: 'gem/v', pattern: ':gem' }
static route = { base: 'gem/v', pattern: ':gem', queryParamSchema }
static examples = [
{
title: 'Gem',
namedParams: { gem: 'formatador' },
staticPreview: this.render({ version: '2.1.0' }),
keywords: ['ruby'],
},
{
title: 'Gem (including prereleases)',
namedParams: { gem: 'flame' },
queryParams: {
include_prereleases: null,
},
staticPreview: this.render({ version: '5.0.0.rc6' }),
keywords: ['ruby'],
},
]

static defaultBadgeData = { label: 'gem' }
Expand All @@ -35,8 +57,21 @@ module.exports = class GemVersion extends BaseJsonService {
})
}

async handle({ gem }) {
const { version } = await this.fetch({ gem })
return this.constructor.render({ version })
async fetchLatest({ gem }) {
return this._requestJson({
schema: versionSchema,
url: `https://rubygems.org/api/v1/versions/${gem}.json`,
})
}

async handle({ gem }, queryParams) {
if (queryParams && typeof queryParams.include_prereleases !== 'undefined') {
const data = await this.fetchLatest({ gem })
const versions = data.map(version => version.number)
return this.constructor.render({ version: latest(versions) })
} else {
const { version } = await this.fetch({ gem })
return this.constructor.render({ version })
}
}
}
21 changes: 20 additions & 1 deletion services/gem/gem-version.tester.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

const { isVPlusDottedVersionAtLeastOne } = require('../test-validators')
const {
isVPlusDottedVersionAtLeastOne,
withRegex,
} = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('version (valid)').get('/formatador.json').expectBadge({
Expand All @@ -11,3 +14,19 @@ t.create('version (valid)').get('/formatador.json').expectBadge({
t.create('version (not found)')
.get('/not-a-package.json')
.expectBadge({ label: 'gem', message: 'not found' })

// this is the same as isVPlusDottedVersionNClausesWithOptionalSuffix from test-validators.js
// except that it also accepts regexes like 5.0.0.rc5 - the . before the rc5 is not accepted in the original
const isVPlusDottedVersionNClausesWithOptionalSuffix = withRegex(
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering whether it would have been better to modify the original regex directly. Admittedly I've not seen many projects use a dot as the separator for the pre-release tag outside the Ruby ecosystem, so what you've done may make more sense. Let's leave it as is for now, but another maintainer will possibly have a stronger view on this one. :)

Copy link
Member

Choose a reason for hiding this comment

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

generally agreed but also content with leaving this here for now. we probably do have some minor proliferation of duplicative regexes but I don't have any concerns with having this one here

/^v\d+(\.\d+)*([-+~.].*)?$/
)
t.create('version including prereleases (valid)')
.get('/flame.json?include_prereleases')
.expectBadge({
label: 'gem',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('version including prereleases (not found)')
.get('/not-a-package.json?include_prereleases')
.expectBadge({ label: 'gem', message: 'not found' })