2018-10-11 20:12:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-10 12:12:31 +00:00
|
|
|
module Gitlab
|
|
|
|
module Badge
|
|
|
|
module Coverage
|
|
|
|
##
|
|
|
|
# Test coverage report badge
|
|
|
|
#
|
|
|
|
class Report < Badge::Base
|
2020-05-29 21:08:35 +00:00
|
|
|
attr_reader :project, :ref, :job, :customization
|
2016-08-11 10:38:03 +00:00
|
|
|
|
2020-05-29 21:08:35 +00:00
|
|
|
def initialize(project, ref, opts: { job: nil })
|
2016-08-10 12:12:31 +00:00
|
|
|
@project = project
|
|
|
|
@ref = ref
|
2020-05-29 21:08:35 +00:00
|
|
|
@job = opts[:job]
|
|
|
|
@customization = {
|
|
|
|
key_width: opts[:key_width].to_i,
|
|
|
|
key_text: opts[:key_text]
|
|
|
|
}
|
2016-08-11 10:38:03 +00:00
|
|
|
|
2019-07-23 19:29:12 +00:00
|
|
|
@pipeline = @project.ci_pipelines.latest_successful_for_ref(@ref)
|
2016-08-10 12:12:31 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 10:38:03 +00:00
|
|
|
def entity
|
|
|
|
'coverage'
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
@coverage ||= raw_coverage
|
|
|
|
return unless @coverage
|
|
|
|
|
2017-03-20 13:34:47 +00:00
|
|
|
@coverage.to_f.round(2)
|
2016-08-11 10:38:03 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:04:37 +00:00
|
|
|
def metadata
|
|
|
|
@metadata ||= Coverage::Metadata.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def template
|
|
|
|
@template ||= Coverage::Template.new(self)
|
|
|
|
end
|
|
|
|
|
2016-08-11 10:38:03 +00:00
|
|
|
private
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-11 10:38:03 +00:00
|
|
|
def raw_coverage
|
|
|
|
return unless @pipeline
|
|
|
|
|
|
|
|
if @job.blank?
|
|
|
|
@pipeline.coverage
|
|
|
|
else
|
|
|
|
@pipeline.builds
|
|
|
|
.find_by(name: @job)
|
|
|
|
.try(:coverage)
|
|
|
|
end
|
2016-08-10 12:12:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-10 12:12:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|