[Snapcraft] license (#10520)
* Add snapcraft license. Update snapcraft version to inherit from snapcraft-base class. Add snapcraft base url in configurations. * Fix spec tests after making method static * remove snapcraft configurations, move into base class * Update services/snapcraft/snapcraft-base.js --------- Co-authored-by: chris48s <chris48s@users.noreply.github.com>
This commit is contained in:
23
services/snapcraft/snapcraft-base.js
Normal file
23
services/snapcraft/snapcraft-base.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import { BaseJsonService, pathParam } from '../index.js'
|
||||
|
||||
export const snapcraftPackageParam = pathParam({
|
||||
name: 'package',
|
||||
example: 'redis',
|
||||
})
|
||||
|
||||
export const snapcraftBaseParams = [snapcraftPackageParam]
|
||||
|
||||
const snapcraftBaseUrl = 'https://api.snapcraft.io/v2/snaps/info'
|
||||
|
||||
export default class SnapcraftBase extends BaseJsonService {
|
||||
async fetch(schema, { packageName }) {
|
||||
return await this._requestJson({
|
||||
schema,
|
||||
url: `${snapcraftBaseUrl}/${packageName}`,
|
||||
options: {
|
||||
headers: { 'Snap-Device-Series': 16 },
|
||||
},
|
||||
httpErrors: { 404: 'package not found' },
|
||||
})
|
||||
}
|
||||
}
|
||||
41
services/snapcraft/snapcraft-licence.service.js
Normal file
41
services/snapcraft/snapcraft-licence.service.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import Joi from 'joi'
|
||||
import { renderLicenseBadge } from '../licenses.js'
|
||||
import SnapcraftBase, { snapcraftPackageParam } from './snapcraft-base.js'
|
||||
|
||||
const licenseSchema = Joi.object({
|
||||
snap: Joi.object({
|
||||
license: Joi.string().required(),
|
||||
}).required(),
|
||||
}).required()
|
||||
|
||||
export default class SnapcraftLicense extends SnapcraftBase {
|
||||
static category = 'license'
|
||||
|
||||
static route = {
|
||||
base: 'snapcraft/l',
|
||||
pattern: ':package',
|
||||
}
|
||||
|
||||
static openApi = {
|
||||
'/snapcraft/l/{package}': {
|
||||
get: {
|
||||
summary: 'Snapcraft License',
|
||||
parameters: [snapcraftPackageParam],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
static render({ license }) {
|
||||
return renderLicenseBadge({ license })
|
||||
}
|
||||
|
||||
static transform(apiData) {
|
||||
return apiData.snap.license
|
||||
}
|
||||
|
||||
async handle({ package: packageName }) {
|
||||
const parsedData = await this.fetch(licenseSchema, { packageName })
|
||||
const license = this.constructor.transform(parsedData)
|
||||
return this.constructor.render({ license })
|
||||
}
|
||||
}
|
||||
14
services/snapcraft/snapcraft-licence.spec.js
Normal file
14
services/snapcraft/snapcraft-licence.spec.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { test, given } from 'sazerac'
|
||||
import SnapcraftLicense from './snapcraft-licence.service.js'
|
||||
|
||||
describe('SnapcraftLicense', function () {
|
||||
const testApiData = {
|
||||
snap: {
|
||||
license: 'BSD-3-Clause',
|
||||
},
|
||||
}
|
||||
|
||||
test(SnapcraftLicense.transform, () => {
|
||||
given(testApiData).expect('BSD-3-Clause')
|
||||
})
|
||||
})
|
||||
14
services/snapcraft/snapcraft-licence.tester.js
Normal file
14
services/snapcraft/snapcraft-licence.tester.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { createServiceTester } from '../tester.js'
|
||||
export const t = await createServiceTester()
|
||||
|
||||
t.create('Snapcraft license (valid)').get('/redis.json').expectBadge({
|
||||
label: 'license',
|
||||
message: 'BSD-3-Clause',
|
||||
})
|
||||
|
||||
t.create('Snapcraft license(invalid)')
|
||||
.get('/this_package_doesnt_exist.json')
|
||||
.expectBadge({
|
||||
label: 'license',
|
||||
message: 'package not found',
|
||||
})
|
||||
@@ -1,6 +1,7 @@
|
||||
import Joi from 'joi'
|
||||
import { BaseJsonService, NotFound, pathParams, queryParam } from '../index.js'
|
||||
import { NotFound, pathParams, queryParam } from '../index.js'
|
||||
import { renderVersionBadge } from '../version.js'
|
||||
import SnapcraftBase, { snapcraftPackageParam } from './snapcraft-base.js'
|
||||
|
||||
const queryParamSchema = Joi.object({
|
||||
arch: Joi.string(),
|
||||
@@ -22,7 +23,7 @@ const versionSchema = Joi.object({
|
||||
.required(),
|
||||
}).required()
|
||||
|
||||
export default class SnapcraftVersion extends BaseJsonService {
|
||||
export default class SnapcraftVersion extends SnapcraftBase {
|
||||
static category = 'version'
|
||||
|
||||
static route = {
|
||||
@@ -36,10 +37,10 @@ export default class SnapcraftVersion extends BaseJsonService {
|
||||
static openApi = {
|
||||
'/snapcraft/v/{package}/{track}/{risk}': {
|
||||
get: {
|
||||
summary: 'Snapcraft version',
|
||||
summary: 'Snapcraft Version',
|
||||
parameters: [
|
||||
snapcraftPackageParam,
|
||||
...pathParams(
|
||||
{ name: 'package', example: 'chromium' },
|
||||
{ name: 'track', example: 'latest' },
|
||||
{ name: 'risk', example: 'stable' },
|
||||
),
|
||||
@@ -54,7 +55,11 @@ export default class SnapcraftVersion extends BaseJsonService {
|
||||
},
|
||||
}
|
||||
|
||||
transform(apiData, track, risk, arch) {
|
||||
static render({ version }) {
|
||||
return renderVersionBadge({ version })
|
||||
}
|
||||
|
||||
static transform(apiData, track, risk, arch) {
|
||||
const channelMap = apiData['channel-map']
|
||||
let filteredChannelMap = channelMap.filter(
|
||||
({ channel }) => channel.architecture === arch,
|
||||
@@ -79,20 +84,15 @@ export default class SnapcraftVersion extends BaseJsonService {
|
||||
}
|
||||
|
||||
async handle({ package: packageName, track, risk }, { arch = 'amd64' }) {
|
||||
const parsedData = await this._requestJson({
|
||||
schema: versionSchema,
|
||||
options: {
|
||||
headers: { 'Snap-Device-Series': 16 },
|
||||
},
|
||||
url: `https://api.snapcraft.io/v2/snaps/info/${packageName}`,
|
||||
httpErrors: {
|
||||
404: 'package not found',
|
||||
},
|
||||
})
|
||||
const parsedData = await this.fetch(versionSchema, { packageName })
|
||||
|
||||
// filter results by track, risk and arch
|
||||
const { version } = this.transform(parsedData, track, risk, arch)
|
||||
|
||||
return renderVersionBadge({ version })
|
||||
const { version } = this.constructor.transform(
|
||||
parsedData,
|
||||
track,
|
||||
risk,
|
||||
arch,
|
||||
)
|
||||
return this.constructor.render({ version })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ describe('SnapcraftVersion', function () {
|
||||
],
|
||||
}
|
||||
|
||||
test(SnapcraftVersion.prototype.transform, () => {
|
||||
test(SnapcraftVersion.transform, () => {
|
||||
given(
|
||||
testApiData,
|
||||
exampleChannel.channel.track,
|
||||
@@ -66,7 +66,7 @@ describe('SnapcraftVersion', function () {
|
||||
|
||||
it('throws NotFound error with missing arch', function () {
|
||||
expect(() => {
|
||||
SnapcraftVersion.prototype.transform(
|
||||
SnapcraftVersion.transform(
|
||||
testApiData,
|
||||
exampleChannel.channel.track,
|
||||
exampleChannel.channel.risk,
|
||||
@@ -78,7 +78,7 @@ describe('SnapcraftVersion', function () {
|
||||
})
|
||||
it('throws NotFound error with missing track', function () {
|
||||
expect(() => {
|
||||
SnapcraftVersion.prototype.transform(
|
||||
SnapcraftVersion.transform(
|
||||
testApiData,
|
||||
'missing',
|
||||
exampleChannel.channel.risk,
|
||||
@@ -90,7 +90,7 @@ describe('SnapcraftVersion', function () {
|
||||
})
|
||||
it('throws NotFound error with missing risk', function () {
|
||||
expect(() => {
|
||||
SnapcraftVersion.prototype.transform(
|
||||
SnapcraftVersion.transform(
|
||||
testApiData,
|
||||
exampleChannel.channel.track,
|
||||
'missing',
|
||||
|
||||
Reference in New Issue
Block a user