From 4cd1b9f4d82efe3ffe810dabf6929a749c36c1bf Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Mar 2016 11:24:18 +0100 Subject: Refactor builds badge, encapsulate inside a class --- app/controllers/projects/badges_controller.rb | 5 ++-- lib/gitlab/badge/build.rb | 24 +++++++++++++++++++ spec/lib/gitlab/badge/build_spec.rb | 33 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 lib/gitlab/badge/build.rb create mode 100644 spec/lib/gitlab/badge/build_spec.rb diff --git a/app/controllers/projects/badges_controller.rb b/app/controllers/projects/badges_controller.rb index 6ff47c4033a..6d4d4360988 100644 --- a/app/controllers/projects/badges_controller.rb +++ b/app/controllers/projects/badges_controller.rb @@ -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 diff --git a/lib/gitlab/badge/build.rb b/lib/gitlab/badge/build.rb new file mode 100644 index 00000000000..28a2391dbf8 --- /dev/null +++ b/lib/gitlab/badge/build.rb @@ -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 diff --git a/spec/lib/gitlab/badge/build_spec.rb b/spec/lib/gitlab/badge/build_spec.rb new file mode 100644 index 00000000000..e835de5e07d --- /dev/null +++ b/spec/lib/gitlab/badge/build_spec.rb @@ -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 -- cgit v1.2.1 From d6097ca402d78fb23383fee2afc91e7bafc604c3 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 29 Mar 2016 08:55:49 +0200 Subject: Extend specs for build badge --- spec/lib/gitlab/badge/build_spec.rb | 57 +++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/spec/lib/gitlab/badge/build_spec.rb b/spec/lib/gitlab/badge/build_spec.rb index e835de5e07d..b78c2b6224f 100644 --- a/spec/lib/gitlab/badge/build_spec.rb +++ b/spec/lib/gitlab/badge/build_spec.rb @@ -3,31 +3,70 @@ 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! } + context 'build exists' do + let(:ci_commit) { create(:ci_commit, project: project, sha: sha) } + let!(:build) { create(:ci_build, commit: ci_commit) } + + + 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 } + + it 'contains infromation about success' do + expect(status_node(data, 'success')).to be_truthy + end + end + end + + context 'build failed' do + before { build.drop! } + describe '#to_s' do + subject { badge.to_s } + it { is_expected.to eq 'build-failed' } + end + + describe '#data' do + let(:data) { badge.data } + + it 'contains infromation about failure' do + expect(status_node(data, 'failed')).to be_truthy + end + end + end + end + + context 'build does not exist' do describe '#to_s' do subject { badge.to_s } - it { is_expected.to eq 'build-success' } + it { is_expected.to eq 'build-unknown' } 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 + it 'contains infromation about unknown build' do + expect(status_node(data, 'unknown')).to be_truthy end end end + + def status_node(data, status) + xml = Nokogiri::XML.parse(data) + xml.at(%Q{text:contains("#{status}")}) + end end -- cgit v1.2.1