forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-license.service.js
More file actions
51 lines (44 loc) · 1.46 KB
/
Copy pathgithub-license.service.js
File metadata and controls
51 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
const Joi = require('joi')
const { renderLicenseBadge } = require('../licenses')
const { GithubAuthV3Service } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
const schema = Joi.object({
// Some repos do not have a license, in which case GitHub returns `{ license: null }`.
license: Joi.object({ spdx_id: Joi.string().required() }).allow(null),
}).required()
module.exports = class GithubLicense extends GithubAuthV3Service {
static category = 'license'
static route = { base: 'github/license', pattern: ':user/:repo' }
static examples = [
{
title: 'GitHub',
namedParams: { user: 'mashape', repo: 'apistatus' },
staticPreview: {
label: 'license',
message: 'MIT',
color: 'green',
},
documentation,
},
]
static defaultBadgeData = { label: 'license' }
static render({ license }) {
if (license === 'NOASSERTION') {
return { message: 'not identifiable by github' }
} else if (license) {
return renderLicenseBadge({ license })
} else {
return { message: 'not specified' }
}
}
async handle({ user, repo }) {
const { license: licenseObject } = await this._requestJson({
schema,
url: `/repos/${user}/${repo}`,
errorMessages: errorMessagesFor('repo not found'),
})
const license = licenseObject ? licenseObject.spdx_id : undefined
return this.constructor.render({ license })
}
}