BigW Consortium Gitlab

commit_status.rb 3.24 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
  scope :exclude_ignored, -> do
34
    # We want to ignore failed_but_allowed jobs
35
    where("allow_failure = ? OR status IN (?)",
36
      false, all_state_names - [:failed, :canceled])
37
  end
38

39 40
  scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
  scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
41

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

47 48 49 50
    event :process do
      transition skipped: :created
    end

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

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

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

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

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

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

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

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

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

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

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

105
  delegate :sha, :short_sha, to: :pipeline
106 107

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

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

115
  def failed_but_allowed?
116
    allow_failure? && (failed? || canceled?)
117 118
  end

119 120 121 122
  def duration
    calculate_duration
  end

Kamil Trzcinski committed
123 124 125 126
  def playable?
    false
  end

127 128
  def stuck?
    false
129
  end
Kamil Trzcinski committed
130

131
  def has_trace?
132 133
    false
  end
134

135
  def detailed_status(current_user)
136 137 138
    Gitlab::Ci::Status::Factory
      .new(self, current_user)
      .fabricate!
139
  end
140

Mike Greiling committed
141
  def sortable_name
142 143 144 145
    name.split(/(\d+)/).map do |v|
      v =~ /\d+/ ? v.to_i : v
    end
  end
146
end