2020-10-15 11:08:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-10-21 17:09:00 -04:00
|
|
|
# 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.
|
2020-10-20 11:08:57 -04:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2020-10-21 17:09:00 -04:00
|
|
|
# The tree structure of the entities results in the same structure for imported
|
|
|
|
# Groups and Projects.
|
2020-10-15 11:08:45 -04:00
|
|
|
class BulkImports::Entity < ApplicationRecord
|
|
|
|
self.table_name = 'bulk_import_entities'
|
|
|
|
|
|
|
|
belongs_to :bulk_import, optional: false
|
|
|
|
belongs_to :parent, class_name: 'BulkImports::Entity', optional: true
|
|
|
|
|
|
|
|
belongs_to :project, optional: true
|
|
|
|
belongs_to :group, foreign_key: :namespace_id, optional: true
|
|
|
|
|
2020-11-12 10:09:09 -05:00
|
|
|
has_many :trackers,
|
|
|
|
class_name: 'BulkImports::Tracker',
|
|
|
|
foreign_key: :bulk_import_entity_id
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
has_many :failures,
|
|
|
|
class_name: 'BulkImports::Failure',
|
|
|
|
inverse_of: :entity,
|
|
|
|
foreign_key: :bulk_import_entity_id
|
|
|
|
|
2020-10-15 11:08:45 -04:00
|
|
|
validates :project, absence: true, if: :group
|
|
|
|
validates :group, absence: true, if: :project
|
2021-02-22 07:10:38 -05:00
|
|
|
validates :source_type, :source_full_path, :destination_name, presence: true
|
|
|
|
validates :destination_namespace, exclusion: [nil], if: :group
|
|
|
|
validates :destination_namespace, presence: true, if: :project
|
2020-10-15 11:08:45 -04:00
|
|
|
|
|
|
|
validate :validate_parent_is_a_group, if: :parent
|
|
|
|
validate :validate_imported_entity_type
|
|
|
|
|
2021-02-02 19:09:41 -05:00
|
|
|
validate :validate_destination_namespace_ascendency, if: :group_entity?
|
|
|
|
|
2020-10-15 11:08:45 -04:00
|
|
|
enum source_type: { group_entity: 0, project_entity: 1 }
|
|
|
|
|
2021-07-05 08:08:33 -04:00
|
|
|
scope :by_user_id, ->(user_id) { joins(:bulk_import).where(bulk_imports: { user_id: user_id }) }
|
|
|
|
|
2020-10-15 11:08:45 -04:00
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 0
|
2020-10-23 11:08:42 -04:00
|
|
|
state :started, value: 1
|
|
|
|
state :finished, value: 2
|
|
|
|
state :failed, value: -1
|
|
|
|
|
|
|
|
event :start do
|
|
|
|
transition created: :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
2020-11-30 10:09:21 -05:00
|
|
|
transition failed: :failed
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :fail_op do
|
|
|
|
transition any => :failed
|
|
|
|
end
|
2020-10-15 11:08:45 -04:00
|
|
|
end
|
|
|
|
|
2021-07-05 08:08:33 -04:00
|
|
|
def self.all_human_statuses
|
|
|
|
state_machine.states.map(&:human_name)
|
|
|
|
end
|
|
|
|
|
2021-05-11 11:10:20 -04:00
|
|
|
def encoded_source_full_path
|
|
|
|
ERB::Util.url_encode(source_full_path)
|
|
|
|
end
|
|
|
|
|
2021-09-13 05:11:26 -04:00
|
|
|
def pipelines
|
|
|
|
@pipelines ||= case source_type
|
|
|
|
when 'group_entity'
|
2021-10-13 08:12:20 -04:00
|
|
|
BulkImports::Groups::Stage.new(bulk_import).pipelines
|
2021-09-13 05:11:26 -04:00
|
|
|
when 'project_entity'
|
2021-10-13 08:12:20 -04:00
|
|
|
BulkImports::Projects::Stage.new(bulk_import).pipelines
|
2021-09-13 05:11:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_exists?(name)
|
|
|
|
pipelines.any? { |_, pipeline| pipeline.to_s == name.to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_pipeline_trackers!
|
|
|
|
self.class.transaction do
|
|
|
|
pipelines.each do |stage, pipeline|
|
|
|
|
trackers.create!(
|
|
|
|
stage: stage,
|
|
|
|
pipeline_name: pipeline
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-24 10:14:19 -05:00
|
|
|
def entity_type
|
|
|
|
source_type.gsub('_entity', '')
|
|
|
|
end
|
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
def pluralized_name
|
2021-11-24 10:14:19 -05:00
|
|
|
entity_type.pluralize
|
2021-09-27 14:12:38 -04:00
|
|
|
end
|
|
|
|
|
2021-12-01 04:10:25 -05:00
|
|
|
def base_resource_url_path
|
|
|
|
"/#{pluralized_name}/#{encoded_source_full_path}"
|
|
|
|
end
|
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
def export_relations_url_path
|
2021-12-01 04:10:25 -05:00
|
|
|
"#{base_resource_url_path}/export_relations"
|
2021-09-27 14:12:38 -04:00
|
|
|
end
|
|
|
|
|
2021-10-19 14:13:24 -04:00
|
|
|
def relation_download_url_path(relation)
|
|
|
|
"#{export_relations_url_path}/download?relation=#{relation}"
|
|
|
|
end
|
|
|
|
|
2021-12-01 04:10:25 -05:00
|
|
|
def wikis_url_path
|
|
|
|
"#{base_resource_url_path}/wikis"
|
|
|
|
end
|
|
|
|
|
2021-11-24 10:14:19 -05:00
|
|
|
def project?
|
|
|
|
source_type == 'project_entity'
|
|
|
|
end
|
|
|
|
|
|
|
|
def group?
|
|
|
|
source_type == 'group_entity'
|
|
|
|
end
|
|
|
|
|
2021-12-03 07:10:23 -05:00
|
|
|
def update_service
|
|
|
|
"::#{pluralized_name.capitalize}::UpdateService".constantize
|
|
|
|
end
|
|
|
|
|
2020-10-15 11:08:45 -04:00
|
|
|
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?
|
2020-10-20 11:08:57 -04:00
|
|
|
errors.add(
|
|
|
|
:group,
|
|
|
|
s_('BulkImport|expected an associated Project but has an associated Group')
|
|
|
|
)
|
2020-10-15 11:08:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if project.present? && group_entity?
|
2020-10-20 11:08:57 -04:00
|
|
|
errors.add(
|
|
|
|
:project,
|
|
|
|
s_('BulkImport|expected an associated Group but has an associated Project')
|
|
|
|
)
|
2020-10-15 11:08:45 -04:00
|
|
|
end
|
|
|
|
end
|
2021-02-02 19:09:41 -05:00
|
|
|
|
|
|
|
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-02-25 10:11:34 -05:00
|
|
|
:base,
|
|
|
|
s_('BulkImport|Import failed: Destination cannot be a subgroup of the source group. Change the destination and try again.')
|
2021-02-02 19:09:41 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-10-15 11:08:45 -04:00
|
|
|
end
|