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
|
|
|
|
validates :source_type, :source_full_path, :destination_name,
|
|
|
|
:destination_namespace, presence: true
|
|
|
|
|
|
|
|
validate :validate_parent_is_a_group, if: :parent
|
|
|
|
validate :validate_imported_entity_type
|
|
|
|
|
|
|
|
enum source_type: { group_entity: 0, project_entity: 1 }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-11-19 19:09:06 -05:00
|
|
|
def update_tracker_for(relation:, has_next_page:, next_page: nil)
|
|
|
|
attributes = {
|
|
|
|
relation: relation,
|
|
|
|
has_next_page: has_next_page,
|
|
|
|
next_page: next_page,
|
|
|
|
bulk_import_entity_id: id
|
|
|
|
}
|
|
|
|
|
|
|
|
trackers.upsert(attributes, unique_by: %i[bulk_import_entity_id relation])
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_next_page?(relation)
|
|
|
|
trackers.find_by(relation: relation)&.has_next_page
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_page_for(relation)
|
|
|
|
trackers.find_by(relation: relation)&.next_page
|
|
|
|
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
|
|
|
|
end
|