commit_status.rb 4.8 KB
Newer Older
K
Kamil Trzcinski 已提交
1
class CommitStatus < ActiveRecord::Base
2
  include HasStatus
3
  include Importable
4
  include AfterCommitQueue
K
Kamil Trzcinski 已提交
5

K
Kamil Trzcinski 已提交
6 7
  self.table_name = 'ci_builds'

8
  belongs_to :user
K
Kamil Trzciński 已提交
9
  belongs_to :project
10
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
11
  belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
K
Kamil Trzcinski 已提交
12

13
  delegate :commit, to: :pipeline
D
Douwe Maan 已提交
14
  delegate :sha, :short_sha, to: :pipeline
15

16
  validates :pipeline, presence: true, unless: :importing?
17
  validates :name, presence: true, unless: :importing?
K
Kamil Trzcinski 已提交
18

K
Kamil Trzcinski 已提交
19
  alias_attribute :author, :user
20
  alias_attribute :pipeline_id, :commit_id
21

22
  scope :failed_but_allowed, -> do
23
    where(allow_failure: true, status: [:failed, :canceled])
24
  end
L
Lin Jen-Shin 已提交
25

26
  scope :exclude_ignored, -> do
27 28 29
    # We want to ignore failed but allowed to fail jobs.
    #
    # TODO, we also skip ignored optional manual actions.
30
    where("allow_failure = ? OR status IN (?)",
31
      false, all_state_names - [:failed, :canceled, :manual])
32
  end
L
Lin Jen-Shin 已提交
33

K
Kamil Trzcinski 已提交
34
  scope :latest, -> { where(retried: [false, nil]) }
35
  scope :retried, -> { where(retried: true) }
36
  scope :ordered, -> { order(:name) }
37 38
  scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
39
  scope :after_stage, -> (index) { where('stage_idx > ?', index) }
K
Kamil Trzcinski 已提交
40

41
  enum failure_reason: {
S
Shinya Maeda 已提交
42
    unknown_failure: nil,
43
    script_failure: 1,
S
Shinya Maeda 已提交
44
    api_failure: 2,
45
    stuck_or_timeout_failure: 3,
46 47
    runner_system_failure: 4,
    missing_dependency_failure: 5
48 49
  }

50 51 52 53 54
  ##
  # We still create some CommitStatuses outside of CreatePipelineService.
  #
  # These are pages deployments and external statuses.
  #
55 56 57
  before_create unless: :importing? do
    Ci::EnsureStageService.new(project, user).execute(self) do |stage|
      self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
58 59 60
    end
  end

61
  state_machine :status do
62
    event :process do
63
      transition [:skipped, :manual] => :created
64 65
    end

66 67 68 69
    event :enqueue do
      transition [:created, :skipped, :manual] => :pending
    end

K
Kamil Trzcinski 已提交
70 71 72 73
    event :run do
      transition pending: :running
    end

74 75 76 77
    event :skip do
      transition [:created, :pending] => :skipped
    end

K
Kamil Trzcinski 已提交
78
    event :drop do
79
      transition [:created, :pending, :running] => :failed
K
Kamil Trzcinski 已提交
80 81 82
    end

    event :success do
83
      transition [:created, :pending, :running] => :success
K
Kamil Trzcinski 已提交
84 85 86
    end

    event :cancel do
87
      transition [:created, :pending, :running, :manual] => :canceled
K
Kamil Trzcinski 已提交
88 89
    end

90
    before_transition [:created, :skipped, :manual] => :pending do |commit_status|
K
Kamil Trzcinski 已提交
91
      commit_status.queued_at = Time.now
K
Kamil Trzcinski 已提交
92 93
    end

K
Kamil Trzcinski 已提交
94 95
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
K
Kamil Trzcinski 已提交
96 97
    end

K
Kamil Trzcinski 已提交
98 99
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
K
Kamil Trzcinski 已提交
100 101
    end

102 103 104 105 106
    before_transition any => :failed do |commit_status, transition|
      failure_reason = transition.args.first
      commit_status.failure_reason = failure_reason
    end

107
    after_transition do |commit_status, transition|
108
      next unless commit_status.project
109
      next if transition.loopback?
110

111
      commit_status.run_after_commit do
112
        if pipeline_id
113
          if complete? || manual?
114
            PipelineProcessWorker.perform_async(pipeline_id)
115
          else
116
            PipelineUpdateWorker.perform_async(pipeline_id)
117
          end
118
        end
119

120 121
        StageUpdateWorker.perform_async(stage_id)
        ExpireJobCacheWorker.perform_async(id)
122
      end
123 124
    end

125
    after_transition any => :failed do |commit_status|
126 127
      next unless commit_status.project

128
      commit_status.run_after_commit do
D
Douwe Maan 已提交
129
        MergeRequests::AddTodoWhenBuildFailsService
130
          .new(project, nil).execute(self)
131
      end
132
    end
K
Kamil Trzcinski 已提交
133 134
  end

135 136 137 138
  def locking_enabled?
    status_changed?
  end

K
Kamil Trzcinski 已提交
139
  def before_sha
140
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
K
Kamil Trzcinski 已提交
141
  end
K
Kamil Trzcinski 已提交
142

K
Kamil Trzcinski 已提交
143
  def group_name
144
    name.to_s.gsub(%r{\d+[\s:/\\]+\d+\s*}, '').strip
K
Kamil Trzcinski 已提交
145 146
  end

147
  def failed_but_allowed?
148
    allow_failure? && (failed? || canceled?)
149 150
  end

151 152 153 154
  def duration
    calculate_duration
  end

K
Kamil Trzcinski 已提交
155 156 157 158
  def playable?
    false
  end

159 160 161 162 163
  # To be overriden when inherrited from
  def retryable?
    false
  end

164 165 166 167 168
  # To be overriden when inherrited from
  def cancelable?
    false
  end

169 170
  def stuck?
    false
K
Kamil Trzcinski 已提交
171
  end
K
Kamil Trzcinski 已提交
172

173
  def has_trace?
174 175
    false
  end
K
Kamil Trzcinski 已提交
176

177 178 179 180
  def auto_canceled?
    canceled? && auto_canceled_by_id?
  end

181
  def detailed_status(current_user)
D
Douwe Maan 已提交
182 183 184
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
K
Kamil Trzcinski 已提交
185
  end
186

M
Mike Greiling 已提交
187
  def sortable_name
188
    name.to_s.split(/(\d+)/).map do |v|
189 190 191
      v =~ /\d+/ ? v.to_i : v
    end
  end
192 193 194 195 196 197 198

  # Rails 5.0 autogenerated question mark enum methods return wrong result if enum value is nil.
  # They always return `false`.
  # This method overwrites the autogenerated one to return correct result.
  def unknown_failure?
    Gitlab.rails5? ? failure_reason.nil? : super
  end
K
Kamil Trzcinski 已提交
199
end