commit_status.rb 3.49 KB
Newer Older
1
class CommitStatus < ActiveRecord::Base
2
  include HasStatus
3
  include Importable
4

5 6
  self.table_name = 'ci_builds'

7
  belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
8
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
9 10
  belongs_to :user

11 12
  delegate :commit, to: :pipeline

13
  validates :pipeline, presence: true, unless: :importing?
14 15 16

  validates_presence_of :name

17 18
  alias_attribute :author, :user

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

25
  scope :retried, -> { where.not(id: latest) }
26
  scope :ordered, -> { order(:name) }
27
  scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
28 29
  scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
30

31
  state_machine :status do
32
    event :enqueue do
33
      transition [:created, :skipped] => :pending
34 35
    end

36 37 38 39
    event :process do
      transition skipped: :created
    end

40 41 42 43
    event :run do
      transition pending: :running
    end

44 45 46 47
    event :skip do
      transition [:created, :pending] => :skipped
    end

48
    event :drop do
49
      transition [:created, :pending, :running] => :failed
50 51 52
    end

    event :success do
53
      transition [:created, :pending, :running] => :success
54 55 56
    end

    event :cancel do
57
      transition [:created, :pending, :running] => :canceled
58 59
    end

60 61
    after_transition created: [:pending, :running] do |commit_status|
      commit_status.update_attributes queued_at: Time.now
62 63
    end

64
    after_transition [:created, :pending] => :running do |commit_status|
65
      commit_status.update_attributes started_at: Time.now
66 67
    end

68 69
    after_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.update_attributes finished_at: Time.now
70 71
    end

72 73 74 75
    # We use around_transition to process pipeline on next stages as soon as possible, before the `after_*` is executed
    around_transition any => [:success, :failed, :canceled] do |commit_status, block|
      block.call

76
      commit_status.pipeline.try(:process!)
77
    end
78

79
    after_transition do |commit_status, transition|
80
      commit_status.pipeline.try(:build_updated) unless transition.loopback?
81
    end
82 83

    after_transition [:created, :pending, :running] => :success do |commit_status|
84
      MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
85
    end
86 87

    after_transition any => :failed do |commit_status|
88
      MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status)
89
    end
90 91
  end

92
  delegate :sha, :short_sha, to: :pipeline
93 94

  def before_sha
95
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
96
  end
97

Kamil Trzciński's avatar
Kamil Trzciński committed
98
  def group_name
Kamil Trzciński's avatar
Kamil Trzciński committed
99
    name.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
Kamil Trzciński's avatar
Kamil Trzciński committed
100 101
  end

102
  def self.stages
103
    # We group by stage name, but order stages by theirs' index
104
    unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
105 106
  end

107 108 109 110 111 112 113
  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
114 115
  end

116
  def ignored?
117
    allow_failure? && (failed? || canceled?)
118 119
  end

Kamil Trzciński's avatar
Kamil Trzciński committed
120 121 122 123
  def playable?
    false
  end

124
  def duration
125
    calculate_duration
126
  end
Kamil Trzciński's avatar
Kamil Trzciński committed
127

128
  def stuck?
129 130
    false
  end
131
end