debian-mirror-gitlab/lib/gitlab/ci/badge/coverage/report.rb

79 lines
1.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2021-03-11 19:13:27 +05:30
module Gitlab::Ci
2016-09-13 17:45:13 +05:30
module Badge
module Coverage
##
# Test coverage report badge
#
class Report < Badge::Base
2020-06-23 00:09:42 +05:30
attr_reader :project, :ref, :job, :customization
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
def initialize(project, ref, opts: { job: nil })
2016-09-13 17:45:13 +05:30
@project = project
@ref = ref
2020-06-23 00:09:42 +05:30
@job = opts[:job]
@customization = {
key_width: opts[:key_width].to_i,
2021-11-18 22:05:49 +05:30
key_text: opts[:key_text],
min_good: opts[:min_good].to_i,
min_acceptable: opts[:min_acceptable].to_i,
min_medium: opts[:min_medium].to_i
2020-06-23 00:09:42 +05:30
}
2016-09-13 17:45:13 +05:30
end
def entity
'coverage'
end
def status
@coverage ||= raw_coverage
return unless @coverage
2022-01-26 12:08:38 +05:30
@coverage.round(2)
2016-09-13 17:45:13 +05:30
end
def metadata
@metadata ||= Coverage::Metadata.new(self)
end
def template
@template ||= Coverage::Template.new(self)
end
private
2021-01-29 00:20:46 +05:30
def successful_pipeline
@successful_pipeline ||= @project.ci_pipelines.latest_successful_for_ref(@ref)
end
def failed_pipeline
@failed_pipeline ||= @project.ci_pipelines.latest_failed_for_ref(@ref)
end
def running_pipeline
@running_pipeline ||= @project.ci_pipelines.latest_running_for_ref(@ref)
end
2016-09-13 17:45:13 +05:30
def raw_coverage
2021-01-29 00:20:46 +05:30
latest =
if @job.present?
builds = ::Ci::Build
.in_pipelines([successful_pipeline, running_pipeline, failed_pipeline])
.latest
.success
.for_ref(@ref)
.by_name(@job)
builds.max_by(&:created_at)
else
successful_pipeline
end
2016-09-13 17:45:13 +05:30
2021-01-29 00:20:46 +05:30
latest&.coverage
2016-09-13 17:45:13 +05:30
end
end
end
end
end