Commit 8522173e authored by James Lopez's avatar James Lopez

refactor code once again to fix IID issues

parent cc061cd1
......@@ -31,7 +31,9 @@ module Gitlab
private
def where_clause
@attributes.except(:group, :project).map do |key, value|
return { project_id: @project.id } unless milestone? || label?
@attributes.slice(:title).map do |key, value|
project_clause = table[key].eq(value).and(table[:project_id].eq(@project.id))
if @group
......@@ -48,13 +50,36 @@ module Gitlab
def project_attributes
@attributes.except(:group).tap do |atts|
atts['type'] = 'ProjectLabel' if label?
if label?
atts['type'] = 'ProjectLabel'
elsif milestone?
if atts['group_id']
atts['iid'] = nil
atts.delete('group_id')
else
claim_iid
end
end
end
end
def label?
@klass == Label || @klass < Label
end
def milestone?
@klass == Milestone
end
def claim_iid
group_milestone = @project.milestones.find_by(iid: @attributes['iid'])
group_milestone.update!(iid: max_milestone_iid + 1) if group_milestone
end
def max_milestone_iid
[@attributes['iid'], @project.milestones.maximum(:iid)].compact.max
end
end
end
end
......@@ -257,15 +257,7 @@ module Gitlab
end
def existing_object
@existing_object ||=
begin
existing_object = find_or_create_object!
# Done in two steps, as MySQL behaves differently than PostgreSQL using
# the +find_or_create_by+ method and does not return the ID the second time.
existing_object.update!(parsed_relation_hash)
existing_object
end
@existing_object ||= find_or_create_object!
end
def unknown_service?
......@@ -275,29 +267,17 @@ module Gitlab
def find_or_create_object!
# Can't use IDs as validation exists calilng `.group` or `.project`
finder_hash = { project: @project }.tap do |hash|
finder_hash = parsed_relation_hash.tap do |hash|
hash[:group] = @project.group if relation_class.attribute_method?('group_id')
hash[:project] = @project
hash[:title] = parsed_relation_hash['title'] if parsed_relation_hash['title']
hash.delete('project_id')
end
if label?
label = GroupProjectFinder.find_or_new(Label, finder_hash)
parsed_relation_hash.delete('priorities') if label.persisted?
parsed_relation_hash.delete('type')
label.save!
label
GroupProjectFinder.find_or_new(Label, finder_hash)
else
object = GroupProjectFinder.find_or_create(relation_class, finder_hash)
if milestone?
parsed_relation_hash.delete('group_id') if object.project
parsed_relation_hash.delete('project_id') if object.group
parsed_relation_hash.delete('iid')
parsed_relation_hash.delete('id')
end
object
GroupProjectFinder.find_or_create(relation_class, finder_hash)
end
end
......
......@@ -16,15 +16,15 @@ describe Gitlab::ImportExport::GroupProjectFinder do
expect(described_class.find_or_new(Label,
title: 'group label',
'project_id' => project.id,
'group_id' => project.group.id)).to eq(group_label)
project: project,
group: project.group)).to eq(group_label)
end
it 'initializes a new label' do
label = described_class.find_or_new(Label,
title: 'group label',
'project_id' => project.id,
'group_id' => project.group.id)
project: project,
group: project.group)
expect(label.persisted?).to be false
end
......@@ -32,8 +32,8 @@ describe Gitlab::ImportExport::GroupProjectFinder do
it 'creates a new label' do
label = described_class.find_or_create(Label,
title: 'group label',
'project_id' => project.id,
'group_id' => project.group.id)
project: project,
group: project.group)
expect(label.persisted?).to be true
end
......@@ -45,15 +45,15 @@ describe Gitlab::ImportExport::GroupProjectFinder do
expect(described_class.find_or_new(Milestone,
title: 'group milestone',
'project_id' => project.id,
'group_id' => project.group.id)).to eq(milestone)
project: project,
group: project.group)).to eq(milestone)
end
it 'initializes a new milestone' do
milestone = described_class.find_or_new(Milestone,
title: 'group milestone',
'project_id' => project.id,
'group_id' => project.group.id)
project: project,
group: project.group)
expect(milestone.persisted?).to be false
end
......@@ -61,8 +61,8 @@ describe Gitlab::ImportExport::GroupProjectFinder do
it 'creates a new milestone' do
milestone = described_class.find_or_create(Milestone,
title: 'group milestone',
'project_id' => project.id,
'group_id' => project.group.id)
project: project,
group: project.group)
expect(milestone.persisted?).to be true
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