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

Support nested groups on [GitLabTag] badge #7158

Merged
merged 4 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' })

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. I reckon we should probably also add a test case with a repo that is nested in a group (and make that standard for gitlab badges).

Copy link
Member Author

@calebcartwright calebcartwright Oct 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me. I did add one such test as part of #7159 to update the Pipeline status badge and can use the same project as the test case here since they do have tags. However, that project doesn't have any releases published and I don't know of any nested examples that do for #7021.

I don't think trying to use such a target with a "no releases found" test would work either, since a bad project path route produces a 404 response as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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