[REUSE] Add service badges (#6330)

* feat/REUSE compliance

Co-authored-by: chris48s <chris48s@users.noreply.github.com>
This commit is contained in:
Tapan Chudasama
2021-04-01 23:57:54 +05:30
committed by GitHub
parent a662e36bf4
commit 1eba958f25
3 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
'use strict'
const Joi = require('joi')
const COLOR_MAP = {
checking: 'brightgreen',
compliant: 'green',
'non-compliant': 'red',
unregistered: 'red',
}
const isReuseCompliance = Joi.string()
.valid('compliant', 'non-compliant', 'checking', 'unregistered')
.required()
module.exports = {
isReuseCompliance,
COLOR_MAP,
}

View File

@@ -0,0 +1,55 @@
'use strict'
const Joi = require('joi')
const { BaseJsonService } = require('..')
const { isReuseCompliance, COLOR_MAP } = require('./reuse-compliance-helper')
const responseSchema = Joi.object({
status: isReuseCompliance,
}).required()
module.exports = class Reuse extends BaseJsonService {
static category = 'license'
static route = {
base: 'reuse/compliance',
pattern: ':remote+',
}
static examples = [
{
title: 'REUSE Compliance',
namedParams: {
remote: 'github.com/fsfe/reuse-tool',
},
staticPreview: this.render({ status: 'compliant' }),
keywords: ['license'],
},
]
static defaultBadgeData = {
label: 'reuse',
}
static render({ status }) {
return {
label: 'reuse',
message: status,
color: COLOR_MAP[status],
}
}
async fetch({ remote }) {
return await this._requestJson({
schema: responseSchema,
url: `https://api.reuse.software/status/${remote}`,
errorMessages: {
400: 'Not a Git repository',
},
})
}
async handle({ remote }) {
const { status } = await this.fetch({ remote })
return this.constructor.render({ status })
}
}

View File

@@ -0,0 +1,69 @@
'use strict'
const t = (module.exports = require('../tester').createServiceTester())
const { isReuseCompliance, COLOR_MAP } = require('./reuse-compliance-helper')
t.create('valid repo -- live')
.get('/github.com/fsfe/reuse-tool.json')
.expectBadge({
label: 'reuse',
message: isReuseCompliance,
color: COLOR_MAP[isReuseCompliance],
})
t.create('valid repo -- compliant')
.get('/github.com/username/repo.json')
.intercept(nock =>
nock('https://api.reuse.software/status')
.get('/github.com/username/repo')
.reply(200, { status: 'compliant' })
)
.expectBadge({
label: 'reuse',
message: 'compliant',
color: COLOR_MAP.compliant,
})
t.create('valid repo -- non-compliant')
.get('/github.com/username/repo.json')
.intercept(nock =>
nock('https://api.reuse.software/status')
.get('/github.com/username/repo')
.reply(200, { status: 'non-compliant' })
)
.expectBadge({
label: 'reuse',
message: 'non-compliant',
color: COLOR_MAP['non-compliant'],
})
t.create('valid repo -- checking')
.get('/github.com/username/repo.json')
.intercept(nock =>
nock('https://api.reuse.software/status')
.get('/github.com/username/repo')
.reply(200, { status: 'checking' })
)
.expectBadge({
label: 'reuse',
message: 'checking',
color: COLOR_MAP.checking,
})
t.create('valid repo -- unregistered')
.get('/github.com/username/repo.json')
.intercept(nock =>
nock('https://api.reuse.software/status')
.get('/github.com/username/repo')
.reply(200, { status: 'unregistered' })
)
.expectBadge({
label: 'reuse',
message: 'unregistered',
color: COLOR_MAP.unregistered,
})
t.create('invalid repo').get('/github.com/repo/invalid-repo.json').expectBadge({
label: 'reuse',
message: 'Not a Git repository',
})