BigW Consortium Gitlab

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

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
10 11
  belongs_to :user

12 13
  delegate :commit, to: :pipeline

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

  validates_presence_of :name

18 19
  alias_attribute :author, :user

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

26
  scope :retried, -> { where.not(id: latest) }
27
  scope :ordered, -> { order(:name) }
28

29 30 31
  scope :failed_but_allowed, -> do
    where(allow_failure: true, status: [:failed, :canceled])
  end
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
43

44 45
  scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
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

56 57 58 59
    event :run do
      transition pending: :running
    end

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

64
    event :drop do
65
      transition [:created, :pending, :running] => :failed
66 67 68
    end

    event :success do
69
      transition [:created, :pending, :running] => :success
70 71 72
    end

    event :cancel do
73
      transition [:created, :pending, :running] => :canceled
74 75
    end

76 77
    before_transition created: [:pending, :running] do |commit_status|
      commit_status.queued_at = Time.now
78 79
    end

80 81
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
82 83
    end

84 85
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
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
108 109
  end

110
  delegate :sha, :short_sha, to: :pipeline
111 112

  def before_sha
113
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
114
  end
115

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

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

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
132 133
  end

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

138 139 140 141
  def duration
    calculate_duration
  end

Kamil Trzcinski committed
142 143 144 145
  def playable?
    false
  end

146 147
  def stuck?
    false
148
  end
Kamil Trzcinski committed
149

150
  def has_trace?
151 152
    false
  end
153
end