Rewrite [GitHubStars] (#3119)

* Refactor [GithubStars]

* Add test of link array
This commit is contained in:
Paul Melnikow
2019-02-28 19:18:32 -05:00
committed by Caleb Cartwright
parent 513b69272e
commit 97358c1399
4 changed files with 80 additions and 88 deletions

View File

@@ -1,21 +1,16 @@
'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { makeLogo: getLogo } = require('../../lib/logos')
const Joi = require('joi')
const { metric } = require('../text-formatters')
const {
documentation,
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthService } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
// This legacy service should be rewritten to use e.g. BaseJsonService.
//
// Tips for rewriting:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not base new services on this code.
module.exports = class GithubStars extends LegacyService {
const schema = Joi.object({
stargazers_count: nonNegativeInteger,
}).required()
module.exports = class GithubStars extends GithubAuthService {
static get category() {
return 'social'
}
@@ -36,6 +31,8 @@ module.exports = class GithubStars extends LegacyService {
repo: 'shields',
},
queryParams: { style: 'social' },
// TODO: This is currently a literal, as `staticPreview` doesn't
// support `link`.
staticPreview: {
label: 'Stars',
message: '7k',
@@ -48,41 +45,28 @@ module.exports = class GithubStars extends LegacyService {
static get defaultBadgeData() {
return {
label: 'stars',
namedLogo: 'github',
}
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/stars\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const user = match[1] // eg, qubyte/rubidium
const repo = match[2]
const format = match[3]
const apiUrl = `/repos/${user}/${repo}`
const badgeData = getBadgeData('stars', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
badgeData.links = [
`https://github.com/${user}/${repo}`,
`https://github.com/${user}/${repo}/stargazers`,
]
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
badgeData.text[1] = metric(JSON.parse(buffer).stargazers_count)
badgeData.colorscheme = 'blue'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
static render({ stars, user, repo }) {
return {
message: metric(stars),
color: 'blue',
link: [
`https://github.com/${user}/${repo}`,
`https://github.com/${user}/${repo}/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 })
}
}