[GitHub] GitHub file size for a specific branch (#8262)

* Modify github-size.service.js

* Add tests for Github Size Service to test the new ref parameter

* Modified request for a file size: ref is now sent only if if was specified
This commit is contained in:
Paula Barszcz
2022-08-02 23:33:13 +02:00
committed by GitHub
parent 1954963cd3
commit fa3839cdab
2 changed files with 52 additions and 8 deletions

View File

@@ -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' })
}

View File

@@ -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 })