commit_status.rb 4.86 KB
Newer Older
1
class CommitStatus < ActiveRecord::Base
2
  include HasStatus
3
  include Importable
4
  include AfterCommitQueue
5
  include Presentable
6

7 8
  self.table_name = 'ci_builds'

9
  belongs_to :user
10
  belongs_to :project
11
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
12
  belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
13

14
  delegate :commit, to: :pipeline
Douwe Maan's avatar
Douwe Maan committed
15
  delegate :sha, :short_sha, to: :pipeline
16

17
  validates :pipeline, presence: true, unless: :importing?
18
  validates :name, presence: true, unless: :importing?
19

Kamil Trzcinski's avatar
Kamil Trzcinski committed
20
  alias_attribute :author, :user
21
  alias_attribute :pipeline_id, :commit_id
22

23
  scope :failed_but_allowed, -> do
24
    where(allow_failure: true, status: [:failed, :canceled])
25
  end
26

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

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

42
  enum failure_reason: {
Shinya Maeda's avatar
Shinya Maeda committed
43
    unknown_failure: nil,
44
    script_failure: 1,
Shinya Maeda's avatar
Shinya Maeda committed
45
    api_failure: 2,
46
    stuck_or_timeout_failure: 3,
47 48
    runner_system_failure: 4,
    missing_dependency_failure: 5
49 50
  }

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

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

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

71 72 73 74
    event :run do
      transition pending: :running
    end

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

79
    event :drop do
80
      transition [:created, :pending, :running] => :failed
81 82 83
    end

    event :success do
84
      transition [:created, :pending, :running] => :success
85 86 87
    end

    event :cancel do
88
      transition [:created, :pending, :running, :manual] => :canceled
89 90
    end

91
    before_transition [:created, :skipped, :manual] => :pending do |commit_status|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
92
      commit_status.queued_at = Time.now
93 94
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
95 96
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
97 98
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
99 100
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
101 102
    end

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

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

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

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

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

129
      commit_status.run_after_commit do
130
        MergeRequests::AddTodoWhenBuildFailsService
131
          .new(project, nil).execute(self)
132
      end
133
    end
134 135
  end

136 137 138 139
  def locking_enabled?
    status_changed?
  end

140
  def before_sha
141
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
142
  end
143

Kamil Trzcinski's avatar
Kamil Trzcinski committed
144
  def group_name
145
    name.to_s.gsub(%r{\d+[\s:/\\]+\d+\s*}, '').strip
Kamil Trzcinski's avatar
Kamil Trzcinski committed
146 147
  end

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

152 153 154 155
  def duration
    calculate_duration
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
156 157 158 159
  def playable?
    false
  end

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

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

170 171
  def stuck?
    false
172
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
173

174
  def has_trace?
175 176
    false
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
177

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

182
  def detailed_status(current_user)
183 184 185
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
186
  end
187

Mike Greiling's avatar
Mike Greiling committed
188
  def sortable_name
189
    name.to_s.split(/(\d+)/).map do |v|
190 191 192
      v =~ /\d+/ ? v.to_i : v
    end
  end
193 194 195 196 197 198 199

  # 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
200
end