Skip to content
Snippets Groups Projects
Unverified Commit 9496b6d8 authored by chris48s's avatar chris48s Committed by GitHub
Browse files

refactor [githubsize] (#3198)

parent b0ff851e
Branches
Tags 18.2.5
No related merge requests found
'use strict' 'use strict'
const Joi = require('joi')
const prettyBytes = require('pretty-bytes') const prettyBytes = require('pretty-bytes')
const LegacyService = require('../legacy-service') const { nonNegativeInteger } = require('../validators')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data') const { GithubAuthService } = require('./github-auth-service')
const { makeLogo: getLogo } = require('../../lib/logos') const { documentation, errorMessagesFor } = require('./github-helpers')
const { const { NotFound } = require('..')
documentation,
checkErrorResponse: githubCheckErrorResponse, const schema = Joi.alternatives(
} = require('./github-helpers') Joi.object({
size: nonNegativeInteger,
}).required(),
Joi.array().required()
)
// This legacy service should be rewritten to use e.g. BaseJsonService. module.exports = class GithubSize extends GithubAuthService {
//
// 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 GithubSize extends LegacyService {
static get category() { static get category() {
return 'size' return 'size'
} }
...@@ -31,66 +30,38 @@ module.exports = class GithubSize extends LegacyService { ...@@ -31,66 +30,38 @@ module.exports = class GithubSize extends LegacyService {
return [ return [
{ {
title: 'GitHub file size in bytes', title: 'GitHub file size in bytes',
pattern: ':user/:repo/:path',
namedParams: { namedParams: {
user: 'webcaetano', user: 'webcaetano',
repo: 'craft', repo: 'craft',
path: 'build/phaser-craft.min.js', path: 'build/phaser-craft.min.js',
}, },
staticPreview: { staticPreview: this.render({ size: 9170 }),
label: 'size',
message: '9.17 kB',
color: 'green',
},
keywords: ['repo'], keywords: ['repo'],
documentation, documentation,
}, },
] ]
} }
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) { static render({ size }) {
camp.route( return {
/^\/github\/size\/([^/]+)\/([^/]+)\/(.*)\.(svg|png|gif|jpg|json)$/, message: prettyBytes(size),
cache((data, match, sendBadge, request) => { color: 'blue',
const user = match[1] // eg, mashape
const repo = match[2] // eg, apistatus
const path = match[3]
const format = match[4]
const apiUrl = `/repos/${user}/${repo}/contents/${path}`
const badgeData = getBadgeData('size', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
} }
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (
githubCheckErrorResponse(
badgeData,
err,
res,
'repo or file not found'
)
) {
sendBadge(format, badgeData)
return
} }
try {
const body = JSON.parse(buffer) async fetch({ user, repo, path }) {
if (body && Number.isInteger(body.size)) { return this._requestJson({
badgeData.text[1] = prettyBytes(body.size) url: `/repos/${user}/${repo}/contents/${path}`,
badgeData.colorscheme = 'green' schema,
sendBadge(format, badgeData) errorMessages: errorMessagesFor('repo or file not found'),
} else { })
badgeData.text[1] = 'not a regular file'
sendBadge(format, badgeData)
} }
} catch (e) {
badgeData.text[1] = 'invalid' async handle({ user, repo, path }) {
sendBadge(format, badgeData) const body = await this.fetch({ user, repo, path })
if (Array.isArray(body)) {
throw new NotFound({ prettyMessage: 'not a regular file' })
} }
}) return this.constructor.render({ size: body.size })
})
)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment