Improve CI status badge implementation
This commit is contained in:
parent
14f928b730
commit
28c4c949a5
|
@ -57,9 +57,8 @@ class Projects::CommitController < Projects::ApplicationController
|
|||
render layout: false
|
||||
end
|
||||
|
||||
def status
|
||||
status_sha = ci_commit.sha if ci_commit
|
||||
image = Ci::ImageForBuildService.new.execute(@project, sha: status_sha)
|
||||
def badge
|
||||
image = Ci::ImageForBuildService.new.execute(@project, ref: params[:id])
|
||||
send_file(image.path, filename: image.name, disposition: 'inline', type: 'image/svg+xml')
|
||||
end
|
||||
|
||||
|
|
|
@ -1,28 +1,23 @@
|
|||
module Ci
|
||||
class ImageForBuildService
|
||||
def execute(project, params)
|
||||
sha = params[:sha]
|
||||
sha ||=
|
||||
if params[:ref]
|
||||
project.commit(params[:ref]).try(:sha)
|
||||
end
|
||||
def execute(project, opts)
|
||||
sha = opts[:sha] || ref_sha(project, opts[:ref])
|
||||
|
||||
commit = project.ci_commits.ordered.find_by(sha: sha)
|
||||
image_name = image_for_commit(commit)
|
||||
|
||||
image_path = Rails.root.join('public/ci', image_name)
|
||||
|
||||
OpenStruct.new(
|
||||
path: image_path,
|
||||
name: image_name
|
||||
)
|
||||
OpenStruct.new(path: image_path, name: image_name)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ref_sha(project, ref)
|
||||
project.commit(ref).try(:sha) if ref
|
||||
end
|
||||
|
||||
def image_for_commit(commit)
|
||||
return 'build-unknown.svg' unless commit
|
||||
|
||||
'build-' + commit.status + ".svg"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -496,9 +496,9 @@ Rails.application.routes.draw do
|
|||
|
||||
get(
|
||||
'/status/*id/badge',
|
||||
to: 'commit#status',
|
||||
to: 'commit#badge',
|
||||
constraints: { format: /png/ },
|
||||
as: :commit_status
|
||||
as: :commit_badge
|
||||
)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue