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

[GitLabCoverage] subgroup bug #8401

Merged
merged 16 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 15 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
13 changes: 13 additions & 0 deletions services/gitlab/gitlab-coverage-redirect.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { redirector } from '../index.js'

export default redirector({
category: 'coverage',
route: {
base: 'gitlab/coverage',
pattern: ':user/:repo/:branch',
},
transformPath: ({ user, repo }) =>
`/gitlab/pipeline-coverage/${user}/${repo}`,
transformQueryParams: ({ branch }) => ({ branch }),
dateAdded: new Date('2022-09-25'),
})
22 changes: 22 additions & 0 deletions services/gitlab/gitlab-coverage-redirect.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('Coverage redirect (with branch)')
.get('/gitlab-org/gitlab-runner/master.json')
.expectRedirect(
'/gitlab/pipeline-coverage/gitlab-org/gitlab-runner.json?branch=master'
)

t.create('Coverage redirect (with branch and job_name)')
.get('/gitlab-org/gitlab-runner/master.json?job_name=test coverage report')
.expectRedirect(
'/gitlab/pipeline-coverage/gitlab-org/gitlab-runner.json?branch=master&job_name=test%20coverage%20report'
)

t.create('Coverage redirect (with branch and gitlab_url)')
.get(
'/gitlab-org/gitlab-runner/master.json?gitlab_url=https://gitlab.gnome.org'
)
.expectRedirect(
'/gitlab/pipeline-coverage/gitlab-org/gitlab-runner.json?branch=master&gitlab_url=https%3a%2f%2fgitlab.gnome.org'
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const schema = Joi.object({
const queryParamSchema = Joi.object({
gitlab_url: optionalUrl,
job_name: Joi.string(),
branch: Joi.string(),
}).required()

const moreDocs = `
Expand All @@ -37,50 +38,52 @@ Also make sure you have set up code covrage parsing as described <a href="https:
</p>
`

export default class GitlabCoverage extends BaseSvgScrapingService {
export default class GitlabPipelineCoverage extends BaseSvgScrapingService {
static category = 'coverage'

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

static examples = [
{
title: 'Gitlab code coverage',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab-runner',
branch: 'master',
},
namedParams: { project: 'gitlab-org/gitlab-runner' },
queryParams: { branch: 'master' },
staticPreview: this.render({ coverage: 67 }),
documentation: documentation + moreDocs,
},
{
title:
'Gitlab code coverage (with a subgroup in the name of the project)',
namedParams: { project: 'megabyte-labs/go/cli/bodega' },
Copy link
Member

Choose a reason for hiding this comment

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

Nice. One more minor thing. We can drop this example. For all the other GitLab badges (pipeline status, issues, merge requests, releases, etc) they work for repos in nested subgroups but we don't explicitly provide an example in the frontend. In general we try and keep the number of example of different variations in the examples to a minimum if possible. Cheers

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, example removed ;)

queryParams: { branch: 'master' },
staticPreview: this.render({ coverage: 67 }),
documentation: documentation + moreDocs,
},
{
title: 'Gitlab code coverage (specific job)',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab-runner',
branch: 'master',
},
queryParams: { job_name: 'test coverage report' },
namedParams: { project: 'gitlab-org/gitlab-runner' },
queryParams: { job_name: 'test coverage report', branch: 'master' },
staticPreview: this.render({ coverage: 96 }),
documentation: documentation + moreDocs,
},
{
title: 'Gitlab code coverage (self-managed)',
namedParams: { user: 'GNOME', repo: 'at-spi2-core', branch: 'master' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org' },
namedParams: { project: 'GNOME/at-spi2-core' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org', branch: 'master' },
staticPreview: this.render({ coverage: 93 }),
documentation: documentation + moreDocs,
},
{
title: 'Gitlab code coverage (self-managed, specific job)',
namedParams: { user: 'GNOME', repo: 'libhandy', branch: 'master' },
namedParams: { project: 'GNOME/libhandy' },
queryParams: {
gitlab_url: 'https://gitlab.gnome.org',
job_name: 'unit-test',
branch: 'master',
},
staticPreview: this.render({ coverage: 93 }),
documentation: documentation + moreDocs,
Expand All @@ -96,11 +99,13 @@ export default class GitlabCoverage extends BaseSvgScrapingService {
}
}

async fetch({ user, repo, branch, baseUrl = 'https://gitlab.com', jobName }) {
async fetch({ project, baseUrl = 'https://gitlab.com', jobName, branch }) {
// Since the URL doesn't return a usable value when an invalid job name is specified,
// it is recommended to not use the query param at all if not required
jobName = jobName ? `?job=${jobName}` : ''
const url = `${baseUrl}/${user}/${repo}/badges/${branch}/coverage.svg${jobName}`
const url = `${baseUrl}/${decodeURIComponent(
project
)}/badges/${branch}/coverage.svg${jobName}`
const errorMessages = errorMessagesFor('project not found')
return this._requestSvg({
schema,
Expand All @@ -117,12 +122,11 @@ export default class GitlabCoverage extends BaseSvgScrapingService {
}

async handle(
{ user, repo, branch },
{ gitlab_url: baseUrl, job_name: jobName }
{ project },
{ gitlab_url: baseUrl, job_name: jobName, branch }
) {
const { message: coverage } = await this.fetch({
user,
repo,
project,
branch,
baseUrl,
jobName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,63 @@ import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('Coverage (branch)')
.get('/gitlab-org/gitlab-runner/12-0-stable.json')
.get('/gitlab-org/gitlab-runner.json?branch=12-0-stable')
.expectBadge({
label: 'coverage',
message: isIntegerPercentage,
})

t.create('Coverage (existent branch but coverage not set up)')
.get('/gitlab-org/gitlab-git-http-server/master.json')
.get('/gitlab-org/gitlab-git-http-server.json?branch=master')
.expectBadge({
label: 'coverage',
message: 'not set up',
})

t.create('Coverage (nonexistent branch)')
.get('/gitlab-org/gitlab-runner/nope-not-a-branch.json')
.get('/gitlab-org/gitlab-runner.json?branch=nope-not-a-branch')
.expectBadge({
label: 'coverage',
message: 'not set up',
})

t.create('Coverage (nonexistent repo)')
.get('/this-repo/does-not-exist/neither-branch.json')
.get('/this-repo/does-not-exist.json')
.expectBadge({
label: 'coverage',
message: 'inaccessible',
})

t.create('Coverage (custom job)')
.get(
'/gitlab-org/gitlab-runner/12-0-stable.json?job_name=test coverage report'
'/gitlab-org/gitlab-runner.json?branch=12-0-stable&job_name=test coverage report'
)
.expectBadge({
label: 'coverage',
message: isIntegerPercentage,
})

t.create('Coverage (custom invalid job)')
.get('/gitlab-org/gitlab-runner/12-0-stable.json?job_name=i dont exist')
.get(
'/gitlab-org/gitlab-runner.json?branch=12-0-stable&job_name=i dont exist'
)
.expectBadge({
label: 'coverage',
message: 'not set up',
})

t.create('Coverage (custom gitlab URL)')
.get('/GNOME/at-spi2-core/master.json?gitlab_url=https://gitlab.gnome.org')
.get(
'/GNOME/at-spi2-core.json?gitlab_url=https://gitlab.gnome.org&branch=master'
)
.expectBadge({
label: 'coverage',
message: isIntegerPercentage,
})

t.create('Coverage (custom gitlab URL and job)')
.get(
'/GNOME/libhandy/master.json?gitlab_url=https://gitlab.gnome.org&job_name=unit-test'
'/GNOME/libhandy.json?gitlab_url=https://gitlab.gnome.org&branch=master&job_name=unit-test'
)
.expectBadge({
label: 'coverage',
Expand Down