Skip to content

Commit

Permalink
Support nested groups on [GitLabTag] badge (#7158)
Browse files Browse the repository at this point in the history
* refactor: support groups on gitlab tag badge

* fix mocked test

* add nested subgroup test

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
calebcartwright and repo-ranger[bot] authored Oct 16, 2021
1 parent d7316f9 commit 694b07d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
43 changes: 24 additions & 19 deletions services/gitlab/gitlab-tag.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,43 @@ const queryParamSchema = Joi.object({
sort: Joi.string().valid('date', 'semver').default('date'),
}).required()

const documentation = `
<p>
You may use your GitLab Project Id (e.g. 25813592) or your Project Path (e.g. megabyte-labs/dockerfile/ci-pipeline/ansible-lint)
</p>
`
const commonProps = {
namedParams: {
project: 'shields-ops-group/tag-test',
},
documentation,
}

export default class GitlabTag extends GitLabBase {
static category = 'version'

static route = {
base: 'gitlab/v/tag',
pattern: ':user/:repo',
pattern: ':project+',
queryParamSchema,
}

static examples = [
{
title: 'GitLab tag (latest by date)',
namedParams: {
user: 'shields-ops-group',
repo: 'tag-test',
},
...commonProps,
queryParams: { sort: 'date' },
staticPreview: this.render({ version: 'v2.0.0' }),
},
{
title: 'GitLab tag (latest by SemVer)',
namedParams: {
user: 'shields-ops-group',
repo: 'tag-test',
},
...commonProps,
queryParams: { sort: 'semver' },
staticPreview: this.render({ version: 'v4.0.0' }),
},
{
title: 'GitLab tag (latest by SemVer pre-release)',
namedParams: {
user: 'shields-ops-group',
repo: 'tag-test',
},
...commonProps,
queryParams: {
sort: 'semver',
include_prereleases: null,
Expand All @@ -61,9 +64,9 @@ export default class GitlabTag extends GitLabBase {
{
title: 'GitLab tag (custom instance)',
namedParams: {
user: 'GNOME',
repo: 'librsvg',
project: 'GNOME/librsvg',
},
documentation,
queryParams: {
sort: 'semver',
include_prereleases: null,
Expand All @@ -82,14 +85,16 @@ export default class GitlabTag extends GitLabBase {
}
}

async fetch({ user, repo, baseUrl }) {
async fetch({ project, baseUrl }) {
// https://docs.gitlab.com/ee/api/tags.html
// N.B. the documentation has contradictory information about default sort order.
// As of 2020-10-11 the default is by date, but we add the `order_by` query param
// explicitly in case that changes upstream.
return super.fetch({
schema,
url: `${baseUrl}/api/v4/projects/${user}%2F${repo}/repository/tags`,
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(
project
)}/repository/tags`,
options: { qs: { order_by: 'updated' } },
errorMessages: {
404: 'repo not found',
Expand All @@ -113,14 +118,14 @@ export default class GitlabTag extends GitLabBase {
}

async handle(
{ user, repo },
{ project },
{
gitlab_url: baseUrl = 'https://gitlab.com',
include_prereleases: pre,
sort,
}
) {
const tags = await this.fetch({ user, repo, baseUrl })
const tags = await this.fetch({ project, baseUrl })
const version = this.constructor.transform({
tags,
sort,
Expand Down
2 changes: 1 addition & 1 deletion services/gitlab/gitlab-tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('GitLabTag', function () {
await GitLabTag.invoke(
defaultContext,
config,
{ user: 'foo', repo: 'bar' },
{ project: 'foo/bar' },
{}
)
).to.deep.equal({
Expand Down
10 changes: 9 additions & 1 deletion services/gitlab/gitlab-tag.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ t.create('Tag (latest by date)')
.get('/shields-ops-group/tag-test.json')
.expectBadge({ label: 'tag', message: 'v2.0.0', color: 'blue' })

t.create('Tag (nested groups)')
.get('/megabyte-labs/dockerfile/ci-pipeline/ansible-lint.json')
.expectBadge({ label: 'tag', message: isSemver, color: 'blue' })

t.create('Tag (project id latest by date)')
.get('/29538796.json')
.expectBadge({ label: 'tag', message: 'v2.0.0', color: 'blue' })

t.create('Tag (latest by SemVer)')
.get('/shields-ops-group/tag-test.json?sort=semver')
.expectBadge({ label: 'tag', message: 'v4.0.0', color: 'blue' })
Expand All @@ -14,7 +22,7 @@ t.create('Tag (latest by SemVer pre-release)')
.get('/shields-ops-group/tag-test.json?sort=semver&include_prereleases')
.expectBadge({ label: 'tag', message: 'v5.0.0-beta.1', color: 'orange' })

t.create('Tag (custom instance')
t.create('Tag (custom instance)')
.get('/GNOME/librsvg.json?gitlab_url=https://gitlab.gnome.org')
.expectBadge({ label: 'tag', message: isSemver, color: 'blue' })

Expand Down

0 comments on commit 694b07d

Please sign in to comment.