Implement All Contributors Badge, run [GitHubAllContributors] (#5163)
* feat: added all-contributors badge and test * Update services/github/github-all-contributors.service.js Updated to schema to ensure the array always exists Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com> * fix: added branch to parameters Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
This commit is contained in:
60
services/github/github-all-contributors.service.js
Normal file
60
services/github/github-all-contributors.service.js
Normal file
@@ -0,0 +1,60 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('@hapi/joi')
|
||||
const { renderContributorBadge } = require('../contributor-count')
|
||||
const { ConditionalGithubAuthV3Service } = require('./github-auth-service')
|
||||
const { fetchJsonFromRepo } = require('./github-common-fetch')
|
||||
const { documentation } = require('./github-helpers')
|
||||
|
||||
const schema = Joi.object({
|
||||
contributors: Joi.array().required(),
|
||||
}).required()
|
||||
|
||||
module.exports = class GithubAllContributorsService extends ConditionalGithubAuthV3Service {
|
||||
static get category() {
|
||||
return 'activity'
|
||||
}
|
||||
|
||||
static get route() {
|
||||
return {
|
||||
base: 'github/all-contributors',
|
||||
pattern: ':user/:repo/:branch*',
|
||||
}
|
||||
}
|
||||
|
||||
static get examples() {
|
||||
return [
|
||||
{
|
||||
title: 'Github All Contributors',
|
||||
namedParams: {
|
||||
repo: 'all-contributors',
|
||||
user: 'all-contributors',
|
||||
branch: 'master',
|
||||
},
|
||||
staticPreview: this.render({ contributorCount: 66 }),
|
||||
documentation,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'all contributors' }
|
||||
}
|
||||
|
||||
static render({ contributorCount }) {
|
||||
return renderContributorBadge({ contributorCount })
|
||||
}
|
||||
|
||||
async handle({ user, repo, branch }) {
|
||||
const { contributors } = await fetchJsonFromRepo(this, {
|
||||
schema,
|
||||
user,
|
||||
repo,
|
||||
branch,
|
||||
filename: '.all-contributorsrc',
|
||||
})
|
||||
|
||||
const contributorCount = contributors.length
|
||||
return this.constructor.render({ contributorCount })
|
||||
}
|
||||
}
|
||||
16
services/github/github-all-contributors.tester.js
Normal file
16
services/github/github-all-contributors.tester.js
Normal file
@@ -0,0 +1,16 @@
|
||||
'use strict'
|
||||
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
const { isMetric } = require('../test-validators')
|
||||
|
||||
t.create('all-contributors repo')
|
||||
.get('/all-contributors/all-contributors.json')
|
||||
.expectBadge({
|
||||
label: 'all contributors',
|
||||
message: isMetric,
|
||||
})
|
||||
|
||||
t.create('shields repo (not found)').get('/badges/shields.json').expectBadge({
|
||||
label: 'all contributors',
|
||||
message: 'repo not found, branch not found, or .all-contributorsrc missing',
|
||||
})
|
||||
Reference in New Issue
Block a user