Skip to content

Commit 9ae1ecf

Browse files
committed
Extract build status badge metadata to separate class
1 parent 42c035e commit 9ae1ecf

File tree

5 files changed

+82
-49
lines changed

5 files changed

+82
-49
lines changed

app/controllers/projects/pipelines_settings_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Projects::PipelinesSettingsController < Projects::ApplicationController
33

44
def show
55
@ref = params[:ref] || @project.default_branch || 'master'
6-
@build_badge = Gitlab::Badge::Build.new(@project, @ref)
6+
@build_badge = Gitlab::Badge::Build.new(@project, @ref).metadata
77
end
88

99
def update

lib/gitlab/badge/build.rb

+4-21
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ module Badge
44
# Build badge
55
#
66
class Build
7-
include Gitlab::Application.routes.url_helpers
8-
include ActionView::Helpers::AssetTagHelper
9-
include ActionView::Helpers::UrlHelper
10-
117
def initialize(project, ref)
128
@project, @ref = project, ref
139
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
1410
end
1511

12+
def metadata
13+
Build::Metadata.new(@project, @ref)
14+
end
15+
1616
def type
1717
'image/svg+xml'
1818
end
@@ -24,23 +24,6 @@ def data
2424
def to_s
2525
@image[:name].sub(/\.svg$/, '')
2626
end
27-
28-
def to_html
29-
link_to(image_tag(image_url, alt: 'build status'), link_url)
30-
end
31-
32-
def to_markdown
33-
"[![build status](#{image_url})](#{link_url})"
34-
end
35-
36-
def image_url
37-
build_namespace_project_badges_url(@project.namespace,
38-
@project, @ref, format: :svg)
39-
end
40-
41-
def link_url
42-
namespace_project_commits_url(@project.namespace, @project, id: @ref)
43-
end
4427
end
4528
end
4629
end

lib/gitlab/badge/build/metadata.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Gitlab
2+
module Badge
3+
class Build
4+
##
5+
# Class that describes build badge metadata
6+
#
7+
class Metadata
8+
include Gitlab::Application.routes.url_helpers
9+
include ActionView::Helpers::AssetTagHelper
10+
include ActionView::Helpers::UrlHelper
11+
12+
def initialize(project, ref)
13+
@project = project
14+
@ref = ref
15+
end
16+
17+
def to_html
18+
link_to(image_tag(image_url, alt: 'build status'), link_url)
19+
end
20+
21+
def to_markdown
22+
"[![build status](#{image_url})](#{link_url})"
23+
end
24+
25+
def image_url
26+
build_namespace_project_badges_url(@project.namespace,
27+
@project, @ref, format: :svg)
28+
end
29+
30+
def link_url
31+
namespace_project_commits_url(@project.namespace, @project, id: @ref)
32+
end
33+
end
34+
end
35+
end
36+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'spec_helper'
2+
3+
describe Gitlab::Badge::Build::Metadata do
4+
let(:project) { create(:project) }
5+
let(:branch) { 'master' }
6+
let(:badge) { described_class.new(project, branch) }
7+
8+
describe '#to_html' do
9+
let(:html) { Nokogiri::HTML.parse(badge.to_html) }
10+
let(:a_href) { html.at('a') }
11+
12+
it 'points to link' do
13+
expect(a_href[:href]).to eq badge.link_url
14+
end
15+
16+
it 'contains clickable image' do
17+
expect(a_href.children.first.name).to eq 'img'
18+
end
19+
end
20+
21+
describe '#to_markdown' do
22+
subject { badge.to_markdown }
23+
24+
it { is_expected.to include badge.image_url }
25+
it { is_expected.to include badge.link_url }
26+
end
27+
28+
describe '#image_url' do
29+
subject { badge.image_url }
30+
it { is_expected.to include "badges/#{branch}/build.svg" }
31+
end
32+
33+
describe '#link_url' do
34+
subject { badge.link_url }
35+
it { is_expected.to include "commits/#{branch}" }
36+
end
37+
end

spec/lib/gitlab/badge/build_spec.rb

+4-27
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,13 @@
1111
it { is_expected.to eq 'image/svg+xml' }
1212
end
1313

14-
describe '#to_html' do
15-
let(:html) { Nokogiri::HTML.parse(badge.to_html) }
16-
let(:a_href) { html.at('a') }
17-
18-
it 'points to link' do
19-
expect(a_href[:href]).to eq badge.link_url
20-
end
21-
22-
it 'contains clickable image' do
23-
expect(a_href.children.first.name).to eq 'img'
14+
describe '#metadata' do
15+
it 'returns badge metadata' do
16+
expect(badge.metadata.image_url)
17+
.to include 'badges/master/build.svg'
2418
end
2519
end
2620

27-
describe '#to_markdown' do
28-
subject { badge.to_markdown }
29-
30-
it { is_expected.to include badge.image_url }
31-
it { is_expected.to include badge.link_url }
32-
end
33-
34-
describe '#image_url' do
35-
subject { badge.image_url }
36-
it { is_expected.to include "badges/#{branch}/build.svg" }
37-
end
38-
39-
describe '#link_url' do
40-
subject { badge.link_url }
41-
it { is_expected.to include "commits/#{branch}" }
42-
end
43-
4421
context 'build exists' do
4522
let!(:build) { create_build(project, sha, branch) }
4623

0 commit comments

Comments
 (0)