Added [OPM] version badge (#4615)

Co-Authored-By: Caleb Cartwright <calebcartwright@users.noreply.github.com>

Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
This commit is contained in:
Wangchong Zhou
2020-02-18 02:08:04 +08:00
committed by GitHub
parent ddfcd80ddb
commit a7b7cc00b5
2 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
'use strict'
const { renderVersionBadge } = require('../version')
const { BaseService, NotFound, InvalidResponse } = require('..')
module.exports = class OpmVersion extends BaseService {
static get category() {
return 'version'
}
static get route() {
return {
base: 'opm/v',
pattern: ':user/:moduleName',
}
}
static get examples() {
return [
{
title: 'OPM',
namedParams: {
user: 'openresty',
moduleName: 'lua-resty-lrucache',
},
staticPreview: renderVersionBadge({ version: 'v0.08' }),
},
]
}
static get defaultBadgeData() {
return {
label: 'opm',
}
}
async fetch({ user, moduleName }) {
const { res } = await this._request({
url: `https://opm.openresty.org/api/pkg/fetch`,
options: {
method: 'HEAD',
qs: {
account: user,
name: moduleName,
},
},
errorMessages: {
404: 'module not found',
},
})
// XXX: intercept 302 redirects and set followRedirect to false
const location = res.request.path
if (!location) {
throw new NotFound({ prettyMessage: 'module not found' })
}
const version = location.match(`${moduleName}-(.+).opm`)[1]
if (!version) {
throw new InvalidResponse({ prettyMessage: 'version invalid' })
}
return version
}
async handle({ user, moduleName }) {
const version = await this.fetch({ user, moduleName })
return renderVersionBadge({ version })
}
}

View File

@@ -0,0 +1,23 @@
'use strict'
const Joi = require('@hapi/joi')
const t = (module.exports = require('../tester').createServiceTester())
const isValidVersion = Joi.string()
.regex(/^v[\d+.]+$/)
.required()
t.create('version')
.get('/openresty/lua-resty-lrucache.json')
.expectBadge({
label: 'opm',
message: isValidVersion,
})
t.create('unknown module')
.get('/openresty/does-not-exist.json')
.expectBadge({ label: 'opm', message: 'module not found' })
t.create('unknown user')
.get('/nil/does-not-exist.json')
.expectBadge({ label: 'opm', message: 'module not found' })