Files
shields/services/github/github-commit-activity.service.js
Paul Melnikow 47e8cc3de3 Refactor route functions in BaseService (#2860)
The route helper functions are fairly well isolated from the rest of BaseService, with a few convenient entry points. They are easier to test in isolation.

The way the code was written before, `pathToRegexp` was invoked once for every request, which seems inefficient.

`route` was validated when it was used, though it seems more helpful to validate it up front.

This breaks out `_makeFullUrl`, `_regex`, `_regexFromPath` into new helper functions `makeFullUrl`, `assertValidRoute`, `prepareRoute`, and `namedParamsForMatch`.

It adds validation to route, and updates the services without patterns to include one, in order to pass the new validation rules.
2019-01-26 02:38:12 -05:00

103 lines
3.2 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { makeLogo: getLogo } = require('../../lib/logos')
const { metric } = require('../../lib/text-formatters')
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 GithubCommitActivity extends LegacyService {
static get category() {
return 'activity'
}
static get route() {
return {
base: 'github/commit-activity',
pattern: ':interval(y|4w|w)/:user/:repo',
}
}
static get examples() {
return [
{
title: 'GitHub commit activity the past week, 4 weeks, year',
pattern: 'y/:user/:repo',
namedParams: { user: 'eslint', repo: 'eslint' },
staticPreview: {
label: 'commit activity',
message: '457/year',
color: 'blue',
},
keywords: ['GitHub', 'commit', 'commits', 'activity'],
documentation,
},
]
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/commit-activity\/(y|4w|w)\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const interval = match[1]
const user = match[2]
const repo = match[3]
const format = match[4]
const apiUrl = `/repos/${user}/${repo}/stats/commit_activity`
const badgeData = getBadgeData('commit activity', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
badgeData.links = [`https://github.com/${user}/${repo}`]
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
const parsedData = JSON.parse(buffer)
let value
let intervalLabel
switch (interval) {
case 'y':
value = parsedData.reduce(
(sum, weekInfo) => sum + weekInfo.total,
0
)
intervalLabel = '/year'
break
case '4w':
value = parsedData
.slice(-4)
.reduce((sum, weekInfo) => sum + weekInfo.total, 0)
intervalLabel = '/4 weeks'
break
case 'w':
value = parsedData.slice(-2)[0].total
intervalLabel = '/week'
break
default:
throw Error('Unhandled case')
}
badgeData.text[1] = `${metric(value)}${intervalLabel}`
badgeData.colorscheme = 'blue'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}