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

[Snapcraft] license #10520

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions config/custom-environment-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public:
authorizedOrigins: 'OBS_ORIGINS'
pypi:
baseUri: 'PYPI_URL'
snapcraft:
baseUri: 'SNAPCRAFT_URL'
sonar:
authorizedOrigins: 'SONAR_ORIGINS'
teamcity:
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public:
authorizedOrigins: 'https://api.opensuse.org'
pypi:
baseUri: 'https://pypi.org'
snapcraft:
baseUri: 'https://api.snapcraft.io/v2/snaps/info'
chris48s marked this conversation as resolved.
Show resolved Hide resolved
weblate:
authorizedOrigins: 'https://hosted.weblate.org'
trace: false
Expand Down
3 changes: 3 additions & 0 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ const publicConfigSchema = Joi.object({
pypi: {
baseUri: requiredUrl,
},
snapcraft: {
baseUri: requiredUrl,
},
sonar: defaultService,
teamcity: defaultService,
weblate: defaultService,
Expand Down
6 changes: 6 additions & 0 deletions doc/server-secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ Create an account, sign in and obtain generate a key on your
`PYPI_URL` can be used to optionally send all the PyPI requests to a Self-hosted Pypi registry,
users can also override this by query parameter `pypiBaseUrl`.

### Snapcraft

- `SNAPCRAFT_URL` (yml: `public.snapcraft.baseUri`)

`SNAPCRAFT_URL` can be used to optionally send all the Snapcraft requests to a Self-hosted Snapcraft registry.

### SymfonyInsight (formerly Sensiolabs)

- `SL_INSIGHT_USER_UUID` (yml: `private.sl_insight_userUuid`)
Expand Down
24 changes: 24 additions & 0 deletions services/snapcraft/snapcraft-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import config from 'config'
import { BaseJsonService, pathParam } from '../index.js'

export const snapcraftPackageParam = pathParam({
name: 'package',
example: 'Redis',
chris48s marked this conversation as resolved.
Show resolved Hide resolved
})

export const snapcraftBaseParams = [snapcraftPackageParam]

export default class SnapcraftBase extends BaseJsonService {
async fetch(schema, { packageName }) {
const snapcraftBaseUrl =
config.util.toObject().public.services.snapcraft.baseUri
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
Loading