refactor [githublastcommit] (#3199)

This commit is contained in:
chris48s
2019-03-13 04:17:39 +00:00
committed by Paul Melnikow
parent 9496b6d84c
commit 2b29c226e0

View File

@@ -1,27 +1,28 @@
'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 { formatDate } = require('../text-formatters')
const { age: ageColor } = require('../color-formatters')
const {
documentation,
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
const { GithubAuthService } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
const commonExampleAttrs = {
keywords: ['activity', 'latest'],
documentation,
}
// 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 GithubLastCommit extends LegacyService {
const schema = Joi.array()
.items(
Joi.object({
commit: Joi.object({
author: Joi.object({
date: Joi.string().required(),
}).required(),
}).required(),
}).required()
)
.required()
module.exports = class GithubLastCommit extends GithubAuthService {
static get category() {
return 'activity'
}
@@ -42,11 +43,7 @@ module.exports = class GithubLastCommit extends LegacyService {
user: 'google',
repo: 'skia',
},
staticPreview: {
label: 'last commit',
message: 'today',
color: 'brightgreen',
},
staticPreview: this.render({ commitDate: '2013-07-31T20:01:41Z' }),
...commonExampleAttrs,
},
{
@@ -57,50 +54,36 @@ module.exports = class GithubLastCommit extends LegacyService {
repo: 'skia',
branch: 'infra/config',
},
staticPreview: {
label: 'last commit',
message: 'april 2018',
color: 'yellow',
},
staticPreview: this.render({ commitDate: '2013-07-31T20:01:41Z' }),
...commonExampleAttrs,
},
]
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/last-commit\/([^/]+)\/([^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const user = match[1] // eg, mashape
const repo = match[2] // eg, apistatus
const branch = match[3]
const format = match[4]
let apiUrl = `/repos/${user}/${repo}/commits`
if (branch) {
apiUrl += `?sha=${branch}`
}
const badgeData = getBadgeData('last commit', 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)
const commitDate = parsedData[0].commit.author.date
badgeData.text[1] = formatDate(commitDate)
badgeData.colorscheme = ageColor(Date.parse(commitDate))
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
static get defaultBadgeData() {
return {
label: 'last commit',
}
}
static render({ commitDate }) {
return {
message: formatDate(commitDate),
color: ageColor(Date.parse(commitDate)),
}
}
async fetch({ user, repo, branch }) {
return this._requestJson({
url: `/repos/${user}/${repo}/commits`,
options: { qs: { sha: branch } },
schema,
errorMessages: errorMessagesFor(),
})
}
async handle({ user, repo, branch }) {
const body = await this.fetch({ user, repo, branch })
return this.constructor.render({ commitDate: body[0].commit.author.date })
}
}