commit_status.rb 4.4 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

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

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

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

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

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

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

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

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

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

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

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

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

K
Kamil Trzcinski 已提交
88 89
    before_transition created: [:pending, :running] do |commit_status|
      commit_status.queued_at = Time.now
K
Kamil Trzcinski 已提交
90 91
    end

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

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

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

105
    after_transition do |commit_status, transition|
106
      next if transition.loopback?
107

108
      commit_status.run_after_commit do
109
        if pipeline
110
          if complete? || manual?
111
            PipelineProcessWorker.perform_async(pipeline.id)
112
          else
113
            PipelineUpdateWorker.perform_async(pipeline.id)
114
          end
115
        end
116

117
        StageUpdateWorker.perform_async(commit_status.stage_id)
118
        ExpireJobCacheWorker.perform_async(commit_status.id)
119
      end
120 121
    end

122
    after_transition any => :failed do |commit_status|
123
      commit_status.run_after_commit do
D
Douwe Maan 已提交
124 125
        MergeRequests::AddTodoWhenBuildFailsService
          .new(pipeline.project, nil).execute(self)
126
      end
127
    end
K
Kamil Trzcinski 已提交
128 129
  end

130 131 132 133
  def locking_enabled?
    status_changed?
  end

K
Kamil Trzcinski 已提交
134
  def before_sha
135
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
K
Kamil Trzcinski 已提交
136
  end
K
Kamil Trzcinski 已提交
137

K
Kamil Trzcinski 已提交
138
  def group_name
139
    name.to_s.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
K
Kamil Trzcinski 已提交
140 141
  end

142
  def failed_but_allowed?
143
    allow_failure? && (failed? || canceled?)
144 145
  end

146 147 148 149
  def duration
    calculate_duration
  end

K
Kamil Trzcinski 已提交
150 151 152 153
  def playable?
    false
  end

154 155 156 157 158
  # To be overriden when inherrited from
  def retryable?
    false
  end

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

164 165
  def stuck?
    false
K
Kamil Trzcinski 已提交
166
  end
K
Kamil Trzcinski 已提交
167

168
  def has_trace?
169 170
    false
  end
K
Kamil Trzcinski 已提交
171

172 173 174 175
  def auto_canceled?
    canceled? && auto_canceled_by_id?
  end

176
  def detailed_status(current_user)
D
Douwe Maan 已提交
177 178 179
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
K
Kamil Trzcinski 已提交
180
  end
181

M
Mike Greiling 已提交
182
  def sortable_name
183
    name.to_s.split(/(\d+)/).map do |v|
184 185 186
      v =~ /\d+/ ? v.to_i : v
    end
  end
K
Kamil Trzcinski 已提交
187
end