-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
6 changed files
with
114 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters