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 ProjectTreeSaver
|
2016-11-03 12:29:30 +05:30
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
attr_reader :full_path
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def initialize(project:, current_user:, shared:, params: {})
|
|
|
|
@params = params
|
2016-06-22 15:30:34 +05:30
|
|
|
@project = project
|
2017-08-17 22:00:37 +05:30
|
|
|
@current_user = current_user
|
2016-06-22 15:30:34 +05:30
|
|
|
@shared = shared
|
|
|
|
@full_path = File.join(@shared.export_path, ImportExport.project_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2016-11-03 12:29:30 +05:30
|
|
|
mkdir_p(@shared.export_path)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
project_tree = serialize_project_tree
|
|
|
|
fix_project_tree(project_tree)
|
2019-12-21 20:55:43 +05:30
|
|
|
project_tree_json = JSON.generate(project_tree)
|
|
|
|
File.write(full_path, project_tree_json)
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
true
|
|
|
|
rescue => e
|
|
|
|
@shared.error(e)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
# Aware that the resulting hash needs to be pure-hash and
|
|
|
|
# does not include any AR objects anymore, only objects that run `.to_json`
|
2019-12-04 20:38:33 +05:30
|
|
|
def fix_project_tree(project_tree)
|
2018-05-09 12:01:36 +05:30
|
|
|
if @params[:description].present?
|
2019-12-04 20:38:33 +05:30
|
|
|
project_tree['description'] = @params[:description]
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
project_tree['project_members'] += group_members_array
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
RelationRenameService.add_new_associations(project_tree)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def serialize_project_tree
|
|
|
|
if Feature.enabled?(:export_fast_serialize, default_enabled: true)
|
|
|
|
Gitlab::ImportExport::FastHashSerializer
|
|
|
|
.new(@project, reader.project_tree)
|
|
|
|
.execute
|
|
|
|
else
|
|
|
|
@project.as_json(reader.project_tree)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def reader
|
|
|
|
@reader ||= Gitlab::ImportExport::Reader.new(shared: @shared)
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def group_members_array
|
2017-08-17 22:00:37 +05:30
|
|
|
group_members.as_json(reader.group_members_tree).each do |group_member|
|
|
|
|
group_member['source_type'] = 'Project' # Make group members project members of the future import
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_members
|
|
|
|
return [] unless @current_user.can?(:admin_group, @project.group)
|
|
|
|
|
|
|
|
# We need `.where.not(user_id: nil)` here otherwise when a group has an
|
|
|
|
# invitee, it would make the following query return 0 rows since a NULL
|
|
|
|
# user_id would be present in the subquery
|
|
|
|
# See http://stackoverflow.com/questions/129077/not-in-clause-and-null-values
|
|
|
|
non_null_user_ids = @project.project_members.where.not(user_id: nil).select(:user_id)
|
|
|
|
|
|
|
|
GroupMembersFinder.new(@project.group).execute.where.not(user_id: non_null_user_ids)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|