Files
shields/services/github/github-contributors.service.js
chris48s c73072deed Remove requestOptions2GotOptions compatibility layer (#7270)
* gzip --> decompress

* strictSSL --> https.rejectUnauthorized

* auth --> username/password

* qs --> searchParams

* fix base service auth docs

* completely remove requestOptions2GotOptions layer

* update the docs

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-11-15 19:56:08 +00:00

58 lines
1.7 KiB
JavaScript

import Joi from 'joi'
import parseLinkHeader from 'parse-link-header'
import { renderContributorBadge } from '../contributor-count.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, errorMessagesFor } from './github-helpers.js'
// All we do is check its length.
const schema = Joi.array().items(Joi.object())
export default class GithubContributors extends GithubAuthV3Service {
static category = 'activity'
static route = {
base: 'github',
pattern: ':variant(contributors|contributors-anon)/:user/:repo',
}
static examples = [
{
title: 'GitHub contributors',
namedParams: {
variant: 'contributors',
user: 'cdnjs',
repo: 'cdnjs',
},
staticPreview: this.render({ contributorCount: 397 }),
documentation,
},
]
static defaultBadgeData = { label: 'contributors' }
static render({ contributorCount }) {
return renderContributorBadge({ contributorCount })
}
async handle({ variant, user, repo }) {
const isAnon = variant === 'contributors-anon'
const { res, buffer } = await this._request({
url: `/repos/${user}/${repo}/contributors`,
options: { searchParams: { 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 })
}
}