Add [AUR] maintainer and last modified badges (#4952)

* Add [AUR] maintainer badge

* Add [AUR] last-modified badge

* Fix missing whitelist to Maintainer schema

* Apply suggestions from code review

Fail-safe maintainer and remove additional label color

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

* Add Arch wiki link. Replace category social with other.

Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
This commit is contained in:
Mohamed Feddad
2020-04-28 05:01:23 +03:00
committed by GitHub
parent 4fe80bb150
commit 98ebc3ad5e
2 changed files with 112 additions and 0 deletions

View File

@@ -5,6 +5,9 @@ const { floorCount: floorCountColor } = require('../color-formatters')
const { addv, metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { BaseJsonService, NotFound } = require('..')
const { formatDate } = require('../text-formatters')
const { age: ageColor } = require('../color-formatters')
const { InvalidResponse } = require('..')
const aurSchema = Joi.object({
resultcount: nonNegativeInteger,
@@ -19,6 +22,10 @@ const aurSchema = Joi.object({
NumVotes: nonNegativeInteger,
Version: Joi.string().required(),
OutOfDate: nonNegativeInteger.allow(null),
Maintainer: Joi.string()
.required()
.allow(null),
LastModified: nonNegativeInteger,
}).required()
),
}).required()
@@ -39,6 +46,8 @@ class BaseAurService extends BaseJsonService {
}
async fetch({ packageName }) {
// Please refer to the Arch wiki page for the full spec and documentation:
// https://wiki.archlinux.org/index.php/Aurweb_RPC_interface
return this._requestJson({
schema: aurSchema,
url: 'https://aur.archlinux.org/rpc.php',
@@ -168,8 +177,90 @@ class AurVersion extends BaseAurService {
}
}
class AurMaintainer extends BaseAurService {
static get category() {
return 'other'
}
static get route() {
return {
base: 'aur/maintainer',
pattern: ':packageName',
}
}
static get examples() {
return [
{
title: 'AUR maintainer',
namedParams: { packageName: 'google-chrome' },
staticPreview: this.render({ maintainer: 'luzifer' }),
},
]
}
static get defaultBadgeData() {
return { label: 'maintainer', color: 'blue' }
}
static render({ maintainer }) {
return { message: maintainer }
}
async handle({ packageName }) {
const {
results: { Maintainer: maintainer },
} = await this.fetch({ packageName })
if (!maintainer) {
throw new InvalidResponse({ prettyMessage: 'No maintainer' })
}
return this.constructor.render({ maintainer })
}
}
class AurLastModified extends BaseAurService {
static get category() {
return 'activity'
}
static get route() {
return {
base: 'aur/last-modified',
pattern: ':packageName',
}
}
static get examples() {
return [
{
title: 'AUR last modified',
namedParams: { packageName: 'google-chrome' },
staticPreview: this.render({ date: new Date().getTime() }),
},
]
}
static get defaultBadgeData() {
return { label: 'last modified' }
}
static render({ date }) {
const color = ageColor(date)
const message = formatDate(date)
return { color, message }
}
async handle({ packageName }) {
const json = await this.fetch({ packageName })
const date = 1000 * parseInt(json.results.LastModified)
return this.constructor.render({ date })
}
}
module.exports = {
AurLicense,
AurVersion,
AurVotes,
AurMaintainer,
AurLastModified,
}

View File

@@ -5,6 +5,7 @@ const {
isVPlusDottedVersionNClausesWithOptionalSuffix,
isMetric,
} = require('../test-validators')
const { isFormattedDate } = require('../test-validators')
const t = (module.exports = new ServiceTester({
id: 'aur',
@@ -59,3 +60,23 @@ t.create('license (no license)')
t.create('license (package not found)')
.get('/license/not-a-package.json')
.expectBadge({ label: 'license', message: 'package not found' })
// maintainer tests
t.create('maintainer (valid)')
.get('/maintainer/google-chrome.json')
.expectBadge({ label: 'maintainer', message: 'luzifer' })
t.create('maintainer (not found)')
.get('/maintainer/not-a-package.json')
.expectBadge({ label: 'maintainer', message: 'package not found' })
// last-modified tests
t.create('last-modified (valid)')
.get('/last-modified/google-chrome.json')
.expectBadge({ label: 'last modified', message: isFormattedDate })
t.create('last-modified (not found)')
.get('/last-modified/not-a-package.json')
.expectBadge({ label: 'last modified', message: 'package not found' })