Files
shields/services/github/github-release.service.js
Paul Melnikow 83ac6ff1b3 Enforce use of template literals (#2242)
This is consistent with what we're pretty much already doing, and saves us from making the request during code review.

These were all autofixed and most of them seem easier to read. Some in the legacy services should be rewritten in more legible forms during refactor (ie using intermediate variables, or using request’s qs option). There are some in helper functions and elsewhere that should get rewritten separately. I don't want to change them in this PR because the changes will get lost in this diff, though we could identify them here and fix them before or just after.
2018-11-02 17:11:44 -04:00

53 lines
1.8 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLogo: getLogo,
} = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const {
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
module.exports = class GithubRelease extends LegacyService {
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/release(-pre)?\/([^/]+\/[^/]+)(?:\/(all))?\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const includePre = Boolean(match[1]) || match[3] === 'all'
const userRepo = match[2] // eg, qubyte/rubidium
const format = match[4]
let apiUrl = `/repos/${userRepo}/releases`
if (!includePre) {
apiUrl = `${apiUrl}/latest`
}
const badgeData = getBadgeData('release', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
let data = JSON.parse(buffer)
if (includePre) {
data = data[0]
}
const version = data.tag_name
const prerelease = data.prerelease
badgeData.text[1] = versionText(version)
badgeData.colorscheme = prerelease ? 'orange' : 'blue'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'none'
sendBadge(format, badgeData)
}
})
})
)
}
}