BigW Consortium Gitlab

commit_status.rb 2.54 KB
Newer Older
1
class CommitStatus < ActiveRecord::Base
2
  include Statuseable
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, touch: true
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

Kamil Trzcinski committed
19
  scope :latest, -> { where(id: unscope(:select).select('max(id)').group(:name, :commit_id)) }
20
  scope :retried, -> { where.not(id: latest) }
21
  scope :ordered, -> { order(:name) }
22
  scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
23 24 25 26 27 28 29

  state_machine :status, initial: :pending do
    event :run do
      transition pending: :running
    end

    event :drop do
30
      transition [:pending, :running] => :failed
31 32 33 34 35 36 37 38 39 40
    end

    event :success do
      transition [:pending, :running] => :success
    end

    event :cancel do
      transition [:pending, :running] => :canceled
    end

41 42
    after_transition pending: :running do |commit_status|
      commit_status.update_attributes started_at: Time.now
43 44
    end

45 46
    after_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.update_attributes finished_at: Time.now
47 48
    end

49
    after_transition [:pending, :running] => :success do |commit_status|
50
      MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
51
    end
52 53

    after_transition any => :failed do |commit_status|
54
      MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status)
55
    end
56 57
  end

58
  delegate :sha, :short_sha, to: :pipeline
59 60

  def before_sha
61
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
62
  end
63

64
  def self.stages
65
    # We group by stage name, but order stages by theirs' index
66
    unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
67 68
  end

69 70 71 72 73 74 75
  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
76 77
  end

78
  def ignored?
79
    allow_failure? && (failed? || canceled?)
80 81
  end

82
  def duration
83 84 85 86 87 88
    duration =
      if started_at && finished_at
        finished_at - started_at
      elsif started_at
        Time.now - started_at
      end
Kamil Trzcinski committed
89
    duration
90
  end
Kamil Trzcinski committed
91

92
  def stuck?
93 94
    false
  end
95
end