debian-mirror-gitlab/lib/gitlab/import_export/project_tree_restorer.rb

246 lines
9 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class ProjectTreeRestorer
2018-11-08 19:23:39 +05:30
# Relations which cannot be saved at project level (and have a group assigned)
GROUP_MODELS = [GroupLabel, Milestone].freeze
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
attr_reader :user
attr_reader :shared
attr_reader :project
2016-06-22 15:30:34 +05:30
def initialize(user:, shared:, project:)
@path = File.join(shared.export_path, 'project.json')
@user = user
@shared = shared
@project = project
2018-03-17 18:26:18 +05:30
@saved = true
2016-06-22 15:30:34 +05:30
end
def restore
2016-11-03 12:29:30 +05:30
begin
2019-12-21 20:55:43 +05:30
@tree_hash = read_tree_hash
2016-11-03 12:29:30 +05:30
rescue => e
2019-09-30 21:07:59 +05:30
Rails.logger.error("Import/Export error: #{e.message}") # rubocop:disable Gitlab/RailsLogger
2016-11-03 12:29:30 +05:30
raise Gitlab::ImportExport::Error.new('Incorrect JSON format')
end
2016-06-22 15:30:34 +05:30
@project_members = @tree_hash.delete('project_members')
2016-08-24 12:49:21 +05:30
2019-02-15 15:39:39 +05:30
RelationRenameService.rename(@tree_hash)
2018-03-17 18:26:18 +05:30
ActiveRecord::Base.uncached do
ActiveRecord::Base.no_touching do
2019-12-21 20:55:43 +05:30
update_project_params!
2018-03-17 18:26:18 +05:30
create_relations
end
2016-08-24 12:49:21 +05:30
end
2019-12-21 20:55:43 +05:30
# ensure that we have latest version of the restore
@project.reload # rubocop:disable Cop/ActiveRecordAssociationReload
true
2016-06-22 15:30:34 +05:30
rescue => e
@shared.error(e)
false
end
2019-12-21 20:55:43 +05:30
private
2018-05-09 12:01:36 +05:30
2019-12-21 20:55:43 +05:30
def read_tree_hash
json = IO.read(@path)
ActiveSupport::JSON.decode(json)
2016-06-22 15:30:34 +05:30
end
def members_mapper
@members_mapper ||= Gitlab::ImportExport::MembersMapper.new(exported_members: @project_members,
user: @user,
2019-12-21 20:55:43 +05:30
project: @project)
end
# A Hash of the imported merge request ID -> imported ID.
def merge_requests_mapping
@merge_requests_mapping ||= {}
2016-06-22 15:30:34 +05:30
end
# Loops through the tree of models defined in import_export.yml and
# finds them in the imported JSON so they can be instantiated and saved
# in the DB. The structure and relationships between models are guessed from
# the configuration yaml file too.
# Finally, it updates each attribute in the newly imported project.
def create_relations
2019-12-21 20:55:43 +05:30
project_relations.each do |relation_key, relation_definition|
2019-12-04 20:38:33 +05:30
relation_key_s = relation_key.to_s
if relation_definition.present?
create_sub_relations(relation_key_s, relation_definition, @tree_hash)
elsif @tree_hash[relation_key_s].present?
save_relation_hash(relation_key_s, @tree_hash[relation_key_s])
2018-03-17 18:26:18 +05:30
end
end
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
@project.merge_requests.set_latest_merge_request_diff_ids!
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
@saved
end
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
def save_relation_hash(relation_key, relation_hash_batch)
2018-03-17 18:26:18 +05:30
relation_hash = create_relation(relation_key, relation_hash_batch)
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
remove_group_models(relation_hash) if relation_hash.is_a?(Array)
2019-12-21 20:55:43 +05:30
@saved = false unless @project.append_or_update_attribute(relation_key, relation_hash)
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
save_id_mappings(relation_key, relation_hash_batch, relation_hash)
@project.reset
end
# Older, serialized CI pipeline exports may only have a
# merge_request_id and not the full hash of the merge request. To
# import these pipelines, we need to preserve the mapping between
# the old and new the merge request ID.
def save_id_mappings(relation_key, relation_hash_batch, relation_hash)
return unless relation_key == 'merge_requests'
relation_hash = Array(relation_hash)
Array(relation_hash_batch).each_with_index do |raw_data, index|
merge_requests_mapping[raw_data['id']] = relation_hash[index]['id']
end
2016-06-22 15:30:34 +05:30
end
2018-11-08 19:23:39 +05:30
# Remove project models that became group models as we found them at group level.
# This no longer required saving them at the root project level.
# For example, in the case of an existing group label that matched the title.
def remove_group_models(relation_hash)
relation_hash.reject! do |value|
GROUP_MODELS.include?(value.class) && value.group_id
end
end
2019-12-21 20:55:43 +05:30
def remove_feature_dependent_sub_relations!(_relation_item)
# no-op
2019-12-04 20:38:33 +05:30
end
def project_relations
2019-12-21 20:55:43 +05:30
@project_relations ||= reader.attributes_finder.find_relations_tree(:project)
2016-06-22 15:30:34 +05:30
end
2019-12-21 20:55:43 +05:30
def update_project_params!
2018-11-20 20:47:30 +05:30
Gitlab::Timeless.timeless(@project) do
2019-12-21 20:55:43 +05:30
project_params = @tree_hash.reject do |key, value|
project_relations.include?(key.to_sym)
end
2016-06-22 15:30:34 +05:30
2019-12-21 20:55:43 +05:30
project_params = project_params.merge(present_project_override_params)
2018-06-03 19:52:53 +05:30
# Cleaning all imported and overridden params
2019-12-21 20:55:43 +05:30
project_params = Gitlab::ImportExport::AttributeCleaner.clean(
relation_hash: project_params,
relation_class: Project,
excluded_keys: excluded_keys_for_relation(:project))
@project.assign_attributes(project_params)
@project.drop_visibility_level!
@project.save!
2016-10-01 15:18:49 +05:30
end
end
2019-12-21 20:55:43 +05:30
def present_project_override_params
# we filter out the empty strings from the overrides
# keeping the default values configured
project_override_params.transform_values do |value|
value.is_a?(String) ? value.presence : value
end.compact
2019-02-02 18:00:53 +05:30
end
2019-12-21 20:55:43 +05:30
def project_override_params
@project_override_params ||= @project.import_data&.data&.fetch('override_params', nil) || {}
2019-12-04 20:38:33 +05:30
end
2016-06-22 15:30:34 +05:30
# Given a relation hash containing one or more models and its relationships,
# loops through each model and each object from a model type and
# and assigns its correspondent attributes hash from +tree_hash+
# Example:
# +relation_key+ issues, loops through the list of *issues* and for each individual
# issue, finds any subrelations such as notes, creates them and assign them back to the hash
2016-08-24 12:49:21 +05:30
#
# Recursively calls this method if the sub-relation is a hash containing more sub-relations
2019-12-04 20:38:33 +05:30
def create_sub_relations(relation_key, relation_definition, tree_hash, save: true)
2016-08-24 12:49:21 +05:30
return if tree_hash[relation_key].blank?
2018-03-17 18:26:18 +05:30
tree_array = [tree_hash[relation_key]].flatten
# Avoid keeping a possible heavy object in memory once we are done with it
2019-12-21 20:55:43 +05:30
while relation_item = tree_array.shift
remove_feature_dependent_sub_relations!(relation_item)
2018-11-20 20:47:30 +05:30
2018-03-17 18:26:18 +05:30
# The transaction at this level is less speedy than one single transaction
# But we can't have it in the upper level or GC won't get rid of the AR objects
# after we save the batch.
Project.transaction do
2019-12-04 20:38:33 +05:30
process_sub_relation(relation_key, relation_definition, relation_item)
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
# For every subrelation that hangs from Project, save the associated records altogether
2018-03-17 18:26:18 +05:30
# This effectively batches all records per subrelation item, only keeping those in memory
# We have to keep in mind that more batch granularity << Memory, but >> Slowness
if save
2019-12-04 20:38:33 +05:30
save_relation_hash(relation_key, [relation_item])
2018-03-17 18:26:18 +05:30
tree_hash[relation_key].delete(relation_item)
end
end
end
tree_hash.delete(relation_key) if save
end
2016-08-24 12:49:21 +05:30
2019-12-04 20:38:33 +05:30
def process_sub_relation(relation_key, relation_definition, relation_item)
relation_definition.each do |sub_relation_key, sub_relation_definition|
2018-03-17 18:26:18 +05:30
# We just use author to get the user ID, do not attempt to create an instance.
2019-12-04 20:38:33 +05:30
next if sub_relation_key == :author
2016-08-24 12:49:21 +05:30
2019-12-04 20:38:33 +05:30
sub_relation_key_s = sub_relation_key.to_s
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
# create dependent relations if present
if sub_relation_definition.present?
create_sub_relations(sub_relation_key_s, sub_relation_definition, relation_item, save: false)
end
2016-06-22 15:30:34 +05:30
2019-12-04 20:38:33 +05:30
# transform relation hash to actual object
sub_relation_hash = relation_item[sub_relation_key_s]
if sub_relation_hash.present?
relation_item[sub_relation_key_s] = create_relation(sub_relation_key, sub_relation_hash)
end
2016-06-22 15:30:34 +05:30
end
end
2019-12-04 20:38:33 +05:30
def create_relation(relation_key, relation_hash_list)
2016-06-22 15:30:34 +05:30
relation_array = [relation_hash_list].flatten.map do |relation_hash|
2019-12-04 20:38:33 +05:30
Gitlab::ImportExport::RelationFactory.create(
relation_sym: relation_key.to_sym,
relation_hash: relation_hash,
members_mapper: members_mapper,
2019-12-21 20:55:43 +05:30
merge_requests_mapping: merge_requests_mapping,
2019-12-04 20:38:33 +05:30
user: @user,
2019-12-21 20:55:43 +05:30
project: @project,
2019-12-04 20:38:33 +05:30
excluded_keys: excluded_keys_for_relation(relation_key))
2017-08-17 22:00:37 +05:30
end.compact
2016-06-22 15:30:34 +05:30
relation_hash_list.is_a?(Array) ? relation_array : relation_array.first
end
2016-11-03 12:29:30 +05:30
2018-05-09 12:01:36 +05:30
def reader
@reader ||= Gitlab::ImportExport::Reader.new(shared: @shared)
end
2018-06-03 19:52:53 +05:30
def excluded_keys_for_relation(relation)
2018-11-20 20:47:30 +05:30
reader.attributes_finder.find_excluded_keys(relation)
end
2016-06-22 15:30:34 +05:30
end
end
end
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::ProjectTreeRestorer.prepend_if_ee('::EE::Gitlab::ImportExport::ProjectTreeRestorer')