Commit f6a55bea authored by Fabian Schneider's avatar Fabian Schneider Committed by Grzegorz Bizon

Resolve "Support custom text for coverage badges"

parent d00e8e24
......@@ -8,14 +8,21 @@ class Projects::BadgesController < Projects::ApplicationController
def pipeline
pipeline_status = Gitlab::Badge::Pipeline::Status
.new(project, params[:ref])
.new(project, params[:ref], opts: {
key_text: params[:key_text],
key_width: params[:key_width]
})
render_badge pipeline_status
end
def coverage
coverage_report = Gitlab::Badge::Coverage::Report
.new(project, params[:ref], params[:job])
.new(project, params[:ref], opts: {
job: params[:job],
key_text: params[:key_text],
key_width: params[:key_width]
})
render_badge coverage_report
end
......
---
title: Allow customization of badge key_text and key_width
merge_request: 29381
author: Fabian Schneider @fabsrc
type: added
......@@ -298,6 +298,16 @@ https://example.gitlab.com/<namespace>/<project>/badges/<branch>/coverage.svg?st
![Badge flat square style](https://gitlab.com/gitlab-org/gitlab-foss/badges/master/coverage.svg?job=coverage&style=flat-square)
### Custom badge text
The text for a badge can be customized. This can be useful to differentiate between multiple coverage jobs that run in the same pipeline. Customize the badge text and width by adding the `key_text=custom_text` and `key_width=custom_key_width` parameters to the URL:
```text
https://gitlab.com/gitlab-org/gitlab-foss/badges/master/coverage.svg?job=karma&key_text=Frontend+Coverage&key_width=100
```
![Badge with custom text and width](https://gitlab.com/gitlab-org/gitlab-foss/badges/master/coverage.svg?job=karma&key_text=Frontend+Coverage&key_width=100)
## Environment Variables
[Environment variables](../variables/README.md#gitlab-cicd-environment-variables) can be set in an environment to be available to a runner.
......
......@@ -7,12 +7,16 @@ module Gitlab
# Test coverage report badge
#
class Report < Badge::Base
attr_reader :project, :ref, :job
attr_reader :project, :ref, :job, :customization
def initialize(project, ref, job = nil)
def initialize(project, ref, opts: { job: nil })
@project = project
@ref = ref
@job = job
@job = opts[:job]
@customization = {
key_width: opts[:key_width].to_i,
key_text: opts[:key_text]
}
@pipeline = @project.ci_pipelines.latest_successful_for_ref(@ref)
end
......
......@@ -20,19 +20,29 @@ module Gitlab
def initialize(badge)
@entity = badge.entity
@status = badge.status
@key_text = badge.customization.dig(:key_text)
@key_width = badge.customization.dig(:key_width)
end
def key_text
if @key_text && @key_text.size <= MAX_KEY_SIZE
@key_text
else
@entity.to_s
end
end
def value_text
@status ? ("%.2f%%" % @status) : 'unknown'
end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_SIZE)
@key_width
else
62
end
end
def value_width
@status ? 54 : 58
......
......@@ -7,11 +7,15 @@ module Gitlab
# Pipeline status badge
#
class Status < Badge::Base
attr_reader :project, :ref
attr_reader :project, :ref, :customization
def initialize(project, ref)
def initialize(project, ref, opts: {})
@project = project
@ref = ref
@customization = {
key_width: opts[:key_width].to_i,
key_text: opts[:key_text]
}
@sha = @project.commit(@ref).try(:sha)
end
......
......@@ -24,19 +24,29 @@ module Gitlab
def initialize(badge)
@entity = badge.entity
@status = badge.status
@key_text = badge.customization.dig(:key_text)
@key_width = badge.customization.dig(:key_width)
end
def key_text
if @key_text && @key_text.size <= MAX_KEY_SIZE
@key_text
else
@entity.to_s
end
end
def value_text
STATUS_RENAME[@status.to_s] || @status.to_s
end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_SIZE)
@key_width
else
62
end
end
def value_width
54
......
......@@ -6,6 +6,8 @@ module Gitlab
# Abstract template class for badges
#
class Template
MAX_KEY_SIZE = 128
def initialize(badge)
@entity = badge.entity
@status = badge.status
......
......@@ -54,7 +54,7 @@ describe Projects::BadgesController do
context 'when style param is set to `flat`' do
it 'renders the `flat` badge layout' do
get_badge(badge_type, 'flat')
get_badge(badge_type, style: 'flat')
expect(response).to render_template('projects/badges/badge')
end
......@@ -62,7 +62,7 @@ describe Projects::BadgesController do
context 'when style param is set to an invalid type' do
it 'renders the `flat` (default) badge layout' do
get_badge(badge_type, 'xxx')
get_badge(badge_type, style: 'xxx')
expect(response).to render_template('projects/badges/badge')
end
......@@ -70,7 +70,7 @@ describe Projects::BadgesController do
context 'when style param is set to `flat-square`' do
it 'renders the `flat-square` badge layout' do
get_badge(badge_type, 'flat-square')
get_badge(badge_type, style: 'flat-square')
expect(response).to render_template('projects/badges/badge_flat-square')
end
......@@ -102,12 +102,37 @@ describe Projects::BadgesController do
end
it 'defaults to project permissions' do
get_badge(:coverage)
get_badge(badge_type)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'customization' do
render_views
before do
project.add_maintainer(user)
sign_in(user)
end
context 'when key_text param is used' do
it 'sets custom key text' do
get_badge(badge_type, key_text: 'custom key text')
expect(response.body).to include('custom key text')
end
end
context 'when key_width param is used' do
it 'sets custom key width' do
get_badge(badge_type, key_width: '123')
expect(response.body).to include('123')
end
end
end
end
describe '#pipeline' do
......@@ -118,13 +143,12 @@ describe Projects::BadgesController do
it_behaves_like 'a badge resource', :coverage
end
def get_badge(badge, style = nil)
def get_badge(badge, args = {})
params = {
namespace_id: project.namespace.to_param,
project_id: project,
ref: pipeline.ref,
style: style
}
ref: pipeline.ref
}.merge(args.slice(:style, :key_text, :key_width))
get badge, params: params, format: :svg
end
......
......@@ -7,7 +7,7 @@ describe Gitlab::Badge::Coverage::Report do
let(:job_name) { nil }
let(:badge) do
described_class.new(project, 'master', job_name)
described_class.new(project, 'master', opts: { job: job_name })
end
describe '#entity' do
......
......@@ -3,13 +3,33 @@
require 'spec_helper'
describe Gitlab::Badge::Coverage::Template do
let(:badge) { double(entity: 'coverage', status: 90.00) }
let(:badge) { double(entity: 'coverage', status: 90.00, customization: {}) }
let(:template) { described_class.new(badge) }
describe '#key_text' do
it 'is always says coverage' do
it 'says coverage by default' do
expect(template.key_text).to eq 'coverage'
end
context 'when custom key_text is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_text: "custom text" })
end
it 'returns custom value' do
expect(template.key_text).to eq "custom text"
end
context 'when its size is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 't' * 129 })
end
it 'returns default value' do
expect(template.key_text).to eq 'coverage'
end
end
end
end
describe '#value_text' do
......@@ -41,9 +61,29 @@ describe Gitlab::Badge::Coverage::Template do
end
describe '#key_width' do
it 'has a fixed key width' do
it 'is fixed by default' do
expect(template.key_width).to eq 62
end
context 'when custom key_width is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 101 })
end
it 'returns custom value' do
expect(template.key_width).to eq 101
end
context 'when it is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 129 })
end
it 'returns default value' do
expect(template.key_width).to eq 62
end
end
end
end
describe '#value_width' do
......
......@@ -3,13 +3,33 @@
require 'spec_helper'
describe Gitlab::Badge::Pipeline::Template do
let(:badge) { double(entity: 'pipeline', status: 'success') }
let(:badge) { double(entity: 'pipeline', status: 'success', customization: {}) }
let(:template) { described_class.new(badge) }
describe '#key_text' do
it 'is always says pipeline' do
it 'says pipeline by default' do
expect(template.key_text).to eq 'pipeline'
end
context 'when custom key_text is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 'custom text' })
end
it 'returns custom value' do
expect(template.key_text).to eq 'custom text'
end
context 'when its size is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 't' * 129 })
end
it 'returns default value' do
expect(template.key_text).to eq 'pipeline'
end
end
end
end
describe '#value_text' do
......@@ -18,6 +38,32 @@ describe Gitlab::Badge::Pipeline::Template do
end
end
describe '#key_width' do
it 'is fixed by default' do
expect(template.key_width).to eq 62
end
context 'when custom key_width is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 101 })
end
it 'returns custom value' do
expect(template.key_width).to eq 101
end
context 'when it is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 129 })
end
it 'returns default value' do
expect(template.key_width).to eq 62
end
end
end
end
describe 'widths and text anchors' do
it 'has fixed width and text anchors' do
expect(template.width).to eq 116
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment