Refactor builds badge, encapsulate inside a class
This commit is contained in:
parent
63c8a05bf7
commit
4cd1b9f4d8
|
@ -2,11 +2,12 @@ class Projects::BadgesController < Projects::ApplicationController
|
|||
before_action :no_cache_headers
|
||||
|
||||
def build
|
||||
badge = Gitlab::Badge::Build.new(project, params[:ref])
|
||||
|
||||
respond_to do |format|
|
||||
format.html { render_404 }
|
||||
format.svg do
|
||||
image = Ci::ImageForBuildService.new.execute(project, ref: params[:ref])
|
||||
send_file(image.path, filename: image.name, disposition: 'inline', type: 'image/svg+xml')
|
||||
send_data(badge.data, type: badge.type, disposition: 'inline')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
module Gitlab
|
||||
module Badge
|
||||
##
|
||||
# Build badge
|
||||
#
|
||||
class Build
|
||||
def initialize(project, ref)
|
||||
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
|
||||
end
|
||||
|
||||
def to_s
|
||||
@image[:name].sub(/\.svg$/, '')
|
||||
end
|
||||
|
||||
def type
|
||||
'image/svg+xml'
|
||||
end
|
||||
|
||||
def data
|
||||
File.read(@image[:path])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Gitlab::Badge::Build do
|
||||
let(:project) { create(:project) }
|
||||
let(:sha) { project.commit.sha }
|
||||
let(:ci_commit) { create(:ci_commit, project: project, sha: sha) }
|
||||
let(:badge) { described_class.new(project, 'master') }
|
||||
|
||||
let!(:build) { create(:ci_build, commit: ci_commit) }
|
||||
|
||||
describe '#type' do
|
||||
subject { badge.type }
|
||||
it { is_expected.to eq 'image/svg+xml' }
|
||||
end
|
||||
|
||||
context 'build success' do
|
||||
before { build.success! }
|
||||
|
||||
describe '#to_s' do
|
||||
subject { badge.to_s }
|
||||
it { is_expected.to eq 'build-success' }
|
||||
end
|
||||
|
||||
describe '#data' do
|
||||
let(:data) { badge.data }
|
||||
let(:xml) { Nokogiri::XML.parse(data) }
|
||||
|
||||
it 'contains infromation about success' do
|
||||
expect(xml.at(%Q{text:contains("success")})).to be_truthy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue