generic_commit_status.rb 919 Bytes
Newer Older
1
2
# frozen_string_literal: true

Kamil Trzcinski's avatar
Kamil Trzcinski committed
3
class GenericCommitStatus < CommitStatus
4
5
  EXTERNAL_STAGE_IDX = 1_000_000

Kamil Trzcinski's avatar
Kamil Trzcinski committed
6
7
  before_validation :set_default_values

8
  validates :target_url, addressable_url: true,
9
10
                         length: { maximum: 255 },
                         allow_nil: true
11
  validate :name_uniqueness_across_types, unless: :importing?
12

Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
14
15
16
17
18
  # GitHub compatible API
  alias_attribute :context, :name

  def set_default_values
    self.context ||= 'default'
    self.stage ||= 'external'
19
    self.stage_idx ||= EXTERNAL_STAGE_IDX
Kamil Trzcinski's avatar
Kamil Trzcinski committed
20
21
22
23
24
  end

  def tags
    [:external]
  end
25
26
27
28
29
30

  def detailed_status(current_user)
    Gitlab::Ci::Status::External::Factory
      .new(self, current_user)
      .fabricate!
  end
31
32
33
34
35
36
37
38
39
40

  private

  def name_uniqueness_across_types
    return if !pipeline || name.blank?

    if pipeline.statuses.by_name(name).where.not(type: type).exists?
      errors.add(:name, :taken)
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41
end