Commit 9ae1ecf8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract build status badge metadata to separate class

parent 42c035ee
...@@ -3,7 +3,7 @@ class Projects::PipelinesSettingsController < Projects::ApplicationController ...@@ -3,7 +3,7 @@ class Projects::PipelinesSettingsController < Projects::ApplicationController
def show def show
@ref = params[:ref] || @project.default_branch || 'master' @ref = params[:ref] || @project.default_branch || 'master'
@build_badge = Gitlab::Badge::Build.new(@project, @ref) @build_badge = Gitlab::Badge::Build.new(@project, @ref).metadata
end end
def update def update
......
...@@ -4,15 +4,15 @@ module Gitlab ...@@ -4,15 +4,15 @@ module Gitlab
# Build badge # Build badge
# #
class Build class Build
include Gitlab::Application.routes.url_helpers
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper
def initialize(project, ref) def initialize(project, ref)
@project, @ref = project, ref @project, @ref = project, ref
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref) @image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
end end
def metadata
Build::Metadata.new(@project, @ref)
end
def type def type
'image/svg+xml' 'image/svg+xml'
end end
...@@ -24,23 +24,6 @@ module Gitlab ...@@ -24,23 +24,6 @@ module Gitlab
def to_s def to_s
@image[:name].sub(/\.svg$/, '') @image[:name].sub(/\.svg$/, '')
end end
def to_html
link_to(image_tag(image_url, alt: 'build status'), link_url)
end
def to_markdown
"[![build status](#{image_url})](#{link_url})"
end
def image_url
build_namespace_project_badges_url(@project.namespace,
@project, @ref, format: :svg)
end
def link_url
namespace_project_commits_url(@project.namespace, @project, id: @ref)
end
end end
end end
end end
module Gitlab
module Badge
class Build
##
# Class that describes build badge metadata
#
class Metadata
include Gitlab::Application.routes.url_helpers
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper
def initialize(project, ref)
@project = project
@ref = ref
end
def to_html
link_to(image_tag(image_url, alt: 'build status'), link_url)
end
def to_markdown
"[![build status](#{image_url})](#{link_url})"
end
def image_url
build_namespace_project_badges_url(@project.namespace,
@project, @ref, format: :svg)
end
def link_url
namespace_project_commits_url(@project.namespace, @project, id: @ref)
end
end
end
end
end
require 'spec_helper'
describe Gitlab::Badge::Build::Metadata do
let(:project) { create(:project) }
let(:branch) { 'master' }
let(:badge) { described_class.new(project, branch) }
describe '#to_html' do
let(:html) { Nokogiri::HTML.parse(badge.to_html) }
let(:a_href) { html.at('a') }
it 'points to link' do
expect(a_href[:href]).to eq badge.link_url
end
it 'contains clickable image' do
expect(a_href.children.first.name).to eq 'img'
end
end
describe '#to_markdown' do
subject { badge.to_markdown }
it { is_expected.to include badge.image_url }
it { is_expected.to include badge.link_url }
end
describe '#image_url' do
subject { badge.image_url }
it { is_expected.to include "badges/#{branch}/build.svg" }
end
describe '#link_url' do
subject { badge.link_url }
it { is_expected.to include "commits/#{branch}" }
end
end
...@@ -11,36 +11,13 @@ describe Gitlab::Badge::Build do ...@@ -11,36 +11,13 @@ describe Gitlab::Badge::Build do
it { is_expected.to eq 'image/svg+xml' } it { is_expected.to eq 'image/svg+xml' }
end end
describe '#to_html' do describe '#metadata' do
let(:html) { Nokogiri::HTML.parse(badge.to_html) } it 'returns badge metadata' do
let(:a_href) { html.at('a') } expect(badge.metadata.image_url)
.to include 'badges/master/build.svg'
it 'points to link' do
expect(a_href[:href]).to eq badge.link_url
end
it 'contains clickable image' do
expect(a_href.children.first.name).to eq 'img'
end end
end end
describe '#to_markdown' do
subject { badge.to_markdown }
it { is_expected.to include badge.image_url }
it { is_expected.to include badge.link_url }
end
describe '#image_url' do
subject { badge.image_url }
it { is_expected.to include "badges/#{branch}/build.svg" }
end
describe '#link_url' do
subject { badge.link_url }
it { is_expected.to include "commits/#{branch}" }
end
context 'build exists' do context 'build exists' do
let!(:build) { create_build(project, sha, branch) } let!(:build) { create_build(project, sha, branch) }
......
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