Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions services/github/github-size.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Joi from 'joi'
import { renderSizeBadge } from '../size.js'
import { nonNegativeInteger } from '../validators.js'
import { NotFound, pathParam, queryParam } from '../index.js'
import { InvalidParameter, NotFound, pathParam, queryParam } from '../index.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, httpErrorsFor } from './github-helpers.js'

const queryParamSchema = Joi.object({
branch: Joi.string(),
path: Joi.string(),
}).required()

const schema = Joi.alternatives(
Expand All @@ -21,7 +22,7 @@ export default class GithubSize extends GithubAuthV3Service {

static route = {
base: 'github/size',
pattern: ':user/:repo/:path+',
pattern: ':user/:repo/:path*',
queryParamSchema,
}

Expand All @@ -42,6 +43,26 @@ export default class GithubSize extends GithubAuthV3Service {
],
},
},
'/github/size/{user}/{repo}': {
get: {
summary: 'GitHub file size in bytes from a query path',
description: documentation,
parameters: [
pathParam({ name: 'user', example: 'webcaetano' }),
pathParam({ name: 'repo', example: 'craft' }),
queryParam({
name: 'path',
example: 'build/phaser-craft.min.js',
required: true,
}),
queryParam({
name: 'branch',
example: 'master',
description: 'Can be a branch, a tag or a commit hash.',
}),
],
},
},
}

async fetch({ user, repo, path, branch }) {
Expand All @@ -60,8 +81,12 @@ export default class GithubSize extends GithubAuthV3Service {
}
}

async handle({ user, repo, path }, queryParams) {
const branch = queryParams.branch
async handle({ user, repo, path: routePath }, queryParams) {
const { branch, path: queryPath } = queryParams
const path = routePath || queryPath
if (!path) {
throw new InvalidParameter({ prettyMessage: 'path is required' })
}
const body = await this.fetch({ user, repo, path, branch })
if (Array.isArray(body)) {
throw new NotFound({ prettyMessage: 'not a regular file' })
Expand Down
13 changes: 13 additions & 0 deletions services/github/github-size.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ t.create('File size')
.get('/webcaetano/craft/build/phaser-craft.min.js.json')
.expectBadge({ label: 'size', message: isIecFileSize })

t.create('File size for a filename matching a badge format')
.get('/badges/shields.json?path=package.json')
.intercept(nock =>
nock('https://api.github.com')
.get('/repos/badges/shields/contents/package.json')
.reply(200, { size: 1024 }),
)
.expectBadge({ label: 'size', message: '1 KiB' })

t.create('File size without a path')
.get('/badges/shields.json')
.expectBadge({ label: 'size', message: 'path is required' })

t.create('File size 404')
.get('/webcaetano/craft/build/does-not-exist.min.js.json')
.expectBadge({ label: 'size', message: 'repo or file not found' })
Expand Down
Loading