debian-mirror-gitlab/app/models/commit_status.rb

122 lines
3.2 KiB
Ruby
Raw Normal View History

2015-10-24 18:46:33 +05:30
class CommitStatus < ActiveRecord::Base
2016-06-02 11:05:42 +05:30
include Statuseable
2016-06-22 15:30:34 +05:30
include Importable
2016-06-02 11:05:42 +05:30
2015-10-24 18:46:33 +05:30
self.table_name = 'ci_builds'
2015-12-23 02:04:40 +05:30
belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
2016-09-13 17:45:13 +05:30
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
2015-10-24 18:46:33 +05:30
belongs_to :user
2016-06-22 15:30:34 +05:30
delegate :commit, to: :pipeline
validates :pipeline, presence: true, unless: :importing?
2015-10-24 18:46:33 +05:30
validates_presence_of :name
alias_attribute :author, :user
2016-08-24 12:49:21 +05:30
scope :latest, -> do
max_id = unscope(:select).select("max(#{quoted_table_name}.id)")
where(id: max_id.group(:name, :commit_id))
end
2016-09-13 17:45:13 +05:30
2016-06-02 11:05:42 +05:30
scope :retried, -> { where.not(id: latest) }
scope :ordered, -> { order(:name) }
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
2016-09-13 17:45:13 +05:30
state_machine :status do
event :enqueue do
transition [:created, :skipped] => :pending
end
event :process do
transition skipped: :created
2016-08-24 12:49:21 +05:30
end
2015-10-24 18:46:33 +05:30
event :run do
transition pending: :running
end
2016-09-13 17:45:13 +05:30
event :skip do
transition [:created, :pending] => :skipped
end
2015-10-24 18:46:33 +05:30
event :drop do
2016-09-13 17:45:13 +05:30
transition [:created, :pending, :running] => :failed
2015-10-24 18:46:33 +05:30
end
event :success do
2016-09-13 17:45:13 +05:30
transition [:created, :pending, :running] => :success
2015-10-24 18:46:33 +05:30
end
event :cancel do
2016-09-13 17:45:13 +05:30
transition [:created, :pending, :running] => :canceled
end
after_transition created: [:pending, :running] do |commit_status|
commit_status.update_attributes queued_at: Time.now
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
after_transition [:created, :pending] => :running do |commit_status|
2016-04-02 18:10:28 +05:30
commit_status.update_attributes started_at: Time.now
2015-10-24 18:46:33 +05:30
end
2016-04-02 18:10:28 +05:30
after_transition any => [:success, :failed, :canceled] do |commit_status|
commit_status.update_attributes finished_at: Time.now
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
# We use around_transition to process pipeline on next stages as soon as possible, before the `after_*` is executed
around_transition any => [:success, :failed, :canceled] do |commit_status, block|
block.call
commit_status.pipeline.try(:process!)
end
after_transition do |commit_status, transition|
commit_status.pipeline.try(:build_updated) unless transition.loopback?
end
after_transition [:created, :pending, :running] => :success do |commit_status|
MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
2015-12-23 02:04:40 +05:30
end
2016-06-02 11:05:42 +05:30
after_transition any => :failed do |commit_status|
MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status)
2016-06-02 11:05:42 +05:30
end
2015-10-24 18:46:33 +05:30
end
delegate :sha, :short_sha, to: :pipeline
2015-10-24 18:46:33 +05:30
def before_sha
pipeline.before_sha || Gitlab::Git::BLANK_SHA
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
def self.stages
# We group by stage name, but order stages by theirs' index
unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
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
2015-10-24 18:46:33 +05:30
end
2016-04-02 18:10:28 +05:30
def ignored?
2016-06-02 11:05:42 +05:30
allow_failure? && (failed? || canceled?)
2016-04-02 18:10:28 +05:30
end
2015-10-24 18:46:33 +05:30
def duration
2016-09-13 17:45:13 +05:30
calculate_duration
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
def stuck?
2015-10-24 18:46:33 +05:30
false
end
end