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

[GithubRelease],[GithubTag] filter by prefix #8205

Closed
wants to merge 12 commits into from
20 changes: 17 additions & 3 deletions services/github/github-common-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ async function fetchReleases(serviceInstance, { user, repo }) {
return releases
}

function getLatestRelease({ releases, sort, includePrereleases }) {
function getLatestRelease({ releases, sort, includePrereleases, prefix }) {
if (prefix) {
releases = releases.filter(r => r.tag_name.startsWith(prefix))
if (releases.length === 0) {
throw new NotFound({ prettyMessage: 'no tags found' })
}
}
if (sort === 'semver') {
const latestTagName = latest(
releases.map(release => release.tag_name),
Expand All @@ -68,6 +74,8 @@ function getLatestRelease({ releases, sort, includePrereleases }) {
const queryParamSchema = Joi.object({
include_prereleases: Joi.equal(''),
sort: Joi.string().valid('date', 'semver').default('date'),
prefix: Joi.alternatives().try(Joi.string(), Joi.number()),
trimPrefixAtSlash: Joi.equal(''),
}).required()

// Fetch the latest release as defined by query params
Expand All @@ -78,8 +86,9 @@ async function fetchLatestRelease(
) {
const sort = queryParams.sort
const includePrereleases = queryParams.include_prereleases !== undefined
const prefix = queryParams.prefix

if (!includePrereleases && sort === 'date') {
if (!includePrereleases && sort === 'date' && !prefix) {
const releaseInfo = await fetchLatestGitHubRelease(serviceInstance, {
user,
repo,
Expand All @@ -91,7 +100,12 @@ async function fetchLatestRelease(
if (releases.length === 0) {
throw new NotFound({ prettyMessage: 'no releases' })
}
const latestRelease = getLatestRelease({ releases, sort, includePrereleases })
const latestRelease = getLatestRelease({
releases,
sort,
includePrereleases,
prefix,
})
return latestRelease
}

Expand Down
13 changes: 13 additions & 0 deletions services/github/github-common-release.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('GithubRelease', function () {
const releaseFixture = [
{ tag_name: 'cheese', prerelease: false }, // any old string
{ tag_name: 'v1.2', prerelease: false }, // semver release
{ tag_name: 'example/v1.1', prerelease: false }, // prefix release
{ tag_name: 'v1.3-beta3', prerelease: true }, // semver pre-release
]
given({
Expand All @@ -28,6 +29,18 @@ describe('GithubRelease', function () {
sort: 'date',
includePrereleases: false,
}).expect({ tag_name: 'cheese', prerelease: false })
given({
releases: releaseFixture,
sort: 'date',
prefix: 'example',
includePrereleases: true,
}).expect({ tag_name: 'example/v1.1', prerelease: false })
given({
releases: releaseFixture,
sort: 'semver',
prefix: 'example',
includePrereleases: false,
}).expect({ tag_name: 'example/v1.1', prerelease: false })

// if there are only pre-releases to choose from
// return a pre-release anyway in preference to nothing
Expand Down
11 changes: 11 additions & 0 deletions services/github/github-release.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ class GithubRelease extends GithubAuthV3Service {
}),
documentation,
},
{
title: 'GitHub release (latest filter by prefix)',
namedParams: { user: 'expressjs', repo: 'express' },
queryParams: {
prefix: '4.16',
},
staticPreview: this.render({
version: 'v4.16.4',
}),
documentation,
},
]

static defaultBadgeData = { label: 'release', namedLogo: 'github' }
Expand Down
56 changes: 49 additions & 7 deletions services/github/github-tag.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ class GithubTag extends GithubAuthV4Service {
}),
documentation,
},
{
title: 'GitHub tag (latest filter by prefix)',
namedParams: { user: 'ros', repo: 'rosdistro' },
queryParams: { prefix: 'galactic/', trimPrefixAtSlash: null },
staticPreview: this.render({
version: '2022-06-24',
}),
documentation,
},
]

static defaultBadgeData = {
Expand All @@ -73,14 +82,24 @@ class GithubTag extends GithubAuthV4Service {
}
}

async fetch({ user, repo, sort }) {
const limit = sort === 'semver' ? 100 : 1
async fetch({ user, repo, prefix, increaselimit }) {
const limit = increaselimit ? 100 : 1
let refPrefix = 'refs/tags/'
if (prefix) {
refPrefix = `${refPrefix}${prefix}/`
}

return this._requestGraphql({
query: gql`
query ($user: String!, $repo: String!, $limit: Int!) {
query (
$user: String!
$repo: String!
$limit: Int!
$refPrefix: String!
) {
repository(owner: $user, name: $repo) {
refs(
refPrefix: "refs/tags/"
refPrefix: $refPrefix
first: $limit
orderBy: { field: TAG_COMMIT_DATE, direction: DESC }
) {
Expand All @@ -93,13 +112,19 @@ class GithubTag extends GithubAuthV4Service {
}
}
`,
variables: { user, repo, limit },
variables: { user, repo, limit, refPrefix },
schema,
transformErrors,
})
}

static getLatestTag({ tags, sort, includePrereleases }) {
static getLatestTag({ tags, sort, includePrereleases, prefix }) {
if (prefix) {
tags = tags.filter(tag => tag.startsWith(prefix))
if (tags.length === 0) {
throw new NotFound({ prettyMessage: 'no tags found' })
}
}
if (sort === 'semver') {
return latest(tags, { pre: includePrereleases })
}
Expand All @@ -109,8 +134,24 @@ class GithubTag extends GithubAuthV4Service {
async handle({ user, repo }, queryParams) {
const sort = queryParams.sort
const includePrereleases = queryParams.include_prereleases !== undefined
const prefix = queryParams.prefix
const trimPrefixAtSlash = queryParams.trimPrefixAtSlash !== undefined

const json = await this.fetch({ user, repo, sort })
let filterPrefix = prefix ? prefix.toString() : ''
let graphQLPrefix = ''
if (trimPrefixAtSlash && filterPrefix.includes('/')) {
const lastIndex = filterPrefix.lastIndexOf('/')
graphQLPrefix = filterPrefix.slice(0, lastIndex)
filterPrefix = filterPrefix.slice(lastIndex + 1)
}

const increaselimit = sort === 'semver' || filterPrefix.length > 0
const json = await this.fetch({
user,
repo,
prefix: graphQLPrefix,
increaselimit,
})
const tags = json.data.repository.refs.edges.map(edge => edge.node.name)
if (tags.length === 0) {
throw new NotFound({ prettyMessage: 'no tags found' })
Expand All @@ -120,6 +161,7 @@ class GithubTag extends GithubAuthV4Service {
tags,
sort,
includePrereleases,
prefix: filterPrefix,
}),
sort,
})
Expand Down
13 changes: 13 additions & 0 deletions services/github/github-tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('GithubTag', function () {
test(GithubTag.getLatestTag, () => {
const tagFixture = [
'cheese', // any old string
'example/v1.1', // prefix release
'v1.2', // semver release
'v1.3-beta3', // semver pre-release
]
Expand All @@ -28,6 +29,18 @@ describe('GithubTag', function () {
sort: 'date',
includePrereleases: false,
}).expect('cheese')
given({
tags: tagFixture,
sort: 'date',
prefix: 'example',
includePrereleases: false,
}).expect('example/v1.1')
given({
tags: tagFixture,
sort: 'semver',
prefix: 'example',
includePrereleases: false,
}).expect('example/v1.1')

// if there are only pre-releases to choose from
// return a pre-release anyway in preference to nothing
Expand Down
14 changes: 14 additions & 0 deletions services/github/github-tag.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ t.create('Tag (repo not found)')
.get('/v/tag/badges/helmets.json')
.expectBadge({ label: 'tag', message: 'repo not found' })

t.create('Tag (filter by prefix + trim prefix at slash)')
.get('/v/tag/ros/rosdistro.json?prefix=galactic/&trimPrefixAtSlash')
.expectBadge({
label: 'tag',
message: Joi.alternatives()
.try(
Joi.date().required(),
Joi.string()
.pattern(/^20.*$/)
.required()
)
.match('all'),
})

// redirects
t.create('Tag (legacy route: tag)')
.get('/tag/photonstorm/phaser.svg')
Expand Down