From 9f0b46c05aef9d0352bfaa5e42e34143227de8ff Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 10 Aug 2016 14:12:31 +0200 Subject: Move badges to separate modules and add base class --- app/controllers/projects/badges_controller.rb | 2 +- .../projects/pipelines_settings_controller.rb | 2 +- lib/gitlab/badge/base.rb | 21 ++++ lib/gitlab/badge/build.rb | 30 ------ lib/gitlab/badge/build/metadata.rb | 2 +- lib/gitlab/badge/build/status.rb | 32 +++++++ lib/gitlab/badge/build/template.rb | 2 +- lib/gitlab/badge/coverage.rb | 17 ---- lib/gitlab/badge/coverage/report.rb | 19 ++++ spec/lib/gitlab/badge/build/status_spec.rb | 106 +++++++++++++++++++++ spec/lib/gitlab/badge/build_spec.rb | 106 --------------------- spec/lib/gitlab/badge/coverage/report_spec.rb | 21 ++++ spec/lib/gitlab/badge/coverage_spec.rb | 19 ---- 13 files changed, 203 insertions(+), 176 deletions(-) create mode 100644 lib/gitlab/badge/base.rb delete mode 100644 lib/gitlab/badge/build.rb create mode 100644 lib/gitlab/badge/build/status.rb delete mode 100644 lib/gitlab/badge/coverage.rb create mode 100644 lib/gitlab/badge/coverage/report.rb create mode 100644 spec/lib/gitlab/badge/build/status_spec.rb delete mode 100644 spec/lib/gitlab/badge/build_spec.rb create mode 100644 spec/lib/gitlab/badge/coverage/report_spec.rb delete mode 100644 spec/lib/gitlab/badge/coverage_spec.rb diff --git a/app/controllers/projects/badges_controller.rb b/app/controllers/projects/badges_controller.rb index d0f5071d2cc..e026ceaf757 100644 --- a/app/controllers/projects/badges_controller.rb +++ b/app/controllers/projects/badges_controller.rb @@ -4,7 +4,7 @@ class Projects::BadgesController < Projects::ApplicationController before_action :no_cache_headers, except: [:index] def build - badge = Gitlab::Badge::Build.new(project, params[:ref]) + badge = Gitlab::Badge::Build::Status.new(project, params[:ref]) respond_to do |format| format.html { render_404 } diff --git a/app/controllers/projects/pipelines_settings_controller.rb b/app/controllers/projects/pipelines_settings_controller.rb index 75dd3648e45..fe7cb748f43 100644 --- a/app/controllers/projects/pipelines_settings_controller.rb +++ b/app/controllers/projects/pipelines_settings_controller.rb @@ -3,7 +3,7 @@ class Projects::PipelinesSettingsController < Projects::ApplicationController def show @ref = params[:ref] || @project.default_branch || 'master' - @build_badge = Gitlab::Badge::Build.new(@project, @ref).metadata + @build_badge = Gitlab::Badge::Build::Status.new(@project, @ref).metadata end def update diff --git a/lib/gitlab/badge/base.rb b/lib/gitlab/badge/base.rb new file mode 100644 index 00000000000..229e7b5aa57 --- /dev/null +++ b/lib/gitlab/badge/base.rb @@ -0,0 +1,21 @@ +module Gitlab + module Badge + class Base + def key_text + raise NotImplementedError + end + + def value_text + raise NotImplementedError + end + + def metadata + raise NotImplementedError + end + + def template + raise NotImplementedError + end + end + end +end diff --git a/lib/gitlab/badge/build.rb b/lib/gitlab/badge/build.rb deleted file mode 100644 index 1de721a2269..00000000000 --- a/lib/gitlab/badge/build.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Gitlab - module Badge - ## - # Build badge - # - class Build - delegate :key_text, :value_text, to: :template - - def initialize(project, ref) - @project = project - @ref = ref - @sha = @project.commit(@ref).try(:sha) - end - - def status - @project.pipelines - .where(sha: @sha, ref: @ref) - .status || 'unknown' - end - - def metadata - @metadata ||= Build::Metadata.new(@project, @ref) - end - - def template - @template ||= Build::Template.new(status) - end - end - end -end diff --git a/lib/gitlab/badge/build/metadata.rb b/lib/gitlab/badge/build/metadata.rb index 553ef8d7b16..fbe10b948c4 100644 --- a/lib/gitlab/badge/build/metadata.rb +++ b/lib/gitlab/badge/build/metadata.rb @@ -1,6 +1,6 @@ module Gitlab module Badge - class Build + module Build ## # Class that describes build badge metadata # diff --git a/lib/gitlab/badge/build/status.rb b/lib/gitlab/badge/build/status.rb new file mode 100644 index 00000000000..a72e284d513 --- /dev/null +++ b/lib/gitlab/badge/build/status.rb @@ -0,0 +1,32 @@ +module Gitlab + module Badge + module Build + ## + # Build status badge + # + class Status < Badge::Base + delegate :key_text, :value_text, to: :template + + def initialize(project, ref) + @project = project + @ref = ref + @sha = @project.commit(@ref).try(:sha) + end + + def status + @project.pipelines + .where(sha: @sha, ref: @ref) + .status || 'unknown' + end + + def metadata + @metadata ||= Build::Metadata.new(@project, @ref) + end + + def template + @template ||= Build::Template.new(status) + end + end + end + end +end diff --git a/lib/gitlab/badge/build/template.rb b/lib/gitlab/badge/build/template.rb index deba3b669b3..779569d0cd7 100644 --- a/lib/gitlab/badge/build/template.rb +++ b/lib/gitlab/badge/build/template.rb @@ -1,6 +1,6 @@ module Gitlab module Badge - class Build + module Build ## # Class that represents a build badge template. # diff --git a/lib/gitlab/badge/coverage.rb b/lib/gitlab/badge/coverage.rb deleted file mode 100644 index 94af3a7ec34..00000000000 --- a/lib/gitlab/badge/coverage.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Gitlab - module Badge - ## - # Test coverage badge - # - class Coverage - def initialize(project, ref, job = nil) - @project = project - @ref = ref - @job = job - end - - def coverage - end - end - end -end diff --git a/lib/gitlab/badge/coverage/report.rb b/lib/gitlab/badge/coverage/report.rb new file mode 100644 index 00000000000..e6de15e085f --- /dev/null +++ b/lib/gitlab/badge/coverage/report.rb @@ -0,0 +1,19 @@ +module Gitlab + module Badge + module Coverage + ## + # Test coverage report badge + # + class Report < Badge::Base + def initialize(project, ref, job = nil) + @project = project + @ref = ref + @job = job + end + + def coverage + end + end + end + end +end diff --git a/spec/lib/gitlab/badge/build/status_spec.rb b/spec/lib/gitlab/badge/build/status_spec.rb new file mode 100644 index 00000000000..fa5bc068918 --- /dev/null +++ b/spec/lib/gitlab/badge/build/status_spec.rb @@ -0,0 +1,106 @@ +require 'spec_helper' + +describe Gitlab::Badge::Build::Status do + let(:project) { create(:project) } + let(:sha) { project.commit.sha } + let(:branch) { 'master' } + let(:badge) { described_class.new(project, branch) } + + describe '#metadata' do + it 'returns badge metadata' do + expect(badge.metadata.image_url) + .to include 'badges/master/build.svg' + end + end + + describe '#key_text' do + it 'always says build' do + expect(badge.key_text).to eq 'build' + end + end + + context 'build exists' do + let!(:build) { create_build(project, sha, branch) } + + context 'build success' do + before { build.success! } + + describe '#status' do + it 'is successful' do + expect(badge.status).to eq 'success' + end + end + + describe '#value_text' do + it 'returns correct value text' do + expect(badge.value_text).to eq 'success' + end + end + end + + context 'build failed' do + before { build.drop! } + + describe '#status' do + it 'failed' do + expect(badge.status).to eq 'failed' + end + end + + describe '#value_text' do + it 'has correct value text' do + expect(badge.value_text).to eq 'failed' + end + end + end + + context 'when outdated pipeline for given ref exists' do + before do + build.success! + + old_build = create_build(project, '11eeffdd', branch) + old_build.drop! + end + + it 'does not take outdated pipeline into account' do + expect(badge.status).to eq 'success' + end + end + + context 'when multiple pipelines exist for given sha' do + before do + build.drop! + + new_build = create_build(project, sha, branch) + new_build.success! + end + + it 'reports the compound status' do + expect(badge.status).to eq 'failed' + end + end + end + + context 'build does not exist' do + describe '#status' do + it 'is unknown' do + expect(badge.status).to eq 'unknown' + end + end + + describe '#value_text' do + it 'has correct value text' do + expect(badge.value_text).to eq 'unknown' + end + end + end + + def create_build(project, sha, branch) + pipeline = create(:ci_empty_pipeline, + project: project, + sha: sha, + ref: branch) + + create(:ci_build, pipeline: pipeline, stage: 'notify') + end +end diff --git a/spec/lib/gitlab/badge/build_spec.rb b/spec/lib/gitlab/badge/build_spec.rb deleted file mode 100644 index bb8144d5122..00000000000 --- a/spec/lib/gitlab/badge/build_spec.rb +++ /dev/null @@ -1,106 +0,0 @@ -require 'spec_helper' - -describe Gitlab::Badge::Build do - let(:project) { create(:project) } - let(:sha) { project.commit.sha } - let(:branch) { 'master' } - let(:badge) { described_class.new(project, branch) } - - describe '#metadata' do - it 'returns badge metadata' do - expect(badge.metadata.image_url) - .to include 'badges/master/build.svg' - end - end - - describe '#key_text' do - it 'always says build' do - expect(badge.key_text).to eq 'build' - end - end - - context 'build exists' do - let!(:build) { create_build(project, sha, branch) } - - context 'build success' do - before { build.success! } - - describe '#status' do - it 'is successful' do - expect(badge.status).to eq 'success' - end - end - - describe '#value_text' do - it 'returns correct value text' do - expect(badge.value_text).to eq 'success' - end - end - end - - context 'build failed' do - before { build.drop! } - - describe '#status' do - it 'failed' do - expect(badge.status).to eq 'failed' - end - end - - describe '#value_text' do - it 'has correct value text' do - expect(badge.value_text).to eq 'failed' - end - end - end - - context 'when outdated pipeline for given ref exists' do - before do - build.success! - - old_build = create_build(project, '11eeffdd', branch) - old_build.drop! - end - - it 'does not take outdated pipeline into account' do - expect(badge.status).to eq 'success' - end - end - - context 'when multiple pipelines exist for given sha' do - before do - build.drop! - - new_build = create_build(project, sha, branch) - new_build.success! - end - - it 'reports the compound status' do - expect(badge.status).to eq 'failed' - end - end - end - - context 'build does not exist' do - describe '#status' do - it 'is unknown' do - expect(badge.status).to eq 'unknown' - end - end - - describe '#value_text' do - it 'has correct value text' do - expect(badge.value_text).to eq 'unknown' - end - end - end - - def create_build(project, sha, branch) - pipeline = create(:ci_empty_pipeline, - project: project, - sha: sha, - ref: branch) - - create(:ci_build, pipeline: pipeline, stage: 'notify') - end -end diff --git a/spec/lib/gitlab/badge/coverage/report_spec.rb b/spec/lib/gitlab/badge/coverage/report_spec.rb new file mode 100644 index 00000000000..57b89dd8cda --- /dev/null +++ b/spec/lib/gitlab/badge/coverage/report_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Gitlab::Badge::Coverage::Report do + let(:project) { create(:project) } + + let(:pipeline) do + create(:ci_pipeline, project: project, + sha: project.commit.id, + ref: 'master') + end + + let(:badge) do + described_class.new(project, 'master') + end + + context 'builds exist' do + end + + context 'builds do not exist' do + end +end diff --git a/spec/lib/gitlab/badge/coverage_spec.rb b/spec/lib/gitlab/badge/coverage_spec.rb deleted file mode 100644 index 343ca3c0f85..00000000000 --- a/spec/lib/gitlab/badge/coverage_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'spec_helper' - -describe Gitlab::Badge::Coverage do - let(:project) { create(:project) } - - let(:pipeline) do - create(:ci_pipeline, project: project, - sha: project.commit.id, - ref: 'master') - end - - let(:badge) { described_class.new(project, 'master') } - - context 'builds exist' do - end - - context 'build does not exist' do - end -end -- cgit v1.2.1