Skip to content

Commit 4cd1b9f

Browse files
committed
Refactor builds badge, encapsulate inside a class
1 parent 63c8a05 commit 4cd1b9f

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

app/controllers/projects/badges_controller.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ class Projects::BadgesController < Projects::ApplicationController
22
before_action :no_cache_headers
33

44
def build
5+
badge = Gitlab::Badge::Build.new(project, params[:ref])
6+
57
respond_to do |format|
68
format.html { render_404 }
79
format.svg do
8-
image = Ci::ImageForBuildService.new.execute(project, ref: params[:ref])
9-
send_file(image.path, filename: image.name, disposition: 'inline', type: 'image/svg+xml')
10+
send_data(badge.data, type: badge.type, disposition: 'inline')
1011
end
1112
end
1213
end

lib/gitlab/badge/build.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Gitlab
2+
module Badge
3+
##
4+
# Build badge
5+
#
6+
class Build
7+
def initialize(project, ref)
8+
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
9+
end
10+
11+
def to_s
12+
@image[:name].sub(/\.svg$/, '')
13+
end
14+
15+
def type
16+
'image/svg+xml'
17+
end
18+
19+
def data
20+
File.read(@image[:path])
21+
end
22+
end
23+
end
24+
end

spec/lib/gitlab/badge/build_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
describe Gitlab::Badge::Build do
4+
let(:project) { create(:project) }
5+
let(:sha) { project.commit.sha }
6+
let(:ci_commit) { create(:ci_commit, project: project, sha: sha) }
7+
let(:badge) { described_class.new(project, 'master') }
8+
9+
let!(:build) { create(:ci_build, commit: ci_commit) }
10+
11+
describe '#type' do
12+
subject { badge.type }
13+
it { is_expected.to eq 'image/svg+xml' }
14+
end
15+
16+
context 'build success' do
17+
before { build.success! }
18+
19+
describe '#to_s' do
20+
subject { badge.to_s }
21+
it { is_expected.to eq 'build-success' }
22+
end
23+
24+
describe '#data' do
25+
let(:data) { badge.data }
26+
let(:xml) { Nokogiri::XML.parse(data) }
27+
28+
it 'contains infromation about success' do
29+
expect(xml.at(%Q{text:contains("success")})).to be_truthy
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)