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 ...@@ -31,7 +31,9 @@ module Gitlab
private private
def where_clause 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)) project_clause = table[key].eq(value).and(table[:project_id].eq(@project.id))
if @group if @group
...@@ -48,13 +50,36 @@ module Gitlab ...@@ -48,13 +50,36 @@ module Gitlab
def project_attributes def project_attributes
@attributes.except(:group).tap do |atts| @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
end end
def label? def label?
@klass == Label || @klass < Label @klass == Label || @klass < Label
end 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 end
end end
...@@ -257,15 +257,7 @@ module Gitlab ...@@ -257,15 +257,7 @@ module Gitlab
end end
def existing_object def existing_object
@existing_object ||= @existing_object ||= find_or_create_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
end end
def unknown_service? def unknown_service?
...@@ -275,29 +267,17 @@ module Gitlab ...@@ -275,29 +267,17 @@ module Gitlab
def find_or_create_object! def find_or_create_object!
# Can't use IDs as validation exists calilng `.group` or `.project` # 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[: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[:title] = parsed_relation_hash['title'] if parsed_relation_hash['title']
hash.delete('project_id')
end end
if label? if label?
label = GroupProjectFinder.find_or_new(Label, finder_hash) GroupProjectFinder.find_or_new(Label, finder_hash)
parsed_relation_hash.delete('priorities') if label.persisted?
parsed_relation_hash.delete('type')
label.save!
label
else else
object = GroupProjectFinder.find_or_create(relation_class, finder_hash) 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
end end
end end
......
...@@ -16,15 +16,15 @@ describe Gitlab::ImportExport::GroupProjectFinder do ...@@ -16,15 +16,15 @@ describe Gitlab::ImportExport::GroupProjectFinder do
expect(described_class.find_or_new(Label, expect(described_class.find_or_new(Label,
title: 'group label', title: 'group label',
'project_id' => project.id, project: project,
'group_id' => project.group.id)).to eq(group_label) group: project.group)).to eq(group_label)
end end
it 'initializes a new label' do it 'initializes a new label' do
label = described_class.find_or_new(Label, label = described_class.find_or_new(Label,
title: 'group label', title: 'group label',
'project_id' => project.id, project: project,
'group_id' => project.group.id) group: project.group)
expect(label.persisted?).to be false expect(label.persisted?).to be false
end end
...@@ -32,8 +32,8 @@ describe Gitlab::ImportExport::GroupProjectFinder do ...@@ -32,8 +32,8 @@ describe Gitlab::ImportExport::GroupProjectFinder do
it 'creates a new label' do it 'creates a new label' do
label = described_class.find_or_create(Label, label = described_class.find_or_create(Label,
title: 'group label', title: 'group label',
'project_id' => project.id, project: project,
'group_id' => project.group.id) group: project.group)
expect(label.persisted?).to be true expect(label.persisted?).to be true
end end
...@@ -45,24 +45,24 @@ describe Gitlab::ImportExport::GroupProjectFinder do ...@@ -45,24 +45,24 @@ describe Gitlab::ImportExport::GroupProjectFinder do
expect(described_class.find_or_new(Milestone, expect(described_class.find_or_new(Milestone,
title: 'group milestone', title: 'group milestone',
'project_id' => project.id, project: project,
'group_id' => project.group.id)).to eq(milestone) group: project.group)).to eq(milestone)
end end
it 'initializes a new milestone' do it 'initializes a new milestone' do
milestone = described_class.find_or_new(Milestone, milestone = described_class.find_or_new(Milestone,
title: 'group milestone', title: 'group milestone',
'project_id' => project.id, project: project,
'group_id' => project.group.id) group: project.group)
expect(milestone.persisted?).to be false expect(milestone.persisted?).to be false
end end
it 'creates a new milestone' do it 'creates a new milestone' do
milestone = described_class.find_or_create(Milestone, milestone = described_class.find_or_create(Milestone,
title: 'group milestone', title: 'group milestone',
'project_id' => project.id, project: project,
'group_id' => project.group.id) group: project.group)
expect(milestone.persisted?).to be true expect(milestone.persisted?).to be true
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