2015-10-06 10:01:16 +00:00
|
|
|
class CommitStatus < ActiveRecord::Base
|
2016-08-25 02:55:32 +00:00
|
|
|
include HasStatus
|
2016-06-13 11:34:36 +00:00
|
|
|
include Importable
|
2016-10-07 12:52:30 +00:00
|
|
|
include AfterCommitQueue
|
2016-03-31 17:51:28 +00:00
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2016-10-18 21:20:36 +00:00
|
|
|
belongs_to :project, foreign_key: :gl_project_id
|
2016-08-11 13:22:35 +00:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2015-10-06 10:01:16 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-06-21 12:43:37 +00:00
|
|
|
delegate :commit, to: :pipeline
|
|
|
|
|
2016-06-13 11:34:36 +00:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2015-10-06 10:01:16 +00:00
|
|
|
|
|
|
|
validates_presence_of :name
|
|
|
|
|
2015-10-12 10:15:48 +00:00
|
|
|
alias_attribute :author, :user
|
|
|
|
|
2016-07-20 17:10:08 +00:00
|
|
|
scope :latest, -> do
|
|
|
|
max_id = unscope(:select).select("max(#{quoted_table_name}.id)")
|
|
|
|
|
|
|
|
where(id: max_id.group(:name, :commit_id))
|
|
|
|
end
|
2016-08-10 14:45:30 +00:00
|
|
|
|
2016-05-16 21:34:42 +00:00
|
|
|
scope :retried, -> { where.not(id: latest) }
|
2016-05-09 21:58:53 +00:00
|
|
|
scope :ordered, -> { order(:name) }
|
2016-10-03 10:33:43 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :failed_but_allowed, -> do
|
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
|
|
|
end
|
2016-10-03 10:33:43 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :exclude_ignored, -> do
|
2016-10-03 10:25:18 +00:00
|
|
|
# We want to ignore failed_but_allowed jobs
|
2016-10-04 16:10:23 +00:00
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2016-12-05 16:52:50 +00:00
|
|
|
false, all_state_names - [:failed, :canceled])
|
2016-10-03 10:22:51 +00:00
|
|
|
end
|
2016-10-03 10:33:43 +00:00
|
|
|
|
2016-12-05 13:17:42 +00:00
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
state_machine :status do
|
2016-08-12 11:57:58 +00:00
|
|
|
event :enqueue do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :skipped] => :pending
|
2016-07-18 12:57:24 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 21:36:54 +00:00
|
|
|
event :process do
|
|
|
|
transition skipped: :created
|
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
event :skip do
|
|
|
|
transition [:created, :pending] => :skipped
|
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :drop do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :failed
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :success
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :canceled
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-10-26 09:34:40 +00:00
|
|
|
before_transition created: [:pending, :running] do |commit_status|
|
|
|
|
commit_status.queued_at = Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-10-26 09:34:40 +00:00
|
|
|
before_transition [:created, :pending] => :running do |commit_status|
|
|
|
|
commit_status.started_at = Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-10-26 09:34:40 +00:00
|
|
|
before_transition any => [:success, :failed, :canceled] do |commit_status|
|
|
|
|
commit_status.finished_at = Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-09-19 12:13:08 +00:00
|
|
|
after_transition do |commit_status, transition|
|
2016-10-12 13:21:11 +00:00
|
|
|
next if transition.loopback?
|
2016-10-08 18:42:09 +00:00
|
|
|
|
2016-10-08 18:25:16 +00:00
|
|
|
commit_status.run_after_commit do
|
|
|
|
pipeline.try do |pipeline|
|
|
|
|
if complete?
|
2016-10-12 09:52:54 +00:00
|
|
|
PipelineProcessWorker.perform_async(pipeline.id)
|
2016-10-08 18:25:16 +00:00
|
|
|
else
|
2016-10-12 09:52:54 +00:00
|
|
|
PipelineUpdateWorker.perform_async(pipeline.id)
|
2016-10-08 18:25:16 +00:00
|
|
|
end
|
2016-10-03 14:30:11 +00:00
|
|
|
end
|
2016-09-19 13:03:51 +00:00
|
|
|
end
|
2016-09-19 12:13:08 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:22:50 +00:00
|
|
|
after_transition any => :failed do |commit_status|
|
2016-10-08 18:25:16 +00:00
|
|
|
commit_status.run_after_commit do
|
|
|
|
MergeRequests::AddTodoWhenBuildFailsService
|
|
|
|
.new(pipeline.project, nil).execute(self)
|
|
|
|
end
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-06-03 11:34:38 +00:00
|
|
|
delegate :sha, :short_sha, to: :pipeline
|
2016-04-16 19:46:26 +00:00
|
|
|
|
|
|
|
def before_sha
|
2016-06-03 11:09:49 +00:00
|
|
|
pipeline.before_sha || Gitlab::Git::BLANK_SHA
|
2016-04-16 19:46:26 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2016-09-07 12:52:13 +00:00
|
|
|
def group_name
|
2016-09-14 13:04:12 +00:00
|
|
|
name.gsub(/\d+[\s:\/\\]+\d+\s*/, '').strip
|
2016-09-07 12:52:13 +00:00
|
|
|
end
|
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
def failed_but_allowed?
|
2016-03-17 14:54:56 +00:00
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-02-19 18:58:43 +00:00
|
|
|
end
|
|
|
|
|
2016-11-22 05:36:19 +00:00
|
|
|
def duration
|
|
|
|
calculate_duration
|
|
|
|
end
|
|
|
|
|
2016-09-14 13:04:12 +00:00
|
|
|
def playable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-11-22 05:36:19 +00:00
|
|
|
def stuck?
|
|
|
|
false
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
2015-10-12 13:45:46 +00:00
|
|
|
|
2016-11-22 05:36:19 +00:00
|
|
|
def has_trace?
|
2015-10-12 19:12:31 +00:00
|
|
|
false
|
|
|
|
end
|
2016-12-08 08:51:36 +00:00
|
|
|
|
|
|
|
def detailed_status
|
|
|
|
Gitlab::Ci::Status::Factory.new(self).fabricate!
|
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|