Commit 4d894a7a authored by James Lopez's avatar James Lopez

added commits mapper and DB config import stuff

parent 13ab2072
module Gitlab
module ImportExport
class CommitMapper
def initialize(commits:, members_map:, project_id:, relation_factory: Gitlab::ImportExport::RelationFactory, user_admin:)
@commits = commits
@members_map = members_map
@project_id = project_id
@relation_factory = relation_factory
@user_admin = user_admin
end
def ids_map
@ids_map ||= map_commits
end
def map_commits
@id_map = Hash.new(-1)
@commits.each do |commit_hash|
@relation_factory.update_user_references(commit_hash, @members_map)
commit_hash['project_id'] = @project_id
@relation_factory.update_project_references(commit_hash, Ci::Commit)
create_commit_statuses(commit_hash)
create_commit(commit_hash)
end
@id_map
end
def create_commit(commit_hash)
old_id = commit_hash.delete('id')
commit = Ci::Commit.new(commit_hash)
commit.save!
@id_map[old_id] = commit.id
end
def create_commit_statuses(commit_hash)
commit_hash['statuses'].map! do |status_hash|
@relation_factory.create(relation_sym: :statuses,
relation_hash: status_hash.merge('project_id' => @project_id,
'commit_id' => nil),
members_mapper: @members_map,
commits_mapper: nil,
user_admin: @user_admin)
end
end
end
end
end
...@@ -14,6 +14,7 @@ module Gitlab ...@@ -14,6 +14,7 @@ module Gitlab
json = IO.read(@path) json = IO.read(@path)
@tree_hash = ActiveSupport::JSON.decode(json) @tree_hash = ActiveSupport::JSON.decode(json)
@project_members = @tree_hash.delete('project_members') @project_members = @tree_hash.delete('project_members')
@commits = @tree_hash.delete('ci_commits')
create_relations create_relations
rescue => e rescue => e
@shared.error(e) @shared.error(e)
...@@ -32,6 +33,13 @@ module Gitlab ...@@ -32,6 +33,13 @@ module Gitlab
project: project) project: project)
end end
def commits_mapper
@commits_mapper ||= Gitlab::ImportExport::CommitMapper.new(commits: @commits,
members_map: members_mapper.map,
project_id: project.id,
user_admin: @user.is_admin?)
end
def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash) def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash)
saved = [] saved = []
relation_list.each do |relation| relation_list.each do |relation|
...@@ -47,7 +55,7 @@ module Gitlab ...@@ -47,7 +55,7 @@ module Gitlab
def default_relation_list def default_relation_list
Gitlab::ImportExport::ImportExportReader.new(shared: @shared).tree.reject do |model| Gitlab::ImportExport::ImportExportReader.new(shared: @shared).tree.reject do |model|
model.is_a?(Hash) && model[:project_members] model.is_a?(Hash) && (model[:project_members] || model[:ci_commits])
end end
end end
...@@ -65,7 +73,6 @@ module Gitlab ...@@ -65,7 +73,6 @@ module Gitlab
relation_key = relation.keys.first.to_s relation_key = relation.keys.first.to_s
tree_hash[relation_key].each do |relation_item| tree_hash[relation_key].each do |relation_item|
relation.values.flatten.each do |sub_relation| relation.values.flatten.each do |sub_relation|
if sub_relation.is_a?(Hash) if sub_relation.is_a?(Hash)
relation_hash = relation_item[sub_relation.keys.first.to_s] relation_hash = relation_item[sub_relation.keys.first.to_s]
sub_relation = sub_relation.keys.first sub_relation = sub_relation.keys.first
...@@ -97,6 +104,7 @@ module Gitlab ...@@ -97,6 +104,7 @@ module Gitlab
Gitlab::ImportExport::RelationFactory.create(relation_sym: relation.to_sym, Gitlab::ImportExport::RelationFactory.create(relation_sym: relation.to_sym,
relation_hash: relation_hash.merge('project_id' => project.id), relation_hash: relation_hash.merge('project_id' => project.id),
members_mapper: members_mapper, members_mapper: members_mapper,
commits_mapper: commits_mapper,
user_admin: @user.is_admin?) user_admin: @user.is_admin?)
end end
end end
......
...@@ -3,21 +3,27 @@ module Gitlab ...@@ -3,21 +3,27 @@ module Gitlab
module RelationFactory module RelationFactory
extend self extend self
OVERRIDES = { snippets: :project_snippets, ci_commits: 'Ci::Commit', statuses: 'commit_status' }.freeze OVERRIDES = { snippets: :project_snippets,
USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze ci_commits: 'Ci::Commit',
statuses: 'commit_status',
def create(relation_sym:, relation_hash:, members_mapper:, user_admin:) variables: 'Ci::Variable',
triggers: 'Ci::Trigger',
builds: 'Ci::Build',
hooks: 'ProjectHook' }.freeze
USER_REFERENCES = %w(author_id assignee_id updated_by_id user_id).freeze
def create(relation_sym:, relation_hash:, members_mapper:, commits_mapper:, user_admin:)
relation_sym = parse_relation_sym(relation_sym) relation_sym = parse_relation_sym(relation_sym)
klass = parse_relation(relation_hash, relation_sym) klass = parse_relation(relation_hash, relation_sym)
update_missing_author(relation_hash, members_mapper, user_admin) if relation_sym == :notes update_missing_author(relation_hash, members_mapper, user_admin) if relation_sym == :notes
update_user_references(relation_hash, members_mapper.map) update_user_references(relation_hash, members_mapper.map)
update_project_references(relation_hash, klass) update_project_references(relation_hash, klass)
update_commit_references(relation_hash, commits_mapper.ids_map) if commits_mapper
imported_object(klass, relation_hash) generate_imported_object(klass, relation_hash, relation_sym)
end end
private
def update_user_references(relation_hash, members_map) def update_user_references(relation_hash, members_map)
USER_REFERENCES.each do |reference| USER_REFERENCES.each do |reference|
...@@ -27,6 +33,32 @@ module Gitlab ...@@ -27,6 +33,32 @@ module Gitlab
end end
end end
def update_project_references(relation_hash, klass)
project_id = relation_hash.delete('project_id')
if relation_hash['source_project_id'] && relation_hash['target_project_id']
# If source and target are the same, populate them with the new project ID.
if relation_hash['target_project_id'] == relation_hash['source_project_id']
relation_hash['source_project_id'] = project_id
else
relation_hash['source_project_id'] = -1
end
end
relation_hash['target_project_id'] = project_id if relation_hash['target_project_id']
# project_id may not be part of the export, but we always need to populate it if required.
relation_hash['project_id'] = project_id if klass.column_names.include?('project_id')
relation_hash['gl_project_id'] = project_id if relation_hash['gl_project_id']
end
private
def update_commit_references(relation_hash, commit_ids_map)
return unless relation_hash['commit_id']
old_commit_id = relation_hash['commit_id']
relation_hash['commit_id'] = commit_ids_map[old_commit_id]
end
def update_missing_author(relation_hash, members_map, user_admin) def update_missing_author(relation_hash, members_map, user_admin)
old_author_id = relation_hash['author_id'] old_author_id = relation_hash['author_id']
...@@ -50,22 +82,15 @@ module Gitlab ...@@ -50,22 +82,15 @@ module Gitlab
"\n\n *By #{author_name} on #{timestamp} (imported from GitLab project)*" "\n\n *By #{author_name} on #{timestamp} (imported from GitLab project)*"
end end
def update_project_references(relation_hash, klass) def generate_imported_object(klass, relation_hash, relation_sym)
project_id = relation_hash.delete('project_id') if relation_sym == 'Ci::Build' # call #trace= method after assigning the other attributes
trace = relation_hash.delete('trace')
if relation_hash['source_project_id'] && relation_hash['target_project_id'] imported_object(klass, relation_hash) do |imported_object|
# If source and target are the same, populate them with the new project ID. imported_object.trace = trace
if relation_hash['target_project_id'] == relation_hash['source_project_id']
relation_hash['source_project_id'] = project_id
else
relation_hash['source_project_id'] = -1
end end
else
imported_object(klass, relation_hash)
end end
relation_hash['target_project_id'] = project_id if relation_hash['target_project_id']
# project_id may not be part of the export, but we always need to populate it if required.
relation_hash['project_id'] = project_id if klass.column_names.include?('project_id')
relation_hash['gl_project_id'] = project_id if relation_hash ['gl_project_id']
end end
def relation_class(relation_sym) def relation_class(relation_sym)
...@@ -78,6 +103,7 @@ module Gitlab ...@@ -78,6 +103,7 @@ module Gitlab
def imported_object(klass, relation_hash) def imported_object(klass, relation_hash)
imported_object = klass.new(relation_hash) imported_object = klass.new(relation_hash)
yield(imported_object) if block_given?
imported_object.importing = true if imported_object.respond_to?(:importing) imported_object.importing = true if imported_object.respond_to?(:importing)
imported_object imported_object
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment