Skip to content

Commit

Permalink
Add [Vcpkg] version service (#8923)
Browse files Browse the repository at this point in the history
Co-authored-by: chris48s <chris48s@users.noreply.github.com>
  • Loading branch information
njakob and chris48s committed Feb 20, 2023
1 parent 174b191 commit 303fb86
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
49 changes: 49 additions & 0 deletions services/vcpkg/vcpkg-version.service.js
@@ -0,0 +1,49 @@
import Joi from 'joi'
import { ConditionalGithubAuthV3Service } from '../github/github-auth-service.js'
import { fetchJsonFromRepo } from '../github/github-common-fetch.js'
import { renderVersionBadge } from '../version.js'
import { NotFound } from '../index.js'

const vcpkgManifestSchema = Joi.object({
version: Joi.string().required(),
}).required()

export default class VcpkgVersion extends ConditionalGithubAuthV3Service {
static category = 'version'

static route = { base: 'vcpkg/v', pattern: ':portName' }

static examples = [
{
title: 'Vcpkg',
namedParams: { portName: 'entt' },
staticPreview: this.render({ version: '3.11.1' }),
},
]

static defaultBadgeData = { label: 'vcpkg' }

static render({ version }) {
return renderVersionBadge({ version })
}

async handle({ portName }) {
try {
const { version } = await fetchJsonFromRepo(this, {
schema: vcpkgManifestSchema,
user: 'microsoft',
repo: 'vcpkg',
branch: 'master',
filename: `ports/${portName}/vcpkg.json`,
})
return this.constructor.render({ version })
} catch (error) {
if (error instanceof NotFound) {
throw new NotFound({
prettyMessage: 'port not found',
})
}
throw error
}
}
}
16 changes: 16 additions & 0 deletions services/vcpkg/vcpkg-version.tester.js
@@ -0,0 +1,16 @@
import { isSemver } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('gets the port version of entt')
.get('/entt.json')
.expectBadge({ label: 'vcpkg', message: isSemver })

t.create('returns not found for invalid port')
.get('/this-port-does-not-exist.json')
.expectBadge({
label: 'vcpkg',
color: 'red',
message: 'port not found',
})

0 comments on commit 303fb86

Please sign in to comment.