Commit 33a63ea4 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '17555-custom-text-for-badges' into 'master'

Resolve "Support custom text for coverage badges"

See merge request gitlab-org/gitlab!29381
parents 62a63f65 f6a55bea
...@@ -8,14 +8,21 @@ class Projects::BadgesController < Projects::ApplicationController ...@@ -8,14 +8,21 @@ class Projects::BadgesController < Projects::ApplicationController
def pipeline def pipeline
pipeline_status = Gitlab::Badge::Pipeline::Status 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 render_badge pipeline_status
end end
def coverage def coverage
coverage_report = Gitlab::Badge::Coverage::Report 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 render_badge coverage_report
end end
......
---
title: Allow customization of badge key_text and key_width
merge_request: 29381
author: Fabian Schneider @fabsrc
type: added
...@@ -301,6 +301,16 @@ https://example.gitlab.com/<namespace>/<project>/badges/<branch>/coverage.svg?st ...@@ -301,6 +301,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) ![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
[Environment variables](../variables/README.md#gitlab-cicd-environment-variables) can be set in an environment to be available to a runner. [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 ...@@ -7,12 +7,16 @@ module Gitlab
# Test coverage report badge # Test coverage report badge
# #
class Report < Badge::Base 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 @project = project
@ref = ref @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) @pipeline = @project.ci_pipelines.latest_successful_for_ref(@ref)
end end
......
...@@ -20,10 +20,16 @@ module Gitlab ...@@ -20,10 +20,16 @@ module Gitlab
def initialize(badge) def initialize(badge)
@entity = badge.entity @entity = badge.entity
@status = badge.status @status = badge.status
@key_text = badge.customization.dig(:key_text)
@key_width = badge.customization.dig(:key_width)
end end
def key_text def key_text
@entity.to_s if @key_text && @key_text.size <= MAX_KEY_SIZE
@key_text
else
@entity.to_s
end
end end
def value_text def value_text
...@@ -31,7 +37,11 @@ module Gitlab ...@@ -31,7 +37,11 @@ module Gitlab
end end
def key_width def key_width
62 if @key_width && @key_width.between?(1, MAX_KEY_SIZE)
@key_width
else
62
end
end end
def value_width def value_width
......
...@@ -7,11 +7,15 @@ module Gitlab ...@@ -7,11 +7,15 @@ module Gitlab
# Pipeline status badge # Pipeline status badge
# #
class Status < Badge::Base class Status < Badge::Base
attr_reader :project, :ref attr_reader :project, :ref, :customization
def initialize(project, ref) def initialize(project, ref, opts: {})
@project = project @project = project
@ref = ref @ref = ref
@customization = {
key_width: opts[:key_width].to_i,
key_text: opts[:key_text]
}
@sha = @project.commit(@ref).try(:sha) @sha = @project.commit(@ref).try(:sha)
end end
......
...@@ -24,10 +24,16 @@ module Gitlab ...@@ -24,10 +24,16 @@ module Gitlab
def initialize(badge) def initialize(badge)
@entity = badge.entity @entity = badge.entity
@status = badge.status @status = badge.status
@key_text = badge.customization.dig(:key_text)
@key_width = badge.customization.dig(:key_width)
end end
def key_text def key_text
@entity.to_s if @key_text && @key_text.size <= MAX_KEY_SIZE
@key_text
else
@entity.to_s
end
end end
def value_text def value_text
...@@ -35,7 +41,11 @@ module Gitlab ...@@ -35,7 +41,11 @@ module Gitlab
end end
def key_width def key_width
62 if @key_width && @key_width.between?(1, MAX_KEY_SIZE)
@key_width
else
62
end
end end
def value_width def value_width
......
...@@ -6,6 +6,8 @@ module Gitlab ...@@ -6,6 +6,8 @@ module Gitlab
# Abstract template class for badges # Abstract template class for badges
# #
class Template class Template
MAX_KEY_SIZE = 128
def initialize(badge) def initialize(badge)
@entity = badge.entity @entity = badge.entity
@status = badge.status @status = badge.status
......
...@@ -54,7 +54,7 @@ describe Projects::BadgesController do ...@@ -54,7 +54,7 @@ describe Projects::BadgesController do
context 'when style param is set to `flat`' do context 'when style param is set to `flat`' do
it 'renders the `flat` badge layout' 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') expect(response).to render_template('projects/badges/badge')
end end
...@@ -62,7 +62,7 @@ describe Projects::BadgesController do ...@@ -62,7 +62,7 @@ describe Projects::BadgesController do
context 'when style param is set to an invalid type' do context 'when style param is set to an invalid type' do
it 'renders the `flat` (default) badge layout' 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') expect(response).to render_template('projects/badges/badge')
end end
...@@ -70,7 +70,7 @@ describe Projects::BadgesController do ...@@ -70,7 +70,7 @@ describe Projects::BadgesController do
context 'when style param is set to `flat-square`' do context 'when style param is set to `flat-square`' do
it 'renders the `flat-square` badge layout' 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') expect(response).to render_template('projects/badges/badge_flat-square')
end end
...@@ -102,12 +102,37 @@ describe Projects::BadgesController do ...@@ -102,12 +102,37 @@ describe Projects::BadgesController do
end end
it 'defaults to project permissions' do it 'defaults to project permissions' do
get_badge(:coverage) get_badge(badge_type)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
end 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 end
describe '#pipeline' do describe '#pipeline' do
...@@ -118,13 +143,12 @@ describe Projects::BadgesController do ...@@ -118,13 +143,12 @@ describe Projects::BadgesController do
it_behaves_like 'a badge resource', :coverage it_behaves_like 'a badge resource', :coverage
end end
def get_badge(badge, style = nil) def get_badge(badge, args = {})
params = { params = {
namespace_id: project.namespace.to_param, namespace_id: project.namespace.to_param,
project_id: project, project_id: project,
ref: pipeline.ref, ref: pipeline.ref
style: style }.merge(args.slice(:style, :key_text, :key_width))
}
get badge, params: params, format: :svg get badge, params: params, format: :svg
end end
......
...@@ -7,7 +7,7 @@ describe Gitlab::Badge::Coverage::Report do ...@@ -7,7 +7,7 @@ describe Gitlab::Badge::Coverage::Report do
let(:job_name) { nil } let(:job_name) { nil }
let(:badge) do let(:badge) do
described_class.new(project, 'master', job_name) described_class.new(project, 'master', opts: { job: job_name })
end end
describe '#entity' do describe '#entity' do
......
...@@ -3,13 +3,33 @@ ...@@ -3,13 +3,33 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Badge::Coverage::Template do 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) } let(:template) { described_class.new(badge) }
describe '#key_text' do describe '#key_text' do
it 'is always says coverage' do it 'says coverage by default' do
expect(template.key_text).to eq 'coverage' expect(template.key_text).to eq 'coverage'
end 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 end
describe '#value_text' do describe '#value_text' do
...@@ -41,9 +61,29 @@ describe Gitlab::Badge::Coverage::Template do ...@@ -41,9 +61,29 @@ describe Gitlab::Badge::Coverage::Template do
end end
describe '#key_width' do describe '#key_width' do
it 'has a fixed key width' do it 'is fixed by default' do
expect(template.key_width).to eq 62 expect(template.key_width).to eq 62
end 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 end
describe '#value_width' do describe '#value_width' do
......
...@@ -3,13 +3,33 @@ ...@@ -3,13 +3,33 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Badge::Pipeline::Template do 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) } let(:template) { described_class.new(badge) }
describe '#key_text' do describe '#key_text' do
it 'is always says pipeline' do it 'says pipeline by default' do
expect(template.key_text).to eq 'pipeline' expect(template.key_text).to eq 'pipeline'
end 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 end
describe '#value_text' do describe '#value_text' do
...@@ -18,6 +38,32 @@ describe Gitlab::Badge::Pipeline::Template do ...@@ -18,6 +38,32 @@ describe Gitlab::Badge::Pipeline::Template do
end end
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 describe 'widths and text anchors' do
it 'has fixed width and text anchors' do it 'has fixed width and text anchors' do
expect(template.width).to eq 116 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