-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 6 commits
40c2ae7
79d48e6
23a169a
55e959a
404c373
7534498
a8ca33c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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']) { | ||
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' }) | ||
} |
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) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
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.