summaryrefslogtreecommitdiff
path: root/lib/gitlab/badge/pipeline/status.rb
blob: f061ba226886603e8277c3812042f63a27f85f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module Badge
    module Pipeline
      ##
      # Pipeline status badge
      #
      class Status < Badge::Base
        attr_reader :project, :ref, :customization

        def initialize(project, ref, opts: {})
          @project = project
          @ref = ref
          @ignore_skipped = Gitlab::Utils.to_boolean(opts[:ignore_skipped], default: false)
          @customization = {
            key_width: opts[:key_width].to_i,
            key_text: opts[:key_text]
          }

          @sha = @project.commit(@ref).try(:sha)
        end

        def entity
          'pipeline'
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def status
          pipelines = @project.ci_pipelines
            .where(sha: @sha)

          relation = @ignore_skipped ? pipelines.without_statuses([:skipped]) : pipelines
          relation.latest_status(@ref) || 'unknown'
        end
        # rubocop: enable CodeReuse/ActiveRecord

        def metadata
          @metadata ||= Pipeline::Metadata.new(self)
        end

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