2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class CommitStatus < ApplicationRecord
|
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
|
2018-05-15 14:03:09 -04:00
|
|
|
include Presentable
|
2018-05-31 14:32:36 -04:00
|
|
|
include EnumWithNil
|
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
|
2018-09-21 02:42:29 -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) }
|
2019-01-24 09:18:47 -05:00
|
|
|
scope :processables, -> { where(type: %w[Ci::Build Ci::Bridge]) }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2018-11-13 11:29:14 -05: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 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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-11 09:32:19 -04:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-10-06 05:45:23 -04:00
|
|
|
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
|
2019-03-03 18:56:20 -05: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 05:33:01 -04:00
|
|
|
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
|
2019-02-26 21:13:06 -05:00
|
|
|
transition [:created, :preparing, :pending] => :skipped
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
event :drop do
|
2019-02-26 21:13:06 -05:00
|
|
|
transition [:created, :preparing, :pending, :running, :scheduled] => :failed
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2019-02-26 21:13:06 -05:00
|
|
|
transition [:created, :preparing, :pending, :running] => :success
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2019-02-26 21:13:06 -05:00
|
|
|
transition [:created, :preparing, :pending, :running, :manual, :scheduled] => :canceled
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2019-02-26 21:13:06 -05:00
|
|
|
before_transition [:created, :preparing, :skipped, :manual, :scheduled] => :pending do |commit_status|
|
2016-10-26 05:34:40 -04:00
|
|
|
commit_status.queued_at = Time.now
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2019-02-26 21:13:06 -05:00
|
|
|
before_transition [:created, :preparing, :pending] => :running do |commit_status|
|
2016-10-26 05:34:40 -04:00
|
|
|
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
|
2018-10-26 04:01:28 -04:00
|
|
|
commit_status.failure_reason = CommitStatus.failure_reasons[failure_reason]
|
2017-08-30 14:20:54 -04:00
|
|
|
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
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
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-04-04 08:10:22 -04: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
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-10-23 06:58:41 -04:00
|
|
|
def archived?
|
|
|
|
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
|
|
|
|
2019-03-03 18:56:20 -05:00
|
|
|
def any_unmet_prerequisites?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
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
|