diff --git a/services/github/github-size.service.js b/services/github/github-size.service.js index b932faf17e21a0eb3ec9da6142de183f7041a431..3dfa6692b15dbef159f6af28e52a21e1343e7ef0 100644 --- a/services/github/github-size.service.js +++ b/services/github/github-size.service.js @@ -5,6 +5,10 @@ import { NotFound } from '../index.js' import { GithubAuthV3Service } from './github-auth-service.js' import { documentation, errorMessagesFor } from './github-helpers.js' +const queryParamSchema = Joi.object({ + branch: Joi.string(), +}).required() + const schema = Joi.alternatives( Joi.object({ size: nonNegativeInteger, @@ -18,6 +22,7 @@ export default class GithubSize extends GithubAuthV3Service { static route = { base: 'github/size', pattern: ':user/:repo/:path*', + queryParamSchema, } static examples = [ @@ -32,6 +37,20 @@ export default class GithubSize extends GithubAuthV3Service { keywords: ['repo'], documentation, }, + { + title: 'GitHub file size in bytes on a specified ref (branch/commit/tag)', + namedParams: { + user: 'webcaetano', + repo: 'craft', + path: 'build/phaser-craft.min.js', + }, + staticPreview: this.render({ size: 9170 }), + keywords: ['repo'], + documentation, + queryParams: { + branch: 'master', + }, + }, ] static render({ size }) { @@ -41,16 +60,25 @@ export default class GithubSize extends GithubAuthV3Service { } } - async fetch({ user, repo, path }) { - return this._requestJson({ - url: `/repos/${user}/${repo}/contents/${path}`, - schema, - errorMessages: errorMessagesFor('repo or file not found'), - }) + async fetch({ user, repo, path, branch }) { + if (branch) { + return this._requestJson({ + url: `/repos/${user}/${repo}/contents/${path}?ref=${branch}`, + schema, + errorMessages: errorMessagesFor('repo, branch or file not found'), + }) + } else { + return this._requestJson({ + url: `/repos/${user}/${repo}/contents/${path}`, + schema, + errorMessages: errorMessagesFor('repo or file not found'), + }) + } } - async handle({ user, repo, path }) { - const body = await this.fetch({ user, repo, path }) + async handle({ user, repo, path }, queryParams) { + const branch = queryParams.branch + const body = await this.fetch({ user, repo, path, branch }) if (Array.isArray(body)) { throw new NotFound({ prettyMessage: 'not a regular file' }) } diff --git a/services/github/github-size.tester.js b/services/github/github-size.tester.js index db61aa9ac9211eec724ba6feef27d4c626714c5f..644b482dbafb483c95736a1367db8a0746dd38e5 100644 --- a/services/github/github-size.tester.js +++ b/services/github/github-size.tester.js @@ -10,6 +10,22 @@ t.create('File size 404') .get('/webcaetano/craft/build/does-not-exist.min.js.json') .expectBadge({ label: 'size', message: 'repo or file not found' }) +t.create('File size for nonexisting branch') + .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=notARealBranch') + .expectBadge({ label: 'size', message: 'repo, branch or file not found' }) + t.create('File size for "not a regular file"') .get('/webcaetano/craft/build.json') .expectBadge({ label: 'size', message: 'not a regular file' }) + +t.create('File size for a specified branch') + .get('/webcaetano/craft/build/craft.min.js.json?branch=version-2') + .expectBadge({ label: 'size', message: isFileSize }) + +t.create('File size for a specified tag') + .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=2.1.2') + .expectBadge({ label: 'size', message: isFileSize }) + +t.create('File size for a specified commit') + .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=b848dbb') + .expectBadge({ label: 'size', message: isFileSize })