commit_status.rb 3.0 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 :project, foreign_key: :gl_project_id
9
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
K
Kamil Trzcinski 已提交
10 11
  belongs_to :user

12 13
  delegate :commit, to: :pipeline

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

  validates_presence_of :name

K
Kamil Trzcinski 已提交
18 19
  alias_attribute :author, :user

L
Lin Jen-Shin 已提交
20 21 22 23 24
  scope :latest, -> do
    max_id = unscope(:select).select("max(#{quoted_table_name}.id)")

    where(id: max_id.group(:name, :commit_id))
  end
25

K
Kamil Trzcinski 已提交
26
  scope :retried, -> { where.not(id: latest) }
27
  scope :ordered, -> { order(:name) }
L
Lin Jen-Shin 已提交
28

29 30 31
  scope :failed_but_allowed, -> do
    where(allow_failure: true, status: [:failed, :canceled])
  end
L
Lin Jen-Shin 已提交
32

33
  scope :exclude_ignored, -> do
34
    # We want to ignore failed_but_allowed jobs
35
    where("allow_failure = ? OR status IN (?)",
36
      false, all_state_names - [:failed, :canceled])
37
  end
L
Lin Jen-Shin 已提交
38

39 40
  scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
K
Kamil Trzcinski 已提交
41

42
  state_machine :status do
43
    event :enqueue do
44
      transition [:created, :skipped] => :pending
45 46
    end

47 48 49 50
    event :process do
      transition skipped: :created
    end

K
Kamil Trzcinski 已提交
51 52 53 54
    event :run do
      transition pending: :running
    end

55 56 57 58
    event :skip do
      transition [:created, :pending] => :skipped
    end

K
Kamil Trzcinski 已提交
59
    event :drop do
60
      transition [:created, :pending, :running] => :failed
K
Kamil Trzcinski 已提交
61 62 63
    end

    event :success do
64
      transition [:created, :pending, :running] => :success
K
Kamil Trzcinski 已提交
65 66 67
    end

    event :cancel do
68
      transition [:created, :pending, :running] => :canceled
K
Kamil Trzcinski 已提交
69 70
    end

K
Kamil Trzcinski 已提交
71 72
    before_transition created: [:pending, :running] do |commit_status|
      commit_status.queued_at = Time.now
K
Kamil Trzcinski 已提交
73 74
    end

K
Kamil Trzcinski 已提交
75 76
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
K
Kamil Trzcinski 已提交
77 78
    end

K
Kamil Trzcinski 已提交
79 80
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
K
Kamil Trzcinski 已提交
81 82
    end

83
    after_transition do |commit_status, transition|
84
      next if transition.loopback?
85

86 87 88
      commit_status.run_after_commit do
        pipeline.try do |pipeline|
          if complete?
89
            PipelineProcessWorker.perform_async(pipeline.id)
90
          else
91
            PipelineUpdateWorker.perform_async(pipeline.id)
92
          end
93
        end
94
      end
95 96
    end

97
    after_transition any => :failed do |commit_status|
98 99 100 101
      commit_status.run_after_commit do
        MergeRequests::AddTodoWhenBuildFailsService
          .new(pipeline.project, nil).execute(self)
      end
102
    end
K
Kamil Trzcinski 已提交
103 104
  end

105
  delegate :sha, :short_sha, to: :pipeline
K
Kamil Trzcinski 已提交
106 107

  def before_sha
108
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
K
Kamil Trzcinski 已提交
109
  end
K
Kamil Trzcinski 已提交
110

K
Kamil Trzcinski 已提交
111
  def group_name
K
Kamil Trzcinski 已提交
112
    name.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
K
Kamil Trzcinski 已提交
113 114
  end

115
  def failed_but_allowed?
116
    allow_failure? && (failed? || canceled?)
117 118
  end

119 120 121 122
  def duration
    calculate_duration
  end

K
Kamil Trzcinski 已提交
123 124 125 126
  def playable?
    false
  end

127 128
  def stuck?
    false
K
Kamil Trzcinski 已提交
129
  end
K
Kamil Trzcinski 已提交
130

131
  def has_trace?
132 133
    false
  end
K
Kamil Trzcinski 已提交
134
end