Files
shields/services/github/github-stars.service.js
Pratapi Hemant 108a819c3a [GITHUB] Badge for total stars of an user / org (#5507)
* added badge for total stars of an user / org
* Added a transformJson method in graphql-base to handle partial data


Co-authored-by: Pratapi Hemant Patel <pratpatel@expedia.com>
Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
2020-09-19 12:07:23 +01:00

74 lines
1.7 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthV3Service } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
const schema = Joi.object({
stargazers_count: nonNegativeInteger,
}).required()
module.exports = class GithubStars extends GithubAuthV3Service {
static get category() {
return 'social'
}
static get route() {
return {
base: 'github/stars',
pattern: ':user/:repo',
}
}
static get examples() {
return [
{
title: 'GitHub Repo stars',
namedParams: {
user: 'badges',
repo: 'shields',
},
queryParams: { style: 'social' },
// TODO: This is currently a literal, as `staticPreview` doesn't
// support `link`.
staticPreview: {
label: 'Stars',
message: '7k',
style: 'social',
},
documentation,
},
]
}
static get defaultBadgeData() {
return {
label: 'stars',
namedLogo: 'github',
}
}
static render({ stars, user, repo }) {
const slug = `${encodeURIComponent(user)}/${encodeURIComponent(repo)}`
return {
message: metric(stars),
color: 'blue',
link: [
`https://github.com/${slug}`,
`https://github.com/${slug}/stargazers`,
],
}
}
async handle({ user, repo }) {
const { stargazers_count: stars } = await this._requestJson({
url: `/repos/${user}/${repo}`,
schema,
errorMessages: errorMessagesFor(),
})
return this.constructor.render({ user, repo, stars })
}
}