2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class CommitStatus < ApplicationRecord
|
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
|
2018-05-15 18:03:09 +00:00
|
|
|
include Presentable
|
2018-05-31 18:32:36 +00:00
|
|
|
include EnumWithNil
|
2016-03-31 17:51:28 +00:00
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-06-02 10:16:11 +00:00
|
|
|
belongs_to :user
|
2017-03-17 23:06:11 +00:00
|
|
|
belongs_to :project
|
2016-08-11 13:22:35 +00:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2017-04-06 13:32:56 +00:00
|
|
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2016-06-21 12:43:37 +00:00
|
|
|
delegate :commit, to: :pipeline
|
2017-02-22 22:35:08 +00:00
|
|
|
delegate :sha, :short_sha, to: :pipeline
|
2016-06-21 12:43:37 +00:00
|
|
|
|
2016-06-13 11:34:36 +00:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2017-06-09 06:57:20 +00:00
|
|
|
validates :name, presence: true, unless: :importing?
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2015-10-12 10:15:48 +00:00
|
|
|
alias_attribute :author, :user
|
2017-11-24 15:30:08 +00:00
|
|
|
alias_attribute :pipeline_id, :commit_id
|
2017-06-02 10:16:11 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :failed_but_allowed, -> do
|
2017-03-06 13:43:32 +00:00
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
2016-10-03 10:22:51 +00:00
|
|
|
end
|
2018-09-21 06:42:29 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :exclude_ignored, -> do
|
2017-03-06 13:43:32 +00:00
|
|
|
# We want to ignore failed but allowed to fail jobs.
|
|
|
|
#
|
|
|
|
# TODO, we also skip ignored optional manual actions.
|
2016-10-04 16:10:23 +00:00
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2017-03-03 13:35:19 +00:00
|
|
|
false, all_state_names - [:failed, :canceled, :manual])
|
2016-10-03 10:22:51 +00:00
|
|
|
end
|
2016-10-03 10:33:43 +00:00
|
|
|
|
2017-05-09 11:14:45 +00:00
|
|
|
scope :latest, -> { where(retried: [false, nil]) }
|
2017-04-16 11:14:39 +00:00
|
|
|
scope :retried, -> { where(retried: true) }
|
2017-02-14 10:38:19 +00:00
|
|
|
scope :ordered, -> { order(:name) }
|
2016-12-05 13:17:42 +00:00
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
|
2017-02-14 10:38:19 +00:00
|
|
|
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
|
2019-01-24 14:18:47 +00:00
|
|
|
scope :processables, -> { where(type: %w[Ci::Build Ci::Bridge]) }
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2018-11-13 16:29:14 +00:00
|
|
|
# We use `CommitStatusEnums.failure_reasons` here so that EE can more easily
|
|
|
|
# extend this `Hash` with new values.
|
|
|
|
enum_with_nil failure_reason: ::CommitStatusEnums.failure_reasons
|
2017-08-30 18:20:54 +00:00
|
|
|
|
2017-10-06 09:45:23 +00:00
|
|
|
##
|
|
|
|
# We still create some CommitStatuses outside of CreatePipelineService.
|
|
|
|
#
|
|
|
|
# These are pages deployments and external statuses.
|
|
|
|
#
|
2017-10-11 13:32:19 +00:00
|
|
|
before_create unless: :importing? do
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-11 13:32:19 +00:00
|
|
|
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
|
|
|
|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
|
2017-10-06 09:45:23 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-10-06 09:45:23 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
state_machine :status do
|
2016-08-18 21:36:54 +00:00
|
|
|
event :process do
|
2017-03-03 13:35:19 +00:00
|
|
|
transition [:skipped, :manual] => :created
|
2016-08-18 21:36:54 +00:00
|
|
|
end
|
|
|
|
|
2017-07-24 09:33:01 +00:00
|
|
|
event :enqueue do
|
2019-03-03 23:56:20 +00:00
|
|
|
# A CommitStatus will never have prerequisites, but this event
|
|
|
|
# is shared by Ci::Build, which cannot progress unless prerequisites
|
|
|
|
# are satisfied.
|
|
|
|
transition [:created, :preparing, :skipped, :manual, :scheduled] => :pending, unless: :any_unmet_prerequisites?
|
2017-07-24 09:33:01 +00:00
|
|
|
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
|
2019-02-27 02:13:06 +00:00
|
|
|
transition [:created, :preparing, :pending] => :skipped
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :drop do
|
2019-02-27 02:13:06 +00:00
|
|
|
transition [:created, :preparing, :pending, :running, :scheduled] => :failed
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2019-02-27 02:13:06 +00:00
|
|
|
transition [:created, :preparing, :pending, :running] => :success
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2019-02-27 02:13:06 +00:00
|
|
|
transition [:created, :preparing, :pending, :running, :manual, :scheduled] => :canceled
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2019-02-27 02:13:06 +00:00
|
|
|
before_transition [:created, :preparing, :skipped, :manual, :scheduled] => :pending do |commit_status|
|
2016-10-26 09:34:40 +00:00
|
|
|
commit_status.queued_at = Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2019-02-27 02:13:06 +00:00
|
|
|
before_transition [:created, :preparing, :pending] => :running do |commit_status|
|
2016-10-26 09:34:40 +00:00
|
|
|
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
|
|
|
|
|
2017-08-30 18:20:54 +00:00
|
|
|
before_transition any => :failed do |commit_status, transition|
|
|
|
|
failure_reason = transition.args.first
|
2018-10-26 08:01:28 +00:00
|
|
|
commit_status.failure_reason = CommitStatus.failure_reasons[failure_reason]
|
2017-08-30 18:20:54 +00:00
|
|
|
end
|
|
|
|
|
2016-09-19 12:13:08 +00:00
|
|
|
after_transition do |commit_status, transition|
|
2017-11-24 15:30:08 +00:00
|
|
|
next unless commit_status.project
|
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
|
2017-11-24 15:30:08 +00:00
|
|
|
if pipeline_id
|
2017-03-03 13:35:19 +00:00
|
|
|
if complete? || manual?
|
2017-11-24 15:30:08 +00:00
|
|
|
PipelineProcessWorker.perform_async(pipeline_id)
|
2016-10-08 18:25:16 +00:00
|
|
|
else
|
2017-11-24 15:30:08 +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
|
2017-06-01 21:36:04 +00:00
|
|
|
|
2017-11-24 15:30:08 +00:00
|
|
|
StageUpdateWorker.perform_async(stage_id)
|
|
|
|
ExpireJobCacheWorker.perform_async(id)
|
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|
|
2017-11-24 15:30:08 +00:00
|
|
|
next unless commit_status.project
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2016-10-08 18:25:16 +00:00
|
|
|
commit_status.run_after_commit do
|
2017-02-22 22:54:59 +00:00
|
|
|
MergeRequests::AddTodoWhenBuildFailsService
|
2017-11-24 15:30:08 +00:00
|
|
|
.new(project, nil).execute(self)
|
2016-10-08 18:25:16 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2017-03-22 17:22:34 +00:00
|
|
|
def locking_enabled?
|
2019-04-23 09:30:18 +00:00
|
|
|
will_save_change_to_status?
|
2017-03-22 17:22:34 +00:00
|
|
|
end
|
|
|
|
|
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
|
2018-04-04 12:10:22 +00:00
|
|
|
name.to_s.gsub(%r{\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
|
|
|
|
|
2017-06-04 10:44:00 +00:00
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-12 09:20:19 +00:00
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-10-23 10:58:41 +00:00
|
|
|
def archived?
|
|
|
|
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
|
|
|
|
2019-03-03 23:56:20 +00:00
|
|
|
def any_unmet_prerequisites?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:32:56 +00:00
|
|
|
def auto_canceled?
|
|
|
|
canceled? && auto_canceled_by_id?
|
|
|
|
end
|
|
|
|
|
2016-12-08 16:52:24 +00:00
|
|
|
def detailed_status(current_user)
|
2017-02-22 22:54:59 +00:00
|
|
|
Gitlab::Ci::Status::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
2016-12-08 08:51:36 +00:00
|
|
|
end
|
2016-12-22 20:31:42 +00:00
|
|
|
|
2016-12-27 15:59:42 +00:00
|
|
|
def sortable_name
|
2017-06-13 07:22:22 +00:00
|
|
|
name.to_s.split(/(\d+)/).map do |v|
|
2016-12-22 20:31:42 +00:00
|
|
|
v =~ /\d+/ ? v.to_i : v
|
|
|
|
end
|
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|