Commit a8d9dbc1 authored by micael.bergeron's avatar micael.bergeron

fix the project import when an issue has a group milestone

parent 09a733ba
...@@ -35,7 +35,7 @@ module Gitlab ...@@ -35,7 +35,7 @@ module Gitlab
def initialize(relation_sym:, relation_hash:, members_mapper:, user:, project:) def initialize(relation_sym:, relation_hash:, members_mapper:, user:, project:)
@relation_name = OVERRIDES[relation_sym] || relation_sym @relation_name = OVERRIDES[relation_sym] || relation_sym
@relation_hash = relation_hash.except('noteable_id').merge('project_id' => project.id) @relation_hash = relation_hash.except('noteable_id')
@members_mapper = members_mapper @members_mapper = members_mapper
@user = user @user = user
@project = project @project = project
...@@ -248,7 +248,14 @@ module Gitlab ...@@ -248,7 +248,14 @@ module Gitlab
end end
def find_or_create_object! def find_or_create_object!
finder_attributes = @relation_name == :group_label ? %w[title group_id] : %w[title project_id] # TODO: Trying to find how I can correctly use the correct id depending on the object's type
finder_attributes = if @relation_type == :group_label
%w[title group_id]
elsif parsed_relation_hash['project_id']
%w[title project_id]
else
%w[title group_id]
end
finder_hash = parsed_relation_hash.slice(*finder_attributes) finder_hash = parsed_relation_hash.slice(*finder_attributes)
if label? if label?
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
"milestones": [ "milestones": [
{ {
"id": 1, "id": 1,
"title": "test milestone", "title": "Project milestone",
"project_id": 8, "project_id": 8,
"description": "test milestone", "description": "Project-level milestone",
"due_date": null, "due_date": null,
"created_at": "2016-06-14T15:02:04.415Z", "created_at": "2016-06-14T15:02:04.415Z",
"updated_at": "2016-06-14T15:02:04.415Z", "updated_at": "2016-06-14T15:02:04.415Z",
...@@ -65,9 +65,9 @@ ...@@ -65,9 +65,9 @@
"group_milestone_id": null, "group_milestone_id": null,
"milestone": { "milestone": {
"id": 1, "id": 1,
"title": "test milestone", "title": "Project milestone",
"project_id": 8, "project_id": 8,
"description": "test milestone", "description": "Project-level milestone",
"due_date": null, "due_date": null,
"created_at": "2016-06-14T15:02:04.415Z", "created_at": "2016-06-14T15:02:04.415Z",
"updated_at": "2016-06-14T15:02:04.415Z", "updated_at": "2016-06-14T15:02:04.415Z",
...@@ -76,27 +76,6 @@ ...@@ -76,27 +76,6 @@
"group_id": null "group_id": null
}, },
"label_links": [ "label_links": [
{
"id": 11,
"label_id": 6,
"target_id": 1,
"target_type": "Issue",
"created_at": "2017-08-15T18:37:40.795Z",
"updated_at": "2017-08-15T18:37:40.795Z",
"label": {
"id": 6,
"title": "group label",
"color": "#A8D695",
"project_id": null,
"created_at": "2017-08-15T18:37:19.698Z",
"updated_at": "2017-08-15T18:37:19.698Z",
"template": false,
"description": "",
"group_id": 5,
"type": "GroupLabel",
"priorities": []
}
},
{ {
"id": 11, "id": 11,
"label_id": 2, "label_id": 2,
...@@ -113,7 +92,7 @@ ...@@ -113,7 +92,7 @@
"updated_at": "2017-08-15T18:37:19.698Z", "updated_at": "2017-08-15T18:37:19.698Z",
"template": false, "template": false,
"description": "", "description": "",
"group_id": 5, "group_id": null,
"type": "ProjectLabel", "type": "ProjectLabel",
"priorities": [] "priorities": []
} }
...@@ -121,10 +100,6 @@ ...@@ -121,10 +100,6 @@
] ]
} }
], ],
"snippets": [ "snippets": [],
"hooks": []
],
"hooks": [
]
} }
...@@ -182,6 +182,53 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -182,6 +182,53 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
end end
end end
shared_examples 'restores project successfully' do
it 'correctly restores project' do
expect(shared.errors).to be_empty
expect(restored_project_json).to be_truthy
end
end
shared_examples 'restores project correctly' do |**results|
it 'has labels' do
expect(project.labels.size).to eq(results.fetch(:labels, 0))
end
it 'has label priorities' do
expect(project.labels.first.priorities).not_to be_empty
end
it 'has milestones' do
expect(project.milestones.size).to eq(results.fetch(:milestones, 0))
end
it 'has issues' do
expect(project.issues.size).to eq(results.fetch(:issues, 0))
end
it 'has issue with group label and project label' do
labels = project.issues.first.labels
expect(labels.where(type: "ProjectLabel").count).to eq(results.fetch(:first_issue_labels, 0))
end
end
shared_examples 'restores group correctly' do |**results|
it 'has group label' do
expect(project.group.labels.size).to eq(results.fetch(:labels, 0))
end
it 'has group milestone' do
expect(project.group.milestones.size).to eq(results.fetch(:milestones, 0))
end
it 'has issue with group label' do
labels = project.issues.first.labels
expect(labels.where(type: "GroupLabel").count).to eq(results.fetch(:first_issue_labels, 0))
end
end
context 'Light JSON' do context 'Light JSON' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: "", project_path: 'path') } let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: "", project_path: 'path') }
...@@ -189,12 +236,23 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -189,12 +236,23 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:project_tree_restorer) { described_class.new(user: user, shared: shared, project: project) } let(:project_tree_restorer) { described_class.new(user: user, shared: shared, project: project) }
let(:restored_project_json) { project_tree_restorer.restore } let(:restored_project_json) { project_tree_restorer.restore }
before do
allow(shared).to receive(:export_path).and_return('spec/lib/gitlab/import_export/')
end
context 'with a simple project' do
before do before do
project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.light.json") project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.light.json")
allow(shared).to receive(:export_path).and_return('spec/lib/gitlab/import_export/') restored_project_json
end end
it_behaves_like 'restores project correctly',
issues: 1,
labels: 1,
milestones: 1,
first_issue_labels: 1
context 'project.json file access check' do context 'project.json file access check' do
it 'does not read a symlink' do it 'does not read a symlink' do
Dir.mktmpdir do |tmpdir| Dir.mktmpdir do |tmpdir|
...@@ -209,14 +267,15 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -209,14 +267,15 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
end end
context 'when there is an existing build with build token' do context 'when there is an existing build with build token' do
it 'restores project json correctly' do before do
create(:ci_build, token: 'abcd') create(:ci_build, token: 'abcd')
end
expect(restored_project_json).to be_truthy it_behaves_like 'restores project successfully'
end end
end end
context 'with group' do context 'with a project that has a group' do
let!(:project) do let!(:project) do
create(:project, create(:project,
:builds_disabled, :builds_disabled,
...@@ -227,43 +286,22 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -227,43 +286,22 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
end end
before do before do
project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.light.json") project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.group.json")
restored_project_json restored_project_json
end end
it 'correctly restores project' do it_behaves_like 'restores project successfully'
expect(shared.errors).to be_empty it_behaves_like 'restores project correctly',
expect(restored_project_json).to be_truthy issues: 2,
end labels: 1,
milestones: 1,
first_issue_labels: 1
it 'has labels' do it_behaves_like 'restores group correctly',
expect(project.labels.count).to eq(2) labels: 1,
end milestones: 1,
first_issue_labels: 1
it 'creates group label' do
expect(project.group.labels.count).to eq(1)
end
it 'has label priorities' do
expect(project.labels.first.priorities).not_to be_empty
end
it 'has milestones' do
expect(project.milestones.count).to eq(1)
end
it 'has issue' do
expect(project.issues.count).to eq(1)
expect(project.issues.first.labels.count).to eq(2)
end
it 'has issue with group label and project label' do
labels = project.issues.first.labels
expect(labels.where(type: "GroupLabel").count).to eq(1)
expect(labels.where(type: "ProjectLabel").count).to eq(1)
end
end end
end end
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