Files
shields/services/github/github-manifest.service.js
Paul Melnikow fafb22efee Move "good" badge helpers from lib/ to services/ (#3101)
This moves a few helpers from `lib/` to `services/`:

build-status.js
build-status.spec.js
color-formatters.js
color-formatters.spec.js
contributor-count.js
licenses.js
licenses.spec.js
php-version.js
php-version.spec.js
text-formatters.js
text-formatters.spec.js
version.js
version.spec.js

And one from `lib/` to `core/`:

unhandled-rejection.spec.js

The diff is long, but the changes are straightforward.

Ref #2832
2019-02-27 20:47:46 -05:00

161 lines
3.6 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { renderVersionBadge } = require('../version')
const {
individualValueSchema,
transformAndValidate,
renderDynamicBadge,
} = require('../dynamic-common')
const { ConditionalGithubAuthService } = require('./github-auth-service')
const { fetchJsonFromRepo } = require('./github-common-fetch')
const { documentation } = require('./github-helpers')
const schema = Joi.object({
version: individualValueSchema,
}).required()
const flexibleSchema = Joi.object().required()
class GithubManifestVersion extends ConditionalGithubAuthService {
static get category() {
return 'version'
}
static get route() {
return {
base: 'github/manifest-json/v',
pattern: ':user/:repo/:branch*',
}
}
static get examples() {
return [
{
title: 'GitHub manifest version',
pattern: ':user/:repo',
namedParams: {
user: 'RedSparr0w',
repo: 'IndieGala-Helper',
},
staticPreview: this.render({ version: 2 }),
documentation,
},
{
title: 'GitHub manifest version',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'RedSparr0w',
repo: 'IndieGala-Helper',
branch: 'master',
},
staticPreview: this.render({ version: 2, branch: 'master' }),
documentation,
},
]
}
static render({ version, branch }) {
return renderVersionBadge({
version,
tag: branch,
defaultLabel: 'manifest',
})
}
async handle({ user, repo, branch }) {
const { version } = await fetchJsonFromRepo(this, {
schema,
user,
repo,
branch,
filename: 'manifest.json',
})
return this.constructor.render({ version, branch })
}
}
class DynamicGithubManifest extends ConditionalGithubAuthService {
static get category() {
return 'other'
}
static get route() {
return {
base: 'github/manifest-json',
format: '(?!v)([^/]+)/([^/]+)/([^/]+)/?([^/]+)?',
capture: ['key', 'user', 'repo', 'branch'],
}
}
static get examples() {
return [
{
title: 'GitHub manifest.json dynamic',
pattern: ':key/:user/:repo',
namedParams: {
key: 'permissions',
user: 'developit',
repo: 'microbundle',
},
staticPreview: this.render({
key: 'permissions',
value: ['bundle', 'rollup', 'micro library'],
}),
documentation,
},
{
title: 'GitHub manifest.json dynamic',
pattern: ':key/:user/:repo/:branch',
namedParams: {
key: 'permissions',
user: 'developit',
repo: 'microbundle',
branch: 'master',
},
staticPreview: this.render({
key: 'permissions',
value: ['bundle', 'rollup', 'micro library'],
branch: 'master',
}),
documentation,
},
]
}
static get defaultBadgeData() {
return {
label: 'manifest',
}
}
static render({ key, value, branch }) {
return renderDynamicBadge({
defaultLabel: key,
tag: branch,
value,
})
}
async handle({ key, user, repo, branch }) {
// Not sure `manifest-json/n` was ever advertised, but it was supported.
if (key === 'n') {
key = 'name'
}
const data = await fetchJsonFromRepo(this, {
schema: flexibleSchema,
user,
repo,
branch,
filename: 'manifest.json',
})
const value = transformAndValidate({ data, key })
return this.constructor.render({ key, value, branch })
}
}
module.exports = {
GithubManifestVersion,
DynamicGithubManifest,
}