Skip to content

Commit

Permalink
Supported nested groups on [GitLabPipeline] badge (#7159)
Browse files Browse the repository at this point in the history
* feat: supported nested groups on gitlab pipeline status badge

* satiate the formatting gods

* use main as default branch

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
calebcartwright and repo-ranger[bot] committed Oct 16, 2021
1 parent f8f1286 commit a43c98a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 28 deletions.
68 changes: 50 additions & 18 deletions services/gitlab/gitlab-pipeline-status.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const badgeSchema = Joi.object({

const queryParamSchema = Joi.object({
gitlab_url: optionalUrl,
branch: Joi.string(),
}).required()

const documentation = `
<p>
Important: If your project is publicly visible, but the badge is like this:
Important: You must use the Project Path, not the Project Id. Additionally, if your project is publicly visible, but the badge is like this:
<img src="https://img.shields.io/badge/build-not&nbsp;found-red" alt="build not found"/>
</p>
<p>
Expand All @@ -39,26 +40,23 @@ class GitlabPipelineStatus extends BaseSvgScrapingService {
static category = 'build'

static route = {
base: 'gitlab/pipeline',
pattern: ':user/:repo/:branch+',
base: 'gitlab/pipeline-status',
pattern: ':project+',
queryParamSchema,
}

static examples = [
{
title: 'Gitlab pipeline status',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab',
branch: 'master',
},
namedParams: { project: 'gitlab-org/gitlab' },
queryParams: { branch: 'master' },
staticPreview: this.render({ status: 'passed' }),
documentation,
},
{
title: 'Gitlab pipeline status (self-hosted)',
namedParams: { user: 'GNOME', repo: 'pango', branch: 'master' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org' },
namedParams: { project: 'GNOME/pango' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org', branch: 'master' },
staticPreview: this.render({ status: 'passed' }),
documentation,
},
Expand All @@ -68,33 +66,67 @@ class GitlabPipelineStatus extends BaseSvgScrapingService {
return renderBuildStatusBadge({ status })
}

async handle(
{ user, repo, branch },
{ gitlab_url: baseUrl = 'https://gitlab.com' }
) {
const { message: status } = await this._requestSvg({
async fetch({ project, branch, baseUrl }) {
return this._requestSvg({
schema: badgeSchema,
url: `${baseUrl}/${user}/${repo}/badges/${branch}/pipeline.svg`,
url: `${baseUrl}/${decodeURIComponent(
project
)}/badges/${branch}/pipeline.svg`,
errorMessages: {
401: 'repo not found',
404: 'repo not found',
},
})
}

static transform(data) {
const { message: status } = data
if (status === 'unknown') {
throw new NotFound({ prettyMessage: 'branch not found' })
}
return { status }
}

async handle(
{ project },
{ gitlab_url: baseUrl = 'https://gitlab.com', branch = 'main' }
) {
const data = await this.fetch({
project,
branch,
baseUrl,
})
const { status } = this.constructor.transform(data)
return this.constructor.render({ status })
}
}

const GitlabPipelineStatusRedirector = redirector({
category: 'build',
name: 'GitlabPipelineStatusRedirector',
route: {
base: 'gitlab/pipeline',
pattern: ':user/:repo',
},
transformPath: ({ user, repo }) => `/gitlab/pipeline/${user}/${repo}/master`,
transformPath: ({ user, repo }) => `/gitlab/pipeline-status/${user}/${repo}`,
transformQueryParams: ({ _b }) => ({ branch: 'master' }),
dateAdded: new Date('2020-07-12'),
})

export { GitlabPipelineStatus, GitlabPipelineStatusRedirector }
const GitlabPipelineStatusBranchRouteParamRedirector = redirector({
category: 'build',
name: 'GitlabPipelineStatusBranchRouteParamRedirector',
route: {
base: 'gitlab/pipeline',
pattern: ':user/:repo/:branch+',
},
transformPath: ({ user, repo }) => `/gitlab/pipeline-status/${user}/${repo}`,
transformQueryParams: ({ branch }) => ({ branch }),
dateAdded: new Date('2021-10-20'),
})

export {
GitlabPipelineStatus,
GitlabPipelineStatusRedirector,
GitlabPipelineStatusBranchRouteParamRedirector,
}
37 changes: 27 additions & 10 deletions services/gitlab/gitlab-pipeline-status.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ import { ServiceTester } from '../tester.js'
export const t = new ServiceTester({
id: 'GitlabPipeline',
title: 'Gitlab Pipeline',
pathPrefix: '/gitlab/pipeline',
pathPrefix: '/gitlab',
})

t.create('Pipeline status').get('/gitlab-org/gitlab/v10.7.6.json').expectBadge({
label: 'build',
message: isBuildStatus,
})
t.create('Pipeline status')
.get('/pipeline-status/gitlab-org/gitlab.json?branch=v10.7.6')
.expectBadge({
label: 'build',
message: isBuildStatus,
})

t.create('Pipeline status (nested groups)')
.get(
'/pipeline-status/megabyte-labs/dockerfile/ci-pipeline/ansible-lint.json?branch=master'
)
.expectBadge({
label: 'build',
message: isBuildStatus,
})

t.create('Pipeline status (nonexistent branch)')
.get('/gitlab-org/gitlab/nope-not-a-branch.json')
.get('/pipeline-status/gitlab-org/gitlab.json?branch=nope-not-a-branch')
.expectBadge({
label: 'build',
message: 'branch not found',
Expand All @@ -26,19 +37,25 @@ t.create('Pipeline status (nonexistent branch)')
// error message, we will simply display inaccessible
// https://github.com/badges/shields/pull/5538
t.create('Pipeline status (nonexistent repo)')
.get('/this-repo/does-not-exist/master.json')
.get('/pipeline-status/this-repo/does-not-exist.json?branch=master')
.expectBadge({
label: 'build',
message: 'inaccessible',
})

t.create('Pipeline status (custom gitlab URL)')
.get('/GNOME/pango/main.json?gitlab_url=https://gitlab.gnome.org')
.get('/pipeline-status/GNOME/pango.json?gitlab_url=https://gitlab.gnome.org')
.expectBadge({
label: 'build',
message: isBuildStatus,
})

t.create('Pipeline no branch redirect')
.get('/gitlab-org/gitlab.svg')
.expectRedirect('/gitlab/pipeline/gitlab-org/gitlab/master.svg')
.get('/pipeline/gitlab-org/gitlab.svg')
.expectRedirect('/gitlab/pipeline-status/gitlab-org/gitlab.svg?branch=master')

t.create('Pipeline legacy route with branch redirect')
.get('/pipeline/gitlab-org/gitlab/v10.7.6?style=flat')
.expectRedirect(
'/gitlab/pipeline-status/gitlab-org/gitlab.svg?branch=v10.7.6&style=flat'
)

0 comments on commit a43c98a

Please sign in to comment.