forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-contributors.service.js
More file actions
59 lines (49 loc) · 1.7 KB
/
Copy pathgithub-contributors.service.js
File metadata and controls
59 lines (49 loc) · 1.7 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
52
53
54
55
56
57
58
59
'use strict'
const Joi = require('joi')
const parseLinkHeader = require('parse-link-header')
const { renderContributorBadge } = require('../contributor-count')
const { GithubAuthV3Service } = 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 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: { 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 })
}
}