2015-10-06 06:01:16 -04:00
|
|
|
class CommitStatus < ActiveRecord::Base
|
2016-08-24 22:55:32 -04:00
|
|
|
include HasStatus
|
2016-06-13 07:34:36 -04:00
|
|
|
include Importable
|
2016-10-07 08:52:30 -04:00
|
|
|
include AfterCommitQueue
|
2016-03-31 13:51:28 -04:00
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-06-02 06:16:11 -04:00
|
|
|
belongs_to :user
|
2017-03-17 19:06:11 -04:00
|
|
|
belongs_to :project
|
2016-08-11 09:22:35 -04:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2017-04-06 09:32:56 -04:00
|
|
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-06-21 08:43:37 -04:00
|
|
|
delegate :commit, to: :pipeline
|
2017-02-22 17:35:08 -05:00
|
|
|
delegate :sha, :short_sha, to: :pipeline
|
2016-06-21 08:43:37 -04:00
|
|
|
|
2016-06-13 07:34:36 -04:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2017-06-09 02:57:20 -04:00
|
|
|
validates :name, presence: true, unless: :importing?
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2015-10-12 06:15:48 -04:00
|
|
|
alias_attribute :author, :user
|
2017-11-24 10:30:08 -05:00
|
|
|
alias_attribute :pipeline_id, :commit_id
|
2017-06-02 06:16:11 -04:00
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
scope :failed_but_allowed, -> do
|
2017-03-06 08:43:32 -05:00
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
2016-10-03 06:22:51 -04:00
|
|
|
end
|
2016-10-03 06:33:43 -04:00
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
scope :exclude_ignored, -> do
|
2017-03-06 08:43:32 -05:00
|
|
|
# We want to ignore failed but allowed to fail jobs.
|
|
|
|
#
|
|
|
|
# TODO, we also skip ignored optional manual actions.
|
2016-10-04 12:10:23 -04:00
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2017-03-03 08:35:19 -05:00
|
|
|
false, all_state_names - [:failed, :canceled, :manual])
|
2016-10-03 06:22:51 -04:00
|
|
|
end
|
2016-10-03 06:33:43 -04:00
|
|
|
|
2017-05-09 07:14:45 -04:00
|
|
|
scope :latest, -> { where(retried: [false, nil]) }
|
2017-04-16 07:14:39 -04:00
|
|
|
scope :retried, -> { where(retried: true) }
|
2017-02-14 05:38:19 -05:00
|
|
|
scope :ordered, -> { order(:name) }
|
2016-12-05 08:17:42 -05:00
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
|
2017-02-14 05:38:19 -05:00
|
|
|
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2017-08-30 14:20:54 -04:00
|
|
|
enum failure_reason: {
|
2017-08-31 09:03:41 -04:00
|
|
|
unknown_failure: nil,
|
2017-09-05 02:10:34 -04:00
|
|
|
script_failure: 1,
|
2017-08-31 09:03:41 -04:00
|
|
|
api_failure: 2,
|
2017-09-05 02:10:34 -04:00
|
|
|
stuck_or_timeout_failure: 3,
|
2017-09-03 10:35:37 -04:00
|
|
|
runner_system_failure: 4,
|
|
|
|
missing_dependency_failure: 5
|
2017-08-30 14:20:54 -04:00
|
|
|
}
|
|
|
|
|
2017-10-06 05:45:23 -04:00
|
|
|
##
|
|
|
|
# We still create some CommitStatuses outside of CreatePipelineService.
|
|
|
|
#
|
|
|
|
# These are pages deployments and external statuses.
|
|
|
|
#
|
2017-10-11 09:32:19 -04:00
|
|
|
before_create unless: :importing? do
|
|
|
|
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
|
|
|
|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
|
2017-10-06 05:45:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
state_machine :status do
|
2016-08-18 17:36:54 -04:00
|
|
|
event :process do
|
2017-03-03 08:35:19 -05:00
|
|
|
transition [:skipped, :manual] => :created
|
2016-08-18 17:36:54 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 05:33:01 -04:00
|
|
|
event :enqueue do
|
|
|
|
transition [:created, :skipped, :manual] => :pending
|
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
event :skip do
|
|
|
|
transition [:created, :pending] => :skipped
|
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
event :drop do
|
2016-08-11 09:22:35 -04:00
|
|
|
transition [:created, :pending, :running] => :failed
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2016-08-11 09:22:35 -04:00
|
|
|
transition [:created, :pending, :running] => :success
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2017-03-03 08:35:19 -05:00
|
|
|
transition [:created, :pending, :running, :manual] => :canceled
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-10-26 05:34:40 -04:00
|
|
|
before_transition created: [:pending, :running] do |commit_status|
|
|
|
|
commit_status.queued_at = Time.now
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-10-26 05:34:40 -04:00
|
|
|
before_transition [:created, :pending] => :running do |commit_status|
|
|
|
|
commit_status.started_at = Time.now
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-10-26 05:34:40 -04:00
|
|
|
before_transition any => [:success, :failed, :canceled] do |commit_status|
|
|
|
|
commit_status.finished_at = Time.now
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2017-08-30 14:20:54 -04:00
|
|
|
before_transition any => :failed do |commit_status, transition|
|
|
|
|
failure_reason = transition.args.first
|
|
|
|
commit_status.failure_reason = failure_reason
|
|
|
|
end
|
|
|
|
|
2016-09-19 08:13:08 -04:00
|
|
|
after_transition do |commit_status, transition|
|
2017-11-24 10:30:08 -05:00
|
|
|
next unless commit_status.project
|
2016-10-12 09:21:11 -04:00
|
|
|
next if transition.loopback?
|
2016-10-08 14:42:09 -04:00
|
|
|
|
2016-10-08 14:25:16 -04:00
|
|
|
commit_status.run_after_commit do
|
2017-11-24 10:30:08 -05:00
|
|
|
if pipeline_id
|
2017-03-03 08:35:19 -05:00
|
|
|
if complete? || manual?
|
2017-11-24 10:30:08 -05:00
|
|
|
PipelineProcessWorker.perform_async(pipeline_id)
|
2016-10-08 14:25:16 -04:00
|
|
|
else
|
2017-11-24 10:30:08 -05:00
|
|
|
PipelineUpdateWorker.perform_async(pipeline_id)
|
2016-10-08 14:25:16 -04:00
|
|
|
end
|
2016-10-03 10:30:11 -04:00
|
|
|
end
|
2017-06-01 17:36:04 -04:00
|
|
|
|
2017-11-24 10:30:08 -05:00
|
|
|
StageUpdateWorker.perform_async(stage_id)
|
|
|
|
ExpireJobCacheWorker.perform_async(id)
|
2016-09-19 09:03:51 -04:00
|
|
|
end
|
2016-09-19 08:13:08 -04:00
|
|
|
end
|
|
|
|
|
2016-03-08 13:22:50 -05:00
|
|
|
after_transition any => :failed do |commit_status|
|
2017-11-24 10:30:08 -05:00
|
|
|
next unless commit_status.project
|
|
|
|
|
2016-10-08 14:25:16 -04:00
|
|
|
commit_status.run_after_commit do
|
2017-02-22 17:54:59 -05:00
|
|
|
MergeRequests::AddTodoWhenBuildFailsService
|
2017-11-24 10:30:08 -05:00
|
|
|
.new(project, nil).execute(self)
|
2016-10-08 14:25:16 -04:00
|
|
|
end
|
2016-03-08 13:22:50 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2017-03-22 13:22:34 -04:00
|
|
|
def locking_enabled?
|
|
|
|
status_changed?
|
|
|
|
end
|
|
|
|
|
2016-04-16 15:46:26 -04:00
|
|
|
def before_sha
|
2016-06-03 07:09:49 -04:00
|
|
|
pipeline.before_sha || Gitlab::Git::BLANK_SHA
|
2016-04-16 15:46:26 -04:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-09-07 08:52:13 -04:00
|
|
|
def group_name
|
2018-01-27 00:35:53 -05:00
|
|
|
name.to_s.gsub(%r{\d+[\s:/\\]+\d+\s*}, '').strip
|
2016-09-07 08:52:13 -04:00
|
|
|
end
|
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
def failed_but_allowed?
|
2016-03-17 10:54:56 -04:00
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-02-19 13:58:43 -05:00
|
|
|
end
|
|
|
|
|
2016-11-22 00:36:19 -05:00
|
|
|
def duration
|
|
|
|
calculate_duration
|
|
|
|
end
|
|
|
|
|
2016-09-14 09:04:12 -04:00
|
|
|
def playable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-04 06:44:00 -04:00
|
|
|
# To be overriden when inherrited from
|
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
# To be overriden when inherrited from
|
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-11-22 00:36:19 -05:00
|
|
|
def stuck?
|
|
|
|
false
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2015-10-12 09:45:46 -04:00
|
|
|
|
2016-11-22 00:36:19 -05:00
|
|
|
def has_trace?
|
2015-10-12 15:12:31 -04:00
|
|
|
false
|
|
|
|
end
|
2016-12-08 03:51:36 -05:00
|
|
|
|
2017-04-06 09:32:56 -04:00
|
|
|
def auto_canceled?
|
|
|
|
canceled? && auto_canceled_by_id?
|
|
|
|
end
|
|
|
|
|
2016-12-08 11:52:24 -05:00
|
|
|
def detailed_status(current_user)
|
2017-02-22 17:54:59 -05:00
|
|
|
Gitlab::Ci::Status::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
2016-12-08 03:51:36 -05:00
|
|
|
end
|
2016-12-22 15:31:42 -05:00
|
|
|
|
2016-12-27 10:59:42 -05:00
|
|
|
def sortable_name
|
2017-06-13 03:22:22 -04:00
|
|
|
name.to_s.split(/(\d+)/).map do |v|
|
2016-12-22 15:31:42 -05:00
|
|
|
v =~ /\d+/ ? v.to_i : v
|
|
|
|
end
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|