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

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

K
Kamil Trzcinski 已提交
7
  belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
8
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
K
Kamil Trzcinski 已提交
9 10
  belongs_to :user

11 12
  delegate :commit, to: :pipeline

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

  validates_presence_of :name

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

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

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

K
Kamil Trzcinski 已提交
25
  scope :retried, -> { where.not(id: latest) }
26
  scope :ordered, -> { order(:name) }
27 28 29 30 31
  scope :failed_but_allowed, -> do
    where(allow_failure: true, status: [:failed, :canceled])
  end
  scope :exclude_ignored, -> do
    quoted_when = connection.quote_column_name('when')
32
    # We want to ignore failed_but_allowed jobs
33 34 35 36 37 38 39 40
    where("allow_failure = ? OR status NOT IN (?)",
      false, [:failed, :canceled]).
      # 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
41 42
  scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
K
Kamil Trzcinski 已提交
43

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

49 50 51 52
    event :process do
      transition skipped: :created
    end

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

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

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

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

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

73 74
    after_transition created: [:pending, :running] do |commit_status|
      commit_status.update_attributes queued_at: Time.now
K
Kamil Trzcinski 已提交
75 76
    end

77
    after_transition [:created, :pending] => :running do |commit_status|
78
      commit_status.update_attributes started_at: Time.now
K
Kamil Trzcinski 已提交
79 80
    end

81 82
    after_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.update_attributes finished_at: Time.now
K
Kamil Trzcinski 已提交
83 84
    end

85
    after_transition any => [:success, :failed, :canceled] do |commit_status|
86 87 88 89
      commit_status.pipeline.try(:process!)
      true
    end

90 91 92 93
    after_transition do |commit_status, transition|
      commit_status.pipeline.try(:build_updated) unless transition.loopback?
    end

94
    after_transition [:created, :pending, :running] => :success do |commit_status|
95
      MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
Z
Zeger-Jan van de Weg 已提交
96
    end
97 98

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

103
  delegate :sha, :short_sha, to: :pipeline
K
Kamil Trzcinski 已提交
104 105

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

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

K
Kamil Trzcinski 已提交
113
  def self.stages
114
    # We group by stage name, but order stages by theirs' index
K
Kamil Trzcinski 已提交
115
    unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
K
Kamil Trzcinski 已提交
116 117
  end

K
Kamil Trzcinski 已提交
118 119 120 121 122 123 124
  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 已提交
125 126
  end

127
  def failed_but_allowed?
128
    allow_failure? && (failed? || canceled?)
129 130
  end

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

K
Kamil Trzcinski 已提交
135
  def duration
136
    calculate_duration
K
Kamil Trzcinski 已提交
137
  end
K
Kamil Trzcinski 已提交
138

K
Kamil Trzcinski 已提交
139
  def stuck?
140 141
    false
  end
K
Kamil Trzcinski 已提交
142
end