BigW Consortium Gitlab

commit_status.rb 3.54 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
9
  belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
10
  belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
11 12
  belongs_to :user

13
  delegate :commit, to: :pipeline
Douwe Maan committed
14
  delegate :sha, :short_sha, to: :pipeline
15

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

Douwe Maan committed
18
  validates :name, presence: true
19

20
  alias_attribute :author, :user
21
  
22
  scope :failed_but_allowed, -> do
23
    where(allow_failure: true, status: [:failed, :canceled])
24
  end
25

26
  scope :exclude_ignored, -> do
27 28 29
    # We want to ignore failed but allowed to fail jobs.
    #
    # TODO, we also skip ignored optional manual actions.
30
    where("allow_failure = ? OR status IN (?)",
31
      false, all_state_names - [:failed, :canceled, :manual])
32
  end
33

34
  scope :latest, -> { where(retried: [false, nil]) }
35
  scope :retried, -> { where(retried: true) }
36
  scope :ordered, -> { order(:name) }
37 38
  scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
39
  scope :after_stage, -> (index) { where('stage_idx > ?', index) }
40

41
  state_machine :status do
42
    event :enqueue do
43
      transition [:created, :skipped, :manual] => :pending
44 45
    end

46
    event :process do
47
      transition [:skipped, :manual] => :created
48 49
    end

50 51 52 53
    event :run do
      transition pending: :running
    end

54 55 56 57
    event :skip do
      transition [:created, :pending] => :skipped
    end

58
    event :drop do
59
      transition [:created, :pending, :running] => :failed
60 61 62
    end

    event :success do
63
      transition [:created, :pending, :running] => :success
64 65 66
    end

    event :cancel do
67
      transition [:created, :pending, :running, :manual] => :canceled
68 69
    end

70 71
    before_transition created: [:pending, :running] do |commit_status|
      commit_status.queued_at = Time.now
72 73
    end

74 75
    before_transition [:created, :pending] => :running do |commit_status|
      commit_status.started_at = Time.now
76 77
    end

78 79
    before_transition any => [:success, :failed, :canceled] do |commit_status|
      commit_status.finished_at = Time.now
80 81
    end

82
    after_transition do |commit_status, transition|
83
      next if transition.loopback?
84

85 86
      commit_status.run_after_commit do
        pipeline.try do |pipeline|
87
          if complete? || manual?
88
            PipelineProcessWorker.perform_async(pipeline.id)
89
          else
90
            PipelineUpdateWorker.perform_async(pipeline.id)
91
          end
92
          ExpireJobCacheWorker.perform_async(commit_status.id)
93
        end
94
      end
95 96
    end

97
    after_transition any => :failed do |commit_status|
98
      commit_status.run_after_commit do
99 100
        MergeRequests::AddTodoWhenBuildFailsService
          .new(pipeline.project, nil).execute(self)
101
      end
102
    end
103 104
  end

105 106 107 108
  def locking_enabled?
    status_changed?
  end

109
  def before_sha
110
    pipeline.before_sha || Gitlab::Git::BLANK_SHA
111
  end
112

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

117
  def failed_but_allowed?
118
    allow_failure? && (failed? || canceled?)
119 120
  end

121 122 123 124
  def duration
    calculate_duration
  end

Kamil Trzcinski committed
125 126 127 128
  def playable?
    false
  end

129 130
  def stuck?
    false
131
  end
Kamil Trzcinski committed
132

133
  def has_trace?
134 135
    false
  end
136

137 138 139 140
  def auto_canceled?
    canceled? && auto_canceled_by_id?
  end

141
  def detailed_status(current_user)
142 143 144
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
145
  end
146

Mike Greiling committed
147
  def sortable_name
148 149 150 151
    name.split(/(\d+)/).map do |v|
      v =~ /\d+/ ? v.to_i : v
    end
  end
152
end