Files
shields/services/github/github-contributors.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

69 lines
1.8 KiB
JavaScript

'use strict'
const Joi = require('joi')
const parseLinkHeader = require('parse-link-header')
const { renderContributorBadge } = require('../contributor-count')
const { GithubAuthService } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
// All we do is check its length.
const schema = Joi.array().items(Joi.object())
module.exports = class GithubContributors extends GithubAuthService {
static get category() {
return 'activity'
}
static get route() {
return {
base: 'github',
pattern: ':which(contributors|contributors-anon)/:user/:repo',
}
}
static get examples() {
return [
{
title: 'GitHub contributors',
namedParams: {
which: 'contributors',
user: 'cdnjs',
repo: 'cdnjs',
},
staticPreview: this.render({ contributorCount: 397 }),
documentation,
},
]
}
static get defaultBadgeData() {
return { label: 'contributors' }
}
static render({ contributorCount }) {
return renderContributorBadge({ contributorCount })
}
async handle({ which, user, repo }) {
const isAnon = which === 'contributors-anon'
const { res, buffer } = await this._request({
url: `/repos/${user}/${repo}/contributors`,
options: { qs: { page: '1', per_page: '1', anon: isAnon } },
errorMessages: errorMessagesFor('repo not found'),
})
const parsed = parseLinkHeader(res.headers['link'])
let contributorCount
if (parsed === null) {
const json = this._parseJson(buffer)
const contributorInfo = this.constructor._validate(json, schema)
contributorCount = contributorInfo.length
} else {
contributorCount = +parsed.last.page
}
return this.constructor.render({ contributorCount })
}
}