2016-04-14 15:10:57 +00:00
|
|
|
module Gitlab
|
2016-03-09 15:21:02 +00:00
|
|
|
module ImportExport
|
2016-03-09 17:42:04 +00:00
|
|
|
module RelationFactory
|
|
|
|
extend self
|
2016-03-09 15:21:02 +00:00
|
|
|
|
2016-04-13 15:47:14 +00:00
|
|
|
OVERRIDES = { snippets: :project_snippets, ci_commits: 'Ci::Commit', statuses: 'commit_status' }.freeze
|
2016-03-11 16:33:45 +00:00
|
|
|
USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze
|
2016-03-09 17:42:04 +00:00
|
|
|
|
2016-05-13 15:17:46 +00:00
|
|
|
def create(relation_sym:, relation_hash:, members_mapper:, user_admin:)
|
2016-03-09 17:42:04 +00:00
|
|
|
relation_sym = parse_relation_sym(relation_sym)
|
2016-04-12 10:24:40 +00:00
|
|
|
klass = parse_relation(relation_hash, relation_sym)
|
|
|
|
|
2016-05-13 15:17:46 +00:00
|
|
|
update_missing_author(relation_hash, members_mapper, user_admin) if relation_sym == :notes
|
2016-05-13 10:33:13 +00:00
|
|
|
update_user_references(relation_hash, members_mapper.map)
|
2016-04-13 15:47:14 +00:00
|
|
|
update_project_references(relation_hash, klass)
|
2016-04-12 10:24:40 +00:00
|
|
|
|
|
|
|
imported_object(klass, relation_hash)
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-03-11 16:33:45 +00:00
|
|
|
def update_user_references(relation_hash, members_map)
|
|
|
|
USER_REFERENCES.each do |reference|
|
|
|
|
if relation_hash[reference]
|
|
|
|
relation_hash[reference] = members_map[relation_hash[reference]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-13 15:17:46 +00:00
|
|
|
def update_missing_author(relation_hash, members_map, user_admin)
|
2016-05-13 12:17:10 +00:00
|
|
|
old_author_id = relation_hash['author_id']
|
2016-05-13 15:17:46 +00:00
|
|
|
|
|
|
|
# Users with admin access have access to mapping of users
|
|
|
|
if user_admin
|
|
|
|
relation_hash['author_id'] = members_map.default_project_member
|
|
|
|
else
|
|
|
|
relation_hash['author_id'] = members_map.map[old_author_id]
|
|
|
|
end
|
|
|
|
|
2016-05-13 14:25:27 +00:00
|
|
|
author = relation_hash.delete('author')
|
|
|
|
|
2016-05-13 15:17:46 +00:00
|
|
|
return unless user_admin && members_map.note_member_list.include?(old_author_id)
|
2016-05-13 10:33:13 +00:00
|
|
|
|
|
|
|
relation_hash['note'] = ('*Blank note*') if relation_hash['note'].blank?
|
2016-05-13 14:25:27 +00:00
|
|
|
relation_hash['note'] += (missing_author_note(relation_hash['updated_at'], author['name']))
|
2016-05-13 10:33:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def missing_author_note(updated_at, author_name)
|
2016-05-13 14:25:27 +00:00
|
|
|
timestamp = updated_at.split('.').first
|
|
|
|
"\n\n *By #{author_name} on #{timestamp} (imported from GitLab project)*"
|
2016-05-13 10:33:13 +00:00
|
|
|
end
|
|
|
|
|
2016-04-13 15:47:14 +00:00
|
|
|
def update_project_references(relation_hash, klass)
|
|
|
|
project_id = relation_hash.delete('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']
|
|
|
|
relation_hash['target_project_id'] = project_id if relation_hash['target_project_id']
|
|
|
|
relation_hash['source_project_id'] = -1 if relation_hash['source_project_id']
|
|
|
|
end
|
|
|
|
|
2016-03-09 17:42:04 +00:00
|
|
|
def relation_class(relation_sym)
|
|
|
|
relation_sym.to_s.classify.constantize
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
2016-03-09 17:42:04 +00:00
|
|
|
|
2016-03-09 17:42:04 +00:00
|
|
|
def parse_relation_sym(relation_sym)
|
2016-03-09 17:42:04 +00:00
|
|
|
OVERRIDES[relation_sym] || relation_sym
|
|
|
|
end
|
2016-04-12 10:24:40 +00:00
|
|
|
|
|
|
|
def imported_object(klass, relation_hash)
|
|
|
|
imported_object = klass.new(relation_hash)
|
|
|
|
imported_object.importing = true if imported_object.respond_to?(:importing)
|
|
|
|
imported_object
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_relation(relation_hash, relation_sym)
|
|
|
|
klass = relation_class(relation_sym)
|
2016-04-12 15:10:30 +00:00
|
|
|
relation_hash.delete('id')
|
2016-04-12 10:24:40 +00:00
|
|
|
klass
|
|
|
|
end
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|