debian-mirror-gitlab/app/models/bulk_imports/entity.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

225 lines
6 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
2021-01-29 00:20:46 +05:30
# The BulkImport::Entity represents a Group or Project to be imported during the
# bulk import process. An entity is nested under the parent group when it is not
# a top level group.
#
# A full bulk import entity structure might look like this, where the links are
# parents:
#
# **Before Import** **After Import**
#
# GroupEntity Group
# | | | |
# GroupEntity ProjectEntity Group Project
# | |
# ProjectEntity Project
#
# The tree structure of the entities results in the same structure for imported
# Groups and Projects.
2021-01-03 14:25:43 +05:30
class BulkImports::Entity < ApplicationRecord
self.table_name = 'bulk_import_entities'
2022-05-07 20:08:51 +05:30
FailedError = Class.new(StandardError)
2021-01-03 14:25:43 +05:30
belongs_to :bulk_import, optional: false
belongs_to :parent, class_name: 'BulkImports::Entity', optional: true
belongs_to :project, optional: true
2023-05-27 22:25:52 +05:30
belongs_to :group, foreign_key: :namespace_id, optional: true, inverse_of: :bulk_import_entities
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
has_many :trackers,
class_name: 'BulkImports::Tracker',
2023-05-27 22:25:52 +05:30
inverse_of: :entity,
2021-01-29 00:20:46 +05:30
foreign_key: :bulk_import_entity_id
2021-02-22 17:27:13 +05:30
has_many :failures,
class_name: 'BulkImports::Failure',
inverse_of: :entity,
foreign_key: :bulk_import_entity_id
2021-01-03 14:25:43 +05:30
validates :project, absence: true, if: :group
validates :group, absence: true, if: :project
2023-04-23 21:23:45 +05:30
validates :source_type, presence: true
2023-07-09 08:55:56 +05:30
validates :source_full_path, presence: true, format: {
with: Gitlab::Regex.bulk_import_source_full_path_regex,
message: Gitlab::Regex.bulk_import_source_full_path_regex_message
}
2023-04-23 21:23:45 +05:30
2023-07-09 08:55:56 +05:30
validates :destination_name, presence: true, if: -> { group || project }
validates :destination_namespace, exclusion: [nil], if: :group
validates :destination_namespace, presence: true, if: :project?
2021-01-03 14:25:43 +05:30
validate :validate_parent_is_a_group, if: :parent
validate :validate_imported_entity_type
2021-03-11 19:13:27 +05:30
validate :validate_destination_namespace_ascendency, if: :group_entity?
2021-01-03 14:25:43 +05:30
enum source_type: { group_entity: 0, project_entity: 1 }
2021-09-30 23:02:18 +05:30
scope :by_user_id, ->(user_id) { joins(:bulk_import).where(bulk_imports: { user_id: user_id }) }
2022-06-21 17:19:12 +05:30
scope :stale, -> { where('created_at < ?', 8.hours.ago).where(status: [0, 1]) }
2022-08-27 11:52:29 +05:30
scope :by_bulk_import_id, ->(bulk_import_id) { where(bulk_import_id: bulk_import_id) }
2023-03-04 22:38:38 +05:30
scope :order_by_created_at, ->(direction) { order(created_at: direction) }
2021-09-30 23:02:18 +05:30
2022-08-27 11:52:29 +05:30
alias_attribute :destination_slug, :destination_name
2023-07-09 08:55:56 +05:30
delegate :default_project_visibility, :default_group_visibility,
to: :'Gitlab::CurrentSettings.current_application_settings'
2023-04-23 21:23:45 +05:30
2021-01-03 14:25:43 +05:30
state_machine :status, initial: :created do
state :created, value: 0
2021-01-29 00:20:46 +05:30
state :started, value: 1
state :finished, value: 2
2022-06-21 17:19:12 +05:30
state :timeout, value: 3
2021-01-29 00:20:46 +05:30
state :failed, value: -1
event :start do
transition created: :started
end
event :finish do
transition started: :finished
2021-02-22 17:27:13 +05:30
transition failed: :failed
2021-01-29 00:20:46 +05:30
end
event :fail_op do
transition any => :failed
end
2022-06-21 17:19:12 +05:30
event :cleanup_stale do
transition created: :timeout
transition started: :timeout
end
2023-05-27 22:25:52 +05:30
# rubocop:disable Style/SymbolProc
after_transition any => [:finished, :failed, :timeout] do |entity|
entity.update_has_failures
end
# rubocop:enable Style/SymbolProc
2021-01-03 14:25:43 +05:30
end
2021-09-30 23:02:18 +05:30
def self.all_human_statuses
state_machine.states.map(&:human_name)
end
2021-06-08 01:23:25 +05:30
def encoded_source_full_path
ERB::Util.url_encode(source_full_path)
end
2021-11-11 11:23:49 +05:30
def pipelines
@pipelines ||= case source_type
when 'group_entity'
2022-06-21 17:19:12 +05:30
BulkImports::Groups::Stage.new(self).pipelines
2021-11-11 11:23:49 +05:30
when 'project_entity'
2022-06-21 17:19:12 +05:30
BulkImports::Projects::Stage.new(self).pipelines
2021-11-11 11:23:49 +05:30
end
end
def pipeline_exists?(name)
2022-07-23 23:45:48 +05:30
pipelines.any? { _1[:pipeline].to_s == name.to_s }
2021-11-11 11:23:49 +05:30
end
2022-01-26 12:08:38 +05:30
def entity_type
source_type.gsub('_entity', '')
end
2021-11-18 22:05:49 +05:30
def pluralized_name
2022-01-26 12:08:38 +05:30
entity_type.pluralize
end
def base_resource_url_path
"/#{pluralized_name}/#{encoded_source_full_path}"
2021-11-18 22:05:49 +05:30
end
2022-11-25 23:54:43 +05:30
def base_xid_resource_url_path
"/#{pluralized_name}/#{source_xid}"
end
def base_resource_path
if source_xid.present?
base_xid_resource_url_path
else
base_resource_url_path
end
end
2021-11-18 22:05:49 +05:30
def export_relations_url_path
2022-11-25 23:54:43 +05:30
"#{base_resource_path}/export_relations"
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
def relation_download_url_path(relation)
"#{export_relations_url_path}/download?relation=#{relation}"
end
2022-01-26 12:08:38 +05:30
def wikis_url_path
2022-11-25 23:54:43 +05:30
"#{base_resource_path}/wikis"
2022-01-26 12:08:38 +05:30
end
def project?
source_type == 'project_entity'
end
def group?
source_type == 'group_entity'
end
def update_service
"::#{pluralized_name.capitalize}::UpdateService".constantize
end
2023-03-17 16:20:25 +05:30
def full_path
project? ? project&.full_path : group&.full_path
end
2023-04-23 21:23:45 +05:30
def default_visibility_level
return default_group_visibility if group?
default_project_visibility
end
2023-05-27 22:25:52 +05:30
def update_has_failures
return if has_failures
return unless failures.any?
update!(has_failures: true)
end
2021-01-03 14:25:43 +05:30
private
def validate_parent_is_a_group
unless parent.group_entity?
errors.add(:parent, s_('BulkImport|must be a group'))
end
end
def validate_imported_entity_type
if group.present? && project_entity?
2021-01-29 00:20:46 +05:30
errors.add(
:group,
s_('BulkImport|expected an associated Project but has an associated Group')
)
2021-01-03 14:25:43 +05:30
end
if project.present? && group_entity?
2021-01-29 00:20:46 +05:30
errors.add(
:project,
s_('BulkImport|expected an associated Group but has an associated Project')
)
2021-01-03 14:25:43 +05:30
end
end
2021-03-11 19:13:27 +05:30
def validate_destination_namespace_ascendency
source = Group.find_by_full_path(source_full_path)
return unless source
if source.self_and_descendants.any? { |namespace| namespace.full_path == destination_namespace }
errors.add(
2021-04-17 20:07:23 +05:30
:base,
s_('BulkImport|Import failed: Destination cannot be a subgroup of the source group. Change the destination and try again.')
2021-03-11 19:13:27 +05:30
)
end
end
2021-01-03 14:25:43 +05:30
end