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