Skip to content

Commit

Permalink
[REUSE] Add service badges (#6330)
Browse files Browse the repository at this point in the history
* feat/REUSE compliance

Co-authored-by: chris48s <chris48s@users.noreply.github.com>
  • Loading branch information
tapanchudasama and chris48s authored Apr 1, 2021
1 parent a662e36 commit 1eba958
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
19 changes: 19 additions & 0 deletions services/reuse/reuse-compliance-helper.js
Original file line number Diff line number Diff line change
@@ -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,
}
55 changes: 55 additions & 0 deletions services/reuse/reuse-compliance.service.js
Original file line number Diff line number Diff line change
@@ -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 })
}
}
69 changes: 69 additions & 0 deletions services/reuse/reuse-compliance.tester.js
Original file line number Diff line number Diff line change
@@ -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',
})

0 comments on commit 1eba958

Please sign in to comment.