BigW Consortium Gitlab

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

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

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

18
  validates :name, presence: true, unless: :importing?
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
      commit_status.run_after_commit do
86
        if pipeline
87
          if complete? || manual?
88
            PipelineProcessWorker.perform_async(pipeline.id)
89
          else
90
            PipelineUpdateWorker.perform_async(pipeline.id)
91
          end
92
        end
93 94

        ExpireJobCacheWorker.perform_async(commit_status.id)
95
      end
96 97
    end

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

106 107 108 109
  def locking_enabled?
    status_changed?
  end

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

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

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

122 123 124 125
  def duration
    calculate_duration
  end

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

130 131 132 133 134
  # To be overriden when inherrited from
  def retryable?
    false
  end

135 136 137 138 139
  # To be overriden when inherrited from
  def cancelable?
    false
  end

140 141
  def stuck?
    false
142
  end
Kamil Trzcinski committed
143

144
  def has_trace?
145 146
    false
  end
147

148 149 150 151
  def auto_canceled?
    canceled? && auto_canceled_by_id?
  end

152
  def detailed_status(current_user)
153 154 155
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
156
  end
157

Mike Greiling committed
158
  def sortable_name
159
    name.to_s.split(/(\d+)/).map do |v|
160 161 162
      v =~ /\d+/ ? v.to_i : v
    end
  end
163
end