Commit 0705c6eb authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '15304-skipped-pipeline-status-badge' into 'master'

Add ignore_skipped option for pipeline status badge

Closes #15304

See merge request gitlab-org/gitlab!28288
parents 8760601b f09eeae2
......@@ -9,6 +9,7 @@ class Projects::BadgesController < Projects::ApplicationController
def pipeline
pipeline_status = Gitlab::Badge::Pipeline::Status
.new(project, params[:ref], opts: {
ignore_skipped: params[:ignore_skipped],
key_text: params[:key_text],
key_width: params[:key_width]
})
......
---
title: Add ignore_skipped option for pipeline status badge
merge_request: 28288
author: Fabian Schneider @fabsrc
type: added
......@@ -266,6 +266,14 @@ You can access a pipeline status badge image using the following link:
https://gitlab.example.com/<namespace>/<project>/badges/<branch>/pipeline.svg
```
#### Display only non-skipped status
If you want the pipeline status badge to only display the last non-skipped status, you can use the `?ignore_skipped=true` query parameter:
```plaintext
https://example.gitlab.com/<namespace>/<project>/badges/<branch>/pipeline.svg?ignore_skipped=true
```
### Test coverage report badge
GitLab makes it possible to define the regular expression for [coverage report](#test-coverage-parsing),
......
......@@ -12,6 +12,7 @@ module Gitlab
def initialize(project, ref, opts: {})
@project = project
@ref = ref
@ignore_skipped = Gitlab::Utils.to_boolean(opts[:ignore_skipped], default: false)
@customization = {
key_width: opts[:key_width].to_i,
key_text: opts[:key_text]
......@@ -26,9 +27,11 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def status
@project.ci_pipelines
pipelines = @project.ci_pipelines
.where(sha: @sha)
.latest_status(@ref) || 'unknown'
relation = @ignore_skipped ? pipelines.without_statuses([:skipped]) : pipelines
relation.latest_status(@ref) || 'unknown'
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -3,9 +3,9 @@
require 'spec_helper'
RSpec.describe Projects::BadgesController do
let(:project) { pipeline.project }
let!(:pipeline) { create(:ci_empty_pipeline) }
let(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :repository) }
let_it_be(:pipeline, reload: true) { create(:ci_empty_pipeline, project: project) }
let_it_be(:user) { create(:user) }
shared_examples 'a badge resource' do |badge_type|
context 'when pipelines are public' do
......@@ -137,6 +137,26 @@ RSpec.describe Projects::BadgesController do
describe '#pipeline' do
it_behaves_like 'a badge resource', :pipeline
context 'with ignore_skipped param' do
render_views
before do
pipeline.update!(status: :skipped)
project.add_maintainer(user)
sign_in(user)
end
it 'returns skipped badge if set to false' do
get_badge(:pipeline, ignore_skipped: false)
expect(response.body).to include('skipped')
end
it 'does not return skipped badge if set to true' do
get_badge(:pipeline, ignore_skipped: true)
expect(response.body).to include('unknown')
end
end
end
describe '#coverage' do
......@@ -148,7 +168,7 @@ RSpec.describe Projects::BadgesController do
namespace_id: project.namespace.to_param,
project_id: project,
ref: pipeline.ref
}.merge(args.slice(:style, :key_text, :key_width))
}.merge(args.slice(:style, :key_text, :key_width, :ignore_skipped))
get badge, params: params, format: :svg
end
......
......@@ -78,6 +78,34 @@ RSpec.describe Gitlab::Badge::Pipeline::Status do
expect(badge.status).to eq 'success'
end
end
context 'when ignored_skipped is set to true' do
let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: true }) }
before do
pipeline.skip!
end
describe '#status' do
it 'uses latest non-skipped status' do
expect(new_badge.status).not_to eq 'skipped'
end
end
end
context 'when ignored_skipped is set to false' do
let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: false }) }
before do
pipeline.skip!
end
describe '#status' do
it 'uses latest status' do
expect(new_badge.status).to eq 'skipped'
end
end
end
end
context 'build does not exist' do
......
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