Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

report.rb 1.14 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
7
8
9
module Gitlab
  module Badge
    module Coverage
      ##
      # Test coverage report badge
      #
      class Report < Badge::Base
10
11
        attr_reader :project, :ref, :job

12
13
14
15
        def initialize(project, ref, job = nil)
          @project = project
          @ref = ref
          @job = job
16

17
          @pipeline = @project.ci_pipelines.latest_successful_for(@ref)
18
19
        end

20
21
22
23
24
25
26
27
        def entity
          'coverage'
        end

        def status
          @coverage ||= raw_coverage
          return unless @coverage

28
          @coverage.to_f.round(2)
29
30
        end

31
32
33
34
35
36
37
38
        def metadata
          @metadata ||= Coverage::Metadata.new(self)
        end

        def template
          @template ||= Coverage::Template.new(self)
        end

39
40
        private

41
        # rubocop: disable CodeReuse/ActiveRecord
42
43
44
45
46
47
48
49
50
51
        def raw_coverage
          return unless @pipeline

          if @job.blank?
            @pipeline.coverage
          else
            @pipeline.builds
              .find_by(name: @job)
              .try(:coverage)
          end
52
        end
53
        # rubocop: enable CodeReuse/ActiveRecord
54
55
56
57
      end
    end
  end
end