-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add [snapcraft] version badge #9976
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8e15ffe
Add snapstore version badge
jNullj cfddb22
Add basic test for Snapcraft version
jNullj 062608c
Handle snapstore-version - package not found
jNullj e2f1e99
Add test for invalid snapcraft package
jNullj 600ef27
Merge branch 'master' into snapcraft
jNullj f8206d5
Remove redundent SnapstoreBase class
jNullj 992569b
Remove logo from default
jNullj 3978c71
Rename snapstore to snapcraft
jNullj f1513ee
Fix SnapcraftVersion schema
jNullj 758428e
Use renderVersionBadge
jNullj cce1946
Merge branch 'master' into snapcraft
jNullj 8361fec
Rename folder from snapstore to snapcraft
jNullj 9ddcf16
Add track & risk path parameters for SnapcraftVersion
jNullj 2c4e2c4
Add architecture query parameter to SnapcraftVersion
jNullj 75f8ac7
move filter logic into a transform function
jNullj b54f1e6
Fix filter logic
jNullj 29dd792
Add tests for SnapcraftVersion.transform
jNullj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,91 @@ | ||
import Joi from 'joi' | ||
import { BaseJsonService, NotFound, pathParams, queryParam } from '../index.js' | ||
import { renderVersionBadge } from '../version.js' | ||
|
||
const queryParamSchema = Joi.object({ | ||
arch: Joi.string(), | ||
}) | ||
|
||
const versionSchema = Joi.object({ | ||
'channel-map': Joi.array() | ||
.items( | ||
Joi.object({ | ||
channel: Joi.object({ | ||
architecture: Joi.string().required(), | ||
risk: Joi.string().required(), | ||
track: Joi.string().required(), | ||
}), | ||
version: Joi.string().required(), | ||
}).required(), | ||
) | ||
.min(1) | ||
.required(), | ||
}).required() | ||
|
||
export default class SnapcraftVersion extends BaseJsonService { | ||
static category = 'version' | ||
|
||
static route = { | ||
base: 'snapcraft/v', | ||
pattern: ':package/:track/:risk', | ||
queryParamSchema, | ||
} | ||
|
||
static defaultBadgeData = { label: 'snapcraft' } | ||
|
||
static openApi = { | ||
'/snapcraft/v/{package}/{track}/{risk}': { | ||
get: { | ||
summary: 'Snapcraft version', | ||
parameters: [ | ||
...pathParams( | ||
{ name: 'package', example: 'chromium' }, | ||
{ name: 'track', example: 'latest' }, | ||
{ name: 'risk', example: 'stable' }, | ||
), | ||
queryParam({ | ||
name: 'arch', | ||
example: 'amd64', | ||
description: | ||
'Architecture, When not specified, this will default to `amd64`.', | ||
}), | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
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 channelMap = parsedData['channel-map'] | ||
let filteredChannelMap = channelMap.filter( | ||
({ channel }) => channel.architecture === arch, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'arch not found' }) | ||
} | ||
filteredChannelMap = channelMap.filter( | ||
({ channel }) => channel.track === track, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'track not found' }) | ||
} | ||
filteredChannelMap = channelMap.filter( | ||
({ channel }) => channel.risk === risk, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'risk not found' }) | ||
} | ||
|
||
return renderVersionBadge({ version: filteredChannelMap[0].version }) | ||
} | ||
} |
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,45 @@ | ||
import { isSemver } from '../test-validators.js' | ||
import { createServiceTester } from '../tester.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('Snapcraft Version for redis') | ||
.get('/redis/latest/stable.json') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: isSemver, | ||
}) | ||
|
||
t.create('Snapcraft Version for redis (query param arch=arm64)') | ||
.get('/redis/latest/stable.json?arch=arm64') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: isSemver, | ||
}) | ||
|
||
t.create('Snapcraft Version for redis (invalid package)') | ||
.get('/this_package_doesnt_exist/fake/fake.json') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: 'package not found', | ||
}) | ||
|
||
t.create('Snapcraft Version for redis (invalid track)') | ||
.get('/redis/notfound/stable.json') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: 'track not found', | ||
}) | ||
|
||
t.create('Snapcraft Version for redis (invalid risk)') | ||
.get('/redis/latest/notfound.json') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: 'risk not found', | ||
}) | ||
|
||
t.create('Snapcraft Version for redis (invalid arch)') | ||
.get('/redis/latest/stable.json?arch=fake') | ||
.expectBadge({ | ||
label: 'snapcraft', | ||
message: 'arch not found', | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code looks wrong to me. These filters should be combined with AND logic, but we're filtering
channelMap
, assigning the result tofilteredChannelMap
and then essentially throwing away that result and overwritingfilteredChannelMap
. We're only really applying the last filter.I think you really want to be doing
for the second and third pass.
What would really help here would be if we split this filtering logic out into a
transform()
function and write a few unit tests (in a spec.js) to make sure this is working as expected.You might look at something like
services/docker/docker-version.spec.js
or
services/crates/crates-base.spec.js
as a point of inspiration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, i copy-pasted without noticing i forgot to change to filteredChannelMap
Fixed filter logic to AND with b54f1e6
Moved the filter logic into transform with 75f8ac7
And most impotent, added testing with 29dd792
This test file is really called for.