Extend build status badge, add html/markdown methods
This commit is contained in:
parent
88fc7ccdda
commit
b7fa7c4d59
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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[](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```")
|
||||
|
|
|
|||
|
|
@ -4,12 +4,13 @@ module Gitlab
|
|||
# Build badge
|
||||
#
|
||||
class Build
|
||||
def initialize(project, ref)
|
||||
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
|
||||
end
|
||||
include Gitlab::Application.routes.url_helpers
|
||||
include ActionView::Helpers::AssetTagHelper
|
||||
include ActionView::Helpers::UrlHelper
|
||||
|
||||
def to_s
|
||||
@image[:name].sub(/\.svg$/, '')
|
||||
def initialize(project, ref)
|
||||
@project, @ref = project, ref
|
||||
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
|
||||
end
|
||||
|
||||
def type
|
||||
|
|
@ -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
|
||||
"[](#{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
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
|
|
|
|||
Loading…
Reference in New Issue