Add [Conan] version service (#7460)

* Add [Conan] version service

* Rework to use conan-center-index GitHub repo

* Conditional mock based on presence of github token

* Refactor yaml parsing into a helper function, move tests to .spec.js

* remove custom version parsing

* improve test data

* updates from review

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
Jacob Bandes-Storch
2022-02-28 22:08:45 -08:00
committed by GitHub
parent 4ccee50ce2
commit 814aa30da4
4 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import yaml from 'js-yaml'
import { NotFound, InvalidResponse } from '../index.js'
import { latest } from '../version.js'
export function parseLatestVersionFromConfig(configYaml) {
let versions
try {
const config = yaml.load(configYaml)
versions = Object.keys(config.versions)
} catch (err) {
throw new InvalidResponse({
prettyMessage: 'invalid config.yml',
underlyingError: err,
})
}
const version = latest(versions)
if (version == null) {
throw new NotFound({ prettyMessage: 'no versions found' })
}
return version
}

View File

@@ -0,0 +1,33 @@
import { expect } from 'chai'
import { NotFound, InvalidResponse } from '../index.js'
import { parseLatestVersionFromConfig } from './conan-version-helpers.js'
describe('parseLatestVersionFromConfig', function () {
it('returns latest available version', function () {
expect(
parseLatestVersionFromConfig(`
versions:
1.68.0:
folder: all
1.70.0:
folder: all
1.69.0:
folder: all
`)
).to.equal('1.70.0')
})
it('rejects invalid yaml', function () {
expect(() => parseLatestVersionFromConfig('[')).to.throw(InvalidResponse)
})
it('treats no results array as invalid', function () {
expect(() =>
parseLatestVersionFromConfig('somethingElse: whatever')
).to.throw(InvalidResponse)
})
it('treats empty results array as not found', function () {
expect(() => parseLatestVersionFromConfig('versions: []')).to.throw(
NotFound
)
})
})

View File

@@ -0,0 +1,34 @@
import { renderVersionBadge } from '../version.js'
import { ConditionalGithubAuthV3Service } from '../github/github-auth-service.js'
import { fetchRepoContent } from '../github/github-common-fetch.js'
import { parseLatestVersionFromConfig } from './conan-version-helpers.js'
export default class ConanVersion extends ConditionalGithubAuthV3Service {
static category = 'version'
static route = { base: 'conan/v', pattern: ':packageName' }
static examples = [
{
title: 'Conan Center',
namedParams: { packageName: 'boost' },
staticPreview: renderVersionBadge({ version: '1.78.0' }),
keywords: ['c++'],
},
]
static defaultBadgeData = { label: 'conan' }
async handle({ packageName }) {
const configContent = await fetchRepoContent(this, {
user: 'conan-io',
repo: 'conan-center-index',
branch: 'master',
filename: `recipes/${packageName}/config.yml`,
})
const version = parseLatestVersionFromConfig(configContent)
return renderVersionBadge({ version })
}
}

View File

@@ -0,0 +1,17 @@
import { isSemver } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()
t.create('gets the package version of zeromq')
.get('/zeromq.json')
.expectBadge({ label: 'conan', message: isSemver })
t.create('returns not found for invalid package')
.get('/this package does not exist - shields test.json')
.expectBadge({
label: 'conan',
color: 'red',
message:
'repo not found, branch not found, or recipes/this package does not exist - shields test/config.yml missing',
})