Commit 17c09916 authored by James Lopez's avatar James Lopez

fix and refactor note user mapping

parent b3bb8dc4
...@@ -7,7 +7,6 @@ module Gitlab ...@@ -7,7 +7,6 @@ module Gitlab
@exported_members = user.admin? ? exported_members : [] @exported_members = user.admin? ? exported_members : []
@user = user @user = user
@project = project @project = project
@missing_author_ids = []
# This needs to run first, as second call would be from #map # This needs to run first, as second call would be from #map
# which means project members already exist. # which means project members already exist.
...@@ -39,7 +38,6 @@ module Gitlab ...@@ -39,7 +38,6 @@ module Gitlab
def missing_keys_tracking_hash def missing_keys_tracking_hash
Hash.new do |_, key| Hash.new do |_, key|
@missing_author_ids << key
default_user_id default_user_id
end end
end end
......
...@@ -80,17 +80,13 @@ module Gitlab ...@@ -80,17 +80,13 @@ module Gitlab
# is left. # is left.
def set_note_author def set_note_author
old_author_id = @relation_hash['author_id'] old_author_id = @relation_hash['author_id']
# Users with admin access can map users
@relation_hash['author_id'] = admin_user? ? @members_mapper.map[old_author_id] : @members_mapper.default_user_id
author = @relation_hash.delete('author') author = @relation_hash.delete('author')
update_note_for_missing_author(author['name']) if missing_author?(old_author_id) update_note_for_missing_author(author['name']) unless has_author?(old_author_id)
end end
def missing_author?(old_author_id) def has_author?(old_author_id)
!admin_user? || @members_mapper.missing_author_ids.include?(old_author_id) admin_user? && !@members_mapper.map.keys.include?(old_author_id)
end end
def missing_author_note(updated_at, author_name) def missing_author_note(updated_at, author_name)
......
...@@ -24,7 +24,7 @@ describe Gitlab::ImportExport::MembersMapper, services: true do ...@@ -24,7 +24,7 @@ describe Gitlab::ImportExport::MembersMapper, services: true do
{ {
"id" => exported_user_id, "id" => exported_user_id,
"email" => user2.email, "email" => user2.email,
"username" => user2.username "username" => 'test'
} }
}, },
{ {
...@@ -48,6 +48,12 @@ describe Gitlab::ImportExport::MembersMapper, services: true do ...@@ -48,6 +48,12 @@ describe Gitlab::ImportExport::MembersMapper, services: true do
exported_members: exported_members, user: user, project: project) exported_members: exported_members, user: user, project: project)
end end
it 'includes the exported user ID in the map' do
members_mapper.map[-1]
expect(members_mapper.map.keys).to include(exported_user_id)
end
it 'maps a project member' do it 'maps a project member' do
expect(members_mapper.map[exported_user_id]).to eq(user2.id) expect(members_mapper.map[exported_user_id]).to eq(user2.id)
end end
...@@ -56,12 +62,6 @@ describe Gitlab::ImportExport::MembersMapper, services: true do ...@@ -56,12 +62,6 @@ describe Gitlab::ImportExport::MembersMapper, services: true do
expect(members_mapper.map[-1]).to eq(user.id) expect(members_mapper.map[-1]).to eq(user.id)
end end
it 'updates missing author IDs on missing project member' do
members_mapper.map[-1]
expect(members_mapper.missing_author_ids.first).to eq(-1)
end
it 'has invited members with no user' do it 'has invited members with no user' do
members_mapper.map members_mapper.map
...@@ -86,5 +86,34 @@ describe Gitlab::ImportExport::MembersMapper, services: true do ...@@ -86,5 +86,34 @@ describe Gitlab::ImportExport::MembersMapper, services: true do
expect(members_mapper.map[-1]).to eq(user.id) expect(members_mapper.map[-1]).to eq(user.id)
end end
end end
context 'chooses the one with an email first' do
before do
exported_members << {
"id" => 2,
"access_level" => 40,
"source_id" => 14,
"source_type" => "Project",
"user_id" => 19,
"notification_level" => 3,
"created_at" => "2016-03-11T10:21:44.822Z",
"updated_at" => "2016-03-11T10:21:44.822Z",
"created_by_id" => nil,
"invite_email" => nil,
"invite_token" => nil,
"invite_accepted_at" => nil,
"user" =>
{
"id" => exported_user_id,
"email" => 'test@email.com',
"username" => user2.username
}
}
end
it 'maps the project member that has a matching email first' do
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
end
end
end end
end end
...@@ -128,7 +128,7 @@ describe Gitlab::ImportExport::RelationFactory, lib: true do ...@@ -128,7 +128,7 @@ describe Gitlab::ImportExport::RelationFactory, lib: true do
let(:new_user) { create(:user) } let(:new_user) { create(:user) }
let(:exported_member) do let(:exported_member) do
{ {
"id" => 999, "id" => 111,
"access_level" => 30, "access_level" => 30,
"source_id" => 1, "source_id" => 1,
"source_type" => "Project", "source_type" => "Project",
...@@ -137,7 +137,7 @@ describe Gitlab::ImportExport::RelationFactory, lib: true do ...@@ -137,7 +137,7 @@ describe Gitlab::ImportExport::RelationFactory, lib: true do
"created_at" => "2016-11-18T09:29:42.634Z", "created_at" => "2016-11-18T09:29:42.634Z",
"updated_at" => "2016-11-18T09:29:42.634Z", "updated_at" => "2016-11-18T09:29:42.634Z",
"user" => { "user" => {
"id" => new_user.id, "id" => 999,
"email" => new_user.email, "email" => new_user.email,
"username" => new_user.username "username" => new_user.username
} }
......
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