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

93 lines
2.4 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
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-06-02 11:05:42 +05:30
belongs_to :commit, class_name: 'Ci::Commit', touch: true
2015-10-24 18:46:33 +05:30
belongs_to :user
validates :commit, presence: true
validates_presence_of :name
alias_attribute :author, :user
2016-06-02 11:05:42 +05:30
scope :latest, -> { where(id: unscope(:select).select('max(id)').group(:name, :commit_id)) }
scope :retried, -> { where.not(id: latest) }
scope :ordered, -> { order(:name) }
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
2015-10-24 18:46:33 +05:30
state_machine :status, initial: :pending do
event :run do
transition pending: :running
end
event :drop do
transition [:pending, :running] => :failed
end
event :success do
transition [:pending, :running] => :success
end
event :cancel do
transition [:pending, :running] => :canceled
end
2016-04-02 18:10:28 +05:30
after_transition pending: :running do |commit_status|
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-04-02 18:10:28 +05:30
after_transition [:pending, :running] => :success do |commit_status|
MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.commit.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.commit.project, nil).execute(commit_status)
end
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
delegate :sha, :short_sha, to: :commit
2015-10-24 18:46:33 +05:30
def before_sha
2016-06-02 11:05:42 +05:30
commit.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-06-02 11:05:42 +05:30
duration =
if started_at && finished_at
finished_at - started_at
elsif started_at
Time.now - started_at
end
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