commit_status.rb 3.7 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 34
  scope :exclude_ignored, -> do
    quoted_when = connection.quote_column_name('when')
35
    # We want to ignore failed_but_allowed jobs
36 37
    where("allow_failure = ? OR status IN (?)",
      false, all_state_names - [:failed, :canceled]).
38 39 40 41 42
      # We want to ignore skipped manual jobs
      where("#{quoted_when} <> ? OR status <> ?", 'manual', 'skipped').
      # We want to ignore skipped on_failure
      where("#{quoted_when} <> ? OR status <> ?", 'on_failure', 'skipped')
  end
L
Lin Jen-Shin 已提交
43

44 45
  scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
K
Kamil Trzcinski 已提交
46

47
  state_machine :status do
48
    event :enqueue do
49
      transition [:created, :skipped] => :pending
50 51
    end

52 53 54 55
    event :process do
      transition skipped: :created
    end

K
Kamil Trzcinski 已提交
56 57 58 59
    event :run do
      transition pending: :running
    end

60 61 62 63
    event :skip do
      transition [:created, :pending] => :skipped
    end

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

    event :success do
69
      transition [:created, :pending, :running] => :success
K
Kamil Trzcinski 已提交
70 71 72
    end

    event :cancel do
73
      transition [:created, :pending, :running] => :canceled
K
Kamil Trzcinski 已提交
74 75
    end

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

K
Kamil Trzcinski 已提交
80 81
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
K
Kamil Trzcinski 已提交
82 83
    end

K
Kamil Trzcinski 已提交
84 85
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
K
Kamil Trzcinski 已提交
86 87
    end

88
    after_transition do |commit_status, transition|
89
      next if transition.loopback?
90

91 92 93
      commit_status.run_after_commit do
        pipeline.try do |pipeline|
          if complete?
94
            PipelineProcessWorker.perform_async(pipeline.id)
95
          else
96
            PipelineUpdateWorker.perform_async(pipeline.id)
97
          end
98
        end
99
      end
100 101
    end

102
    after_transition any => :failed do |commit_status|
103 104 105 106
      commit_status.run_after_commit do
        MergeRequests::AddTodoWhenBuildFailsService
          .new(pipeline.project, nil).execute(self)
      end
107
    end
K
Kamil Trzcinski 已提交
108 109
  end

110
  delegate :sha, :short_sha, to: :pipeline
K
Kamil Trzcinski 已提交
111 112

  def before_sha
113
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
K
Kamil Trzcinski 已提交
114
  end
K
Kamil Trzcinski 已提交
115

K
Kamil Trzcinski 已提交
116
  def group_name
K
Kamil Trzcinski 已提交
117
    name.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
K
Kamil Trzcinski 已提交
118 119
  end

K
Kamil Trzcinski 已提交
120
  def self.stages
121
    # We group by stage name, but order stages by theirs' index
K
Kamil Trzcinski 已提交
122
    unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
K
Kamil Trzcinski 已提交
123 124
  end

K
Kamil Trzcinski 已提交
125 126 127 128 129 130 131
  def self.stages_status
    # We execute subquery for each stage to calculate a stage status
    statuses = unscoped.from(all, :sg).group('stage').pluck('sg.stage', all.where('stage=sg.stage').status_sql)
    statuses.inject({}) do |h, k|
      h[k.first] = k.last
      h
    end
K
Kamil Trzcinski 已提交
132 133
  end

134
  def failed_but_allowed?
135
    allow_failure? && (failed? || canceled?)
136 137
  end

K
Kamil Trzcinski 已提交
138 139 140 141
  def playable?
    false
  end

K
Kamil Trzcinski 已提交
142
  def duration
143
    calculate_duration
K
Kamil Trzcinski 已提交
144
  end
K
Kamil Trzcinski 已提交
145

K
Kamil Trzcinski 已提交
146
  def stuck?
147 148
    false
  end
K
Kamil Trzcinski 已提交
149
end