command.rb 1.77 KB
Newer Older
1 2 3 4
# rubocop:disable Naming/FileName
# frozen_string_literal: true

module Gitlab
5 6 7 8 9
  module Ci
    module Pipeline
      module Chain
        Command = Struct.new(
          :source, :project, :current_user,
10
          :origin_ref, :checkout_sha, :after_sha, :before_sha, :source_sha, :target_sha,
Shinya Maeda's avatar
Shinya Maeda committed
11
          :trigger_request, :schedule, :merge_request,
12
          :ignore_skip_ci, :save_incompleted,
James Fargher's avatar
James Fargher committed
13 14
          :seeds_block, :variables_attributes, :push_options,
          :chat_data
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
        ) do
          include Gitlab::Utils::StrongMemoize

          def initialize(**params)
            params.each do |key, value|
              self[key] = value
            end
          end

          def branch_exists?
            strong_memoize(:is_branch) do
              project.repository.branch_exists?(ref)
            end
          end

          def tag_exists?
            strong_memoize(:is_tag) do
              project.repository.tag_exists?(ref)
            end
          end

          def ref
            strong_memoize(:ref) do
              Gitlab::Git.ref_name(origin_ref)
            end
          end

          def sha
            strong_memoize(:sha) do
              project.commit(origin_sha || origin_ref).try(:id)
            end
          end

          def origin_sha
            checkout_sha || after_sha
          end

          def before_sha
53
            self[:before_sha] || checkout_sha || Gitlab::Git::BLANK_SHA
54
          end
55 56 57 58 59 60

          def protected_ref?
            strong_memoize(:protected_ref) do
              project.protected_for?(origin_ref)
            end
          end
61 62 63 64 65 66

          def ambiguous_ref?
            strong_memoize(:ambiguous_ref) do
              project.repository.ambiguous_ref?(origin_ref)
            end
          end
67 68 69 70 71
        end
      end
    end
  end
end