summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/status/factory.rb
blob: 3446644eff871e1a085380dad81aefdf279e5179 (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
49
50
51
52
53
54
# frozen_string_literal: true

module Gitlab
  module Ci
    module Status
      class Factory
        def initialize(subject, user)
          @subject = subject
          @user = user
          @status = subject.status || HasStatus::DEFAULT_STATUS
        end

        def fabricate!
          if extended_statuses.none?
            core_status
          else
            compound_extended_status
          end
        end

        def core_status
          Gitlab::Ci::Status
            .const_get(@status.capitalize)
            .new(@subject, @user)
            .extend(self.class.common_helpers)
        end

        def compound_extended_status
          extended_statuses.inject(core_status) do |status, extended|
            extended.new(status)
          end
        end

        def extended_statuses
          return @extended_statuses if defined?(@extended_statuses)

          groups = self.class.extended_statuses.map do |group|
            Array(group).find { |status| status.matches?(@subject, @user) }
          end

          @extended_statuses = groups.flatten.compact
        end

        def self.extended_statuses
          []
        end

        def self.common_helpers
          Module.new
        end
      end
    end
  end
end