diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-04-01 13:03:14 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-04-06 19:56:34 +0200 |
commit | b7fa7c4d59b2fbdc49db81aa2d6a2531c931a2fe (patch) | |
tree | 01b4f5e2aa9231049caa56196f09b4b8a6ebd406 | |
parent | 88fc7ccddaab18435bdc85021d06d9aa21d28a68 (diff) | |
download | gitlab-ce-b7fa7c4d59b2fbdc49db81aa2d6a2531c931a2fe.tar.gz |
Extend build status badge, add html/markdown methods
-rw-r--r-- | app/controllers/projects/badges_controller.rb | 2 | ||||
-rw-r--r-- | app/views/projects/badges/index.html.haml | 6 | ||||
-rw-r--r-- | lib/gitlab/badge/build.rb | 30 | ||||
-rw-r--r-- | spec/lib/gitlab/badge/build_spec.rb | 33 |
4 files changed, 63 insertions, 8 deletions
diff --git a/app/controllers/projects/badges_controller.rb b/app/controllers/projects/badges_controller.rb index 01db85c4a8a..b96555a153a 100644 --- a/app/controllers/projects/badges_controller.rb +++ b/app/controllers/projects/badges_controller.rb @@ -2,6 +2,8 @@ class Projects::BadgesController < Projects::ApplicationController before_action :no_cache_headers, except: [:index] def index + @ref = params[:ref] || 'master' + @badge = Gitlab::Badge::Build.new(@project, @ref) end def build diff --git a/app/views/projects/badges/index.html.haml b/app/views/projects/badges/index.html.haml index b527df8ac98..87c525cc1cb 100644 --- a/app/views/projects/badges/index.html.haml +++ b/app/views/projects/badges/index.html.haml @@ -4,12 +4,12 @@ .panel.panel-default .panel-heading %b Builds badge · - = image_tag(build_namespace_project_badges_path(@project.namespace, @project, :master, format: :svg), alt: 'Builds badge') + = @badge.to_html .panel-body %table.table %tr %td Markdown - %td= markdown("```markdown\n[![build status](url)](link)\n```") + %td= markdown("```markdown\n#{@badge.to_markdown}\n```") %tr %td HTML - %td= markdown("```html\n<a href='link'><img src='url' /></a>\n```") + %td= markdown("```html\n#{@badge.to_html}\n```") diff --git a/lib/gitlab/badge/build.rb b/lib/gitlab/badge/build.rb index 28a2391dbf8..e5e9fab3f5c 100644 --- a/lib/gitlab/badge/build.rb +++ b/lib/gitlab/badge/build.rb @@ -4,14 +4,15 @@ module Gitlab # Build badge # class Build + include Gitlab::Application.routes.url_helpers + include ActionView::Helpers::AssetTagHelper + include ActionView::Helpers::UrlHelper + def initialize(project, ref) + @project, @ref = 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 @@ -19,6 +20,27 @@ module Gitlab def data File.read(@image[:path]) end + + def to_s + @image[:name].sub(/\.svg$/, '') + 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 diff --git a/spec/lib/gitlab/badge/build_spec.rb b/spec/lib/gitlab/badge/build_spec.rb index b78c2b6224f..329792bb685 100644 --- a/spec/lib/gitlab/badge/build_spec.rb +++ b/spec/lib/gitlab/badge/build_spec.rb @@ -3,13 +3,44 @@ require 'spec_helper' describe Gitlab::Badge::Build do let(:project) { create(:project) } let(:sha) { project.commit.sha } - let(:badge) { described_class.new(project, 'master') } + let(:branch) { 'master' } + let(:badge) { described_class.new(project, branch) } describe '#type' do subject { badge.type } it { is_expected.to eq 'image/svg+xml' } end + 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 + context 'build exists' do let(:ci_commit) { create(:ci_commit, project: project, sha: sha) } let!(:build) { create(:ci_build, commit: ci_commit) } |