-
-
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.
[Snapcraft] - Added snapcraft last update badge (#10610)
* Added badge for Maven-Cenral last update. * Update services/maven-central/maven-central-last-update.service.js Co-authored-by: chris48s <chris48s@users.noreply.github.com> * updated according to the review comments. * added Snapcraft last updated badges. * made changes according to the review. --------- Co-authored-by: chris48s <chris48s@users.noreply.github.com>
- Loading branch information
1 parent
d502e70
commit 1d2bf19
Showing
2 changed files
with
156 additions
and
0 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,110 @@ | ||
import Joi from 'joi' | ||
import dayjs from 'dayjs' | ||
import { pathParams, queryParam, NotFound, InvalidResponse } from '../index.js' | ||
import { formatDate } from '../text-formatters.js' | ||
import { age as ageColor } from '../color-formatters.js' | ||
import SnapcraftBase, { snapcraftPackageParam } from './snapcraft-base.js' | ||
|
||
const queryParamSchema = Joi.object({ | ||
arch: Joi.string(), | ||
}) | ||
|
||
const lastUpdateSchema = Joi.object({ | ||
'channel-map': Joi.array() | ||
.items( | ||
Joi.object({ | ||
channel: Joi.object({ | ||
architecture: Joi.string().required(), | ||
risk: Joi.string().required(), | ||
track: Joi.string().required(), | ||
'released-at': Joi.string().required(), | ||
}), | ||
}).required(), | ||
) | ||
.min(1) | ||
.required(), | ||
}).required() | ||
|
||
export default class SnapcraftLastUpdate extends SnapcraftBase { | ||
static category = 'activity' | ||
|
||
static route = { | ||
base: 'snapcraft/last-update', | ||
pattern: ':package/:track/:risk', | ||
queryParamSchema, | ||
} | ||
|
||
static defaultBadgeData = { label: 'last updated' } | ||
|
||
static openApi = { | ||
'/snapcraft/last-update/{package}/{track}/{risk}': { | ||
get: { | ||
summary: 'Snapcraft Last Update', | ||
parameters: [ | ||
snapcraftPackageParam, | ||
...pathParams( | ||
{ name: 'track', example: 'latest' }, | ||
{ name: 'risk', example: 'stable' }, | ||
), | ||
queryParam({ | ||
name: 'arch', | ||
example: 'amd64', | ||
description: | ||
'Architecture, when not specified, this will default to `amd64`.', | ||
}), | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
static render({ lastUpdatedDate }) { | ||
return { | ||
message: formatDate(lastUpdatedDate), | ||
color: ageColor(lastUpdatedDate), | ||
} | ||
} | ||
|
||
static transform(apiData, track, risk, arch) { | ||
const channelMap = apiData['channel-map'] | ||
let filteredChannelMap = channelMap.filter( | ||
({ channel }) => channel.architecture === arch, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'arch not found' }) | ||
} | ||
filteredChannelMap = filteredChannelMap.filter( | ||
({ channel }) => channel.track === track, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'track not found' }) | ||
} | ||
filteredChannelMap = filteredChannelMap.filter( | ||
({ channel }) => channel.risk === risk, | ||
) | ||
if (filteredChannelMap.length === 0) { | ||
throw new NotFound({ prettyMessage: 'risk not found' }) | ||
} | ||
|
||
return filteredChannelMap[0] | ||
} | ||
|
||
async handle({ package: packageName, track, risk }, { arch = 'amd64' }) { | ||
const parsedData = await this.fetch(lastUpdateSchema, { packageName }) | ||
|
||
// filter results by track, risk and arch | ||
const { channel } = this.constructor.transform( | ||
parsedData, | ||
track, | ||
risk, | ||
arch, | ||
) | ||
|
||
const lastUpdatedDate = dayjs(channel['released-at']) | ||
|
||
if (!lastUpdatedDate.isValid) { | ||
throw new InvalidResponse({ prettyMessage: 'invalid date' }) | ||
} | ||
|
||
return this.constructor.render({ lastUpdatedDate }) | ||
} | ||
} |
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,46 @@ | ||
import { isFormattedDate } from '../test-validators.js' | ||
import { createServiceTester } from '../tester.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('last update for redis/latest/stable') | ||
.get('/redis/latest/stable.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('last update for redis/latest/stable and query param arch=arm64') | ||
.get('/redis/latest/stable.json?arch=arm64') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('last update when package not found') | ||
.get('/fake_package/fake/fake.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'package not found', | ||
}) | ||
|
||
t.create('last update for redis and invalid track') | ||
.get('/redis/notFound/stable.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'track not found', | ||
}) | ||
|
||
t.create('last update for redis/latest and invalid risk') | ||
.get('/redis/latest/notFound.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'risk not found', | ||
}) | ||
|
||
t.create('last update for redis/latest/stable and invalid arch (query param)') | ||
.get('/redis/latest/stable.json?arch=fake') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'arch not found', | ||
}) |