use v5 API for [AUR] badges (#6836)
* refactor: use v5 API for AUR badges * Update services/aur/aur.service.js Co-authored-by: chris48s <chris48s@users.noreply.github.com> Co-authored-by: chris48s <chris48s@users.noreply.github.com>
This commit is contained in:
@@ -3,23 +3,25 @@ import {
|
||||
floorCount as floorCountColor,
|
||||
age as ageColor,
|
||||
} from '../color-formatters.js'
|
||||
import { renderLicenseBadge } from '../licenses.js'
|
||||
import { addv, metric, formatDate } from '../text-formatters.js'
|
||||
import { nonNegativeInteger } from '../validators.js'
|
||||
import { BaseJsonService, NotFound, InvalidResponse } from '../index.js'
|
||||
|
||||
const aurSchema = Joi.object({
|
||||
resultcount: nonNegativeInteger,
|
||||
results: Joi.alternatives(
|
||||
Joi.array().length(0).required(),
|
||||
Joi.object({
|
||||
License: Joi.string().required().allow(null),
|
||||
NumVotes: nonNegativeInteger,
|
||||
Version: Joi.string().required(),
|
||||
OutOfDate: nonNegativeInteger.allow(null),
|
||||
Maintainer: Joi.string().required().allow(null),
|
||||
LastModified: nonNegativeInteger,
|
||||
}).required()
|
||||
),
|
||||
results: Joi.array()
|
||||
.items(
|
||||
Joi.object({
|
||||
License: Joi.array().items(Joi.string().required()).allow(null),
|
||||
NumVotes: nonNegativeInteger,
|
||||
Version: Joi.string().required(),
|
||||
OutOfDate: nonNegativeInteger.allow(null),
|
||||
Maintainer: Joi.string().required().allow(null),
|
||||
LastModified: nonNegativeInteger,
|
||||
})
|
||||
)
|
||||
.required(),
|
||||
}).required()
|
||||
|
||||
class BaseAurService extends BaseJsonService {
|
||||
@@ -41,7 +43,7 @@ class BaseAurService extends BaseJsonService {
|
||||
return this._requestJson({
|
||||
schema: aurSchema,
|
||||
url: 'https://aur.archlinux.org/rpc.php',
|
||||
options: { qs: { type: 'info', arg: packageName } },
|
||||
options: { qs: { v: 5, type: 'info', arg: packageName } },
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -54,29 +56,25 @@ class AurLicense extends BaseAurService {
|
||||
{
|
||||
title: 'AUR license',
|
||||
namedParams: { packageName: 'android-studio' },
|
||||
staticPreview: this.render({ license: 'Apache' }),
|
||||
staticPreview: renderLicenseBadge({ license: 'Apache', color: 'blue' }),
|
||||
},
|
||||
]
|
||||
|
||||
static defaultBadgeData = { label: 'license' }
|
||||
|
||||
static render({ license }) {
|
||||
return { message: license, color: 'blue' }
|
||||
}
|
||||
|
||||
transform(json) {
|
||||
const license = json.results.License
|
||||
if (!license) {
|
||||
const licenses = json.results[0].License
|
||||
if (!licenses) {
|
||||
throw new NotFound({ prettyMessage: 'not specified' })
|
||||
}
|
||||
|
||||
return { license }
|
||||
return { licenses }
|
||||
}
|
||||
|
||||
async handle({ packageName }) {
|
||||
const json = await this.fetch({ packageName })
|
||||
const { license } = this.transform(json)
|
||||
return this.constructor.render({ license })
|
||||
const { licenses } = this.transform(json)
|
||||
return renderLicenseBadge({ licenses, color: 'blue' })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +102,7 @@ class AurVotes extends BaseAurService {
|
||||
|
||||
async handle({ packageName }) {
|
||||
const json = await this.fetch({ packageName })
|
||||
return this.constructor.render({ votes: json.results.NumVotes })
|
||||
return this.constructor.render({ votes: json.results[0].NumVotes })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,10 +125,12 @@ class AurVersion extends BaseAurService {
|
||||
}
|
||||
|
||||
async handle({ packageName }) {
|
||||
const json = await this.fetch({ packageName })
|
||||
const {
|
||||
results: [{ Version: version, OutOfDate: outOfDate }],
|
||||
} = await this.fetch({ packageName })
|
||||
return this.constructor.render({
|
||||
version: json.results.Version,
|
||||
outOfDate: json.results.OutOfDate,
|
||||
version,
|
||||
outOfDate,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -156,7 +156,7 @@ class AurMaintainer extends BaseAurService {
|
||||
|
||||
async handle({ packageName }) {
|
||||
const {
|
||||
results: { Maintainer: maintainer },
|
||||
results: [{ Maintainer: maintainer }],
|
||||
} = await this.fetch({ packageName })
|
||||
if (!maintainer) {
|
||||
throw new InvalidResponse({ prettyMessage: 'No maintainer' })
|
||||
@@ -191,7 +191,7 @@ class AurLastModified extends BaseAurService {
|
||||
|
||||
async handle({ packageName }) {
|
||||
const json = await this.fetch({ packageName })
|
||||
const date = 1000 * parseInt(json.results.LastModified)
|
||||
const date = 1000 * parseInt(json.results[0].LastModified)
|
||||
return this.constructor.render({ date })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,19 +47,22 @@ t.create('license (no license)')
|
||||
nock('https://aur.archlinux.org')
|
||||
.get('/rpc.php')
|
||||
.query({
|
||||
v: 5,
|
||||
type: 'info',
|
||||
arg: 'vscodium-bin',
|
||||
})
|
||||
.reply(200, {
|
||||
resultcount: 1,
|
||||
results: {
|
||||
License: null,
|
||||
NumVotes: 1,
|
||||
Version: '1',
|
||||
OutOfDate: null,
|
||||
Maintainer: null,
|
||||
LastModified: 1,
|
||||
},
|
||||
results: [
|
||||
{
|
||||
License: null,
|
||||
NumVotes: 1,
|
||||
Version: '1',
|
||||
OutOfDate: null,
|
||||
Maintainer: null,
|
||||
LastModified: 1,
|
||||
},
|
||||
],
|
||||
})
|
||||
)
|
||||
.expectBadge({ label: 'license', message: 'not specified' })
|
||||
|
||||
@@ -113,7 +113,7 @@ function licenseToColor(licenses) {
|
||||
return color
|
||||
}
|
||||
|
||||
function renderLicenseBadge({ license, licenses }) {
|
||||
function renderLicenseBadge({ license, licenses, color }) {
|
||||
if (licenses === undefined) {
|
||||
licenses = toArray(license)
|
||||
}
|
||||
@@ -124,7 +124,7 @@ function renderLicenseBadge({ license, licenses }) {
|
||||
|
||||
return {
|
||||
message: licenses.join(', '),
|
||||
color: licenseToColor(licenses),
|
||||
color: color || licenseToColor(licenses),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,5 +40,9 @@ describe('license helpers', function () {
|
||||
message: 'MPL-2.0, MIT',
|
||||
color: 'green',
|
||||
})
|
||||
given({ license: 'MIT', color: 'pink' }).expect({
|
||||
message: 'MIT',
|
||||
color: 'pink',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user