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

150 lines
3.8 KiB
Ruby
Raw Normal View History

2015-10-24 18:46:33 +05:30
class CommitStatus < ActiveRecord::Base
2016-09-29 09:46:39 +05:30
include HasStatus
2016-06-22 15:30:34 +05:30
include Importable
2016-11-03 12:29:30 +05:30
include AfterCommitQueue
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) }
2016-11-03 12:29:30 +05:30
scope :failed_but_allowed, -> do
where(allow_failure: true, status: [:failed, :canceled])
end
scope :exclude_ignored, -> do
quoted_when = connection.quote_column_name('when')
# We want to ignore failed_but_allowed jobs
where("allow_failure = ? OR status IN (?)",
false, all_state_names - [:failed, :canceled]).
# We want to ignore skipped manual jobs
where("#{quoted_when} <> ? OR status <> ?", 'manual', 'skipped').
# We want to ignore skipped on_failure
where("#{quoted_when} <> ? OR status <> ?", 'on_failure', 'skipped')
end
2016-09-29 09:46:39 +05:30
scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
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
after_transition do |commit_status, transition|
2016-11-03 12:29:30 +05:30
next if transition.loopback?
commit_status.run_after_commit do
pipeline.try do |pipeline|
if complete?
PipelineProcessWorker.perform_async(pipeline.id)
else
PipelineUpdateWorker.perform_async(pipeline.id)
end
end
end
2015-12-23 02:04:40 +05:30
end
2016-06-02 11:05:42 +05:30
after_transition any => :failed do |commit_status|
2016-11-03 12:29:30 +05:30
commit_status.run_after_commit do
MergeRequests::AddTodoWhenBuildFailsService
.new(pipeline.project, nil).execute(self)
end
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-09-29 09:46:39 +05:30
def group_name
name.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
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-11-03 12:29:30 +05:30
def failed_but_allowed?
2016-06-02 11:05:42 +05:30
allow_failure? && (failed? || canceled?)
2016-04-02 18:10:28 +05:30
end
2016-09-29 09:46:39 +05:30
def playable?
false
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