Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix [Vcpkg] version service for different version fields #8945

Merged
merged 7 commits into from
Mar 4, 2023
Merged
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
17 changes: 17 additions & 0 deletions services/vcpkg/vcpkg-version-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { InvalidResponse } from '../index.js'

export function parseVersionFromVcpkgManifest(manifest) {
if (manifest['version-date']) {
Copy link
Contributor Author

@njakob njakob Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: It would have been better to also validate the schema directly inside the helper. However, this would require working around the current validation API in place through the service class and directly using validate or the Joi schema validation.

However, looking at other usages of fetchJsonFromRepo, I left the schema validation outside of this parsing helper.

return manifest['version-date']
}
if (manifest['version-semver']) {
return manifest['version-semver']
}
if (manifest['version-string']) {
return manifest['version-string']
}
if (manifest.version) {
return manifest.version
}
throw new InvalidResponse({ prettyMessage: 'missing version' })
}
41 changes: 41 additions & 0 deletions services/vcpkg/vcpkg-version-helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai'
import { InvalidResponse } from '../index.js'
import { parseVersionFromVcpkgManifest } from './vcpkg-version-helpers.js'

describe('parseVersionFromVcpkgManifest', function () {
it('returns a version when `version` field is detected', function () {
expect(
parseVersionFromVcpkgManifest({
version: '2.12.1',
})
).to.equal('2.12.1')
})

it('returns a version when `version-date` field is detected', function () {
expect(
parseVersionFromVcpkgManifest({
'version-date': '2022-12-04',
})
).to.equal('2022-12-04')
})

it('returns a version when `version-semver` field is detected', function () {
expect(
parseVersionFromVcpkgManifest({
'version-semver': '3.11.2',
})
).to.equal('3.11.2')
})

it('returns a version when `version-date` field is detected', function () {
expect(
parseVersionFromVcpkgManifest({
'version-string': '22.01',
})
).to.equal('22.01')
})

it('rejects when no version field variant is detected', function () {
expect(() => parseVersionFromVcpkgManifest('{}')).to.throw(InvalidResponse)
})
})
25 changes: 21 additions & 4 deletions services/vcpkg/vcpkg-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@ 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'
import { parseVersionFromVcpkgManifest } from './vcpkg-version-helpers.js'

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can add a comment here linking to https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json?source=recommendations#version to make it a bit more obvious what is going on here

Copy link
Contributor Author

@njakob njakob Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea to directly link the documentation, I added it directly as a comment!

// Handle the different version fields available in Vcpkg manifests
// https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json?source=recommendations#version
const vcpkgManifestSchema = Joi.alternatives()
.match('one')
.try(
Joi.object({
version: Joi.string().required(),
}).required(),
Joi.object({
'version-date': Joi.string().required(),
}).required(),
Joi.object({
'version-semver': Joi.string().required(),
}).required(),
Joi.object({
'version-string': Joi.string().required(),
}).required()
)

export default class VcpkgVersion extends ConditionalGithubAuthV3Service {
static category = 'version'
Expand All @@ -29,13 +45,14 @@ export default class VcpkgVersion extends ConditionalGithubAuthV3Service {

async handle({ portName }) {
try {
const { version } = await fetchJsonFromRepo(this, {
const manifest = await fetchJsonFromRepo(this, {
schema: vcpkgManifestSchema,
user: 'microsoft',
repo: 'vcpkg',
branch: 'master',
filename: `ports/${portName}/vcpkg.json`,
})
const version = parseVersionFromVcpkgManifest(manifest)
return this.constructor.render({ version })
} catch (error) {
if (error instanceof NotFound) {
Expand Down
8 changes: 4 additions & 4 deletions services/vcpkg/vcpkg-version.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ 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('gets nlohmann-json port version')
.get('/nlohmann-json.json')
.expectBadge({ label: 'vcpkg', color: 'blue', message: isSemver })

t.create('returns not found for invalid port')
t.create('gets not found error for invalid port')
.get('/this-port-does-not-exist.json')
.expectBadge({
label: 'vcpkg',
Expand Down