Files
shields/services/github/github-forks.service.js
Paul Melnikow 4597d77015 Refactor badge color functions (#2742)
- Replace the idea of color schemes with the idea of named colors (since none of our colorschemes have used `colorA`)
- Pass through the normalized color to `_shields_test` to harmonize with BaseService and simplify testing
    - Update service tests
- Move responsibility for color generation into the npm package
- Remove several color helper functions and their tests
- Update gh-badge public API to accept `color` and `labelColor`

This is a precursor to refactoring some of the logo code for #2473.
2019-01-15 16:43:33 -05:00

78 lines
2.2 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLogo: getLogo,
} = require('../../lib/badge-data')
const {
documentation,
checkErrorResponse: githubCheckErrorResponse,
} = 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 GithubForks extends LegacyService {
static get category() {
return 'social'
}
static get route() {
return {
base: 'github/forks',
}
}
static get examples() {
return [
{
title: 'GitHub forks',
previewUrl: 'badges/shields',
queryParams: { style: 'social', label: 'Fork' },
documentation,
},
]
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/forks\/([^/]+)\/([^/]+)\.(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('forks', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
badgeData.links = [
`https://github.com/${user}/${repo}/fork`,
`https://github.com/${user}/${repo}/network`,
]
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
const forks = data.forks_count
badgeData.text[1] = forks
badgeData.colorscheme = undefined
badgeData.colorB = '#4183C4'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}