BigW Consortium Gitlab

stage.rb 1.68 KB
Newer Older
1 2
module Ci
  class Stage < ActiveRecord::Base
3
    extend Gitlab::Ci::Model
4
    include Importable
5
    include HasStatus
6
    include Gitlab::OptimisticLocking
7

8
    enum status: HasStatus::STATUSES_ENUM
9 10 11 12

    belongs_to :project
    belongs_to :pipeline

13
    has_many :statuses, class_name: 'CommitStatus', foreign_key: :stage_id
14
    has_many :builds, foreign_key: :stage_id
15 16 17 18

    validates :project, presence: true, unless: :importing?
    validates :pipeline, presence: true, unless: :importing?
    validates :name, presence: true, unless: :importing?
19

20 21 22 23
    after_initialize do |stage|
      self.status = DEFAULT_STATUS if self.status.nil?
    end

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    state_machine :status, initial: :created do
      event :enqueue do
        transition created: :pending
        transition [:success, :failed, :canceled, :skipped] => :running
      end

      event :run do
        transition any - [:running] => :running
      end

      event :skip do
        transition any - [:skipped] => :skipped
      end

      event :drop do
        transition any - [:failed] => :failed
      end

      event :succeed do
        transition any - [:success] => :success
      end

      event :cancel do
        transition any - [:canceled] => :canceled
      end

      event :block do
        transition any - [:manual] => :manual
      end
    end

55
    def update_status
56
      retry_optimistic_lock(self) do
57
        case statuses.latest.status
58 59 60 61 62 63 64 65 66 67 68
        when 'pending' then enqueue
        when 'running' then run
        when 'success' then succeed
        when 'failed' then drop
        when 'canceled' then cancel
        when 'manual' then block
        when 'skipped' then skip
        else skip
        end
      end
    end
69 70
  end
end