Skip to content

Commit

Permalink
[Snapcraft] license (#10520)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
davide-pi and chris48s authored Sep 15, 2024
1 parent 9eaa2ed commit eca1b28
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 22 deletions.
23 changes: 23 additions & 0 deletions services/snapcraft/snapcraft-base.js
Original file line number Diff line number Diff line change
@@ -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 changes: 41 additions & 0 deletions services/snapcraft/snapcraft-licence.service.js
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions services/snapcraft/snapcraft-licence.spec.js
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions services/snapcraft/snapcraft-licence.tester.js
Original file line number Diff line number Diff line change
@@ -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',
})
36 changes: 18 additions & 18 deletions services/snapcraft/snapcraft-version.service.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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 = {
Expand All @@ -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' },
),
Expand All @@ -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,
Expand All @@ -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 })
}
}
8 changes: 4 additions & 4 deletions services/snapcraft/snapcraft-version.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('SnapcraftVersion', function () {
],
}

test(SnapcraftVersion.prototype.transform, () => {
test(SnapcraftVersion.transform, () => {
given(
testApiData,
exampleChannel.channel.track,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit eca1b28

Please sign in to comment.