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
def initialize(relation_sym:, relation_hash:, members_mapper:, user:, project:)
@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
@user = user
@project = project
......@@ -248,7 +248,14 @@ module Gitlab
end
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)
if label?
......
......@@ -5,9 +5,9 @@
"milestones": [
{
"id": 1,
"title": "test milestone",
"title": "Project milestone",
"project_id": 8,
"description": "test milestone",
"description": "Project-level milestone",
"due_date": null,
"created_at": "2016-06-14T15:02:04.415Z",
"updated_at": "2016-06-14T15:02:04.415Z",
......@@ -65,9 +65,9 @@
"group_milestone_id": null,
"milestone": {
"id": 1,
"title": "test milestone",
"title": "Project milestone",
"project_id": 8,
"description": "test milestone",
"description": "Project-level milestone",
"due_date": null,
"created_at": "2016-06-14T15:02:04.415Z",
"updated_at": "2016-06-14T15:02:04.415Z",
......@@ -76,27 +76,6 @@
"group_id": null
},
"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,
"label_id": 2,
......@@ -113,7 +92,7 @@
"updated_at": "2017-08-15T18:37:19.698Z",
"template": false,
"description": "",
"group_id": 5,
"group_id": null,
"type": "ProjectLabel",
"priorities": []
}
......@@ -121,10 +100,6 @@
]
}
],
"snippets": [
],
"hooks": [
]
"snippets": [],
"hooks": []
}
......@@ -182,6 +182,53 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
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
let(:user) { create(:user) }
let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: "", project_path: 'path') }
......@@ -190,33 +237,45 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:restored_project_json) { project_tree_restorer.restore }
before do
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/')
end
context 'project.json file access check' do
it 'does not read a symlink' do
Dir.mktmpdir do |tmpdir|
setup_symlink(tmpdir, 'project.json')
allow(shared).to receive(:export_path).and_call_original
context 'with a simple project' do
before do
project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.light.json")
restored_project_json
end
it_behaves_like 'restores project correctly',
issues: 1,
labels: 1,
milestones: 1,
first_issue_labels: 1
restored_project_json
context 'project.json file access check' do
it 'does not read a symlink' do
Dir.mktmpdir do |tmpdir|
setup_symlink(tmpdir, 'project.json')
allow(shared).to receive(:export_path).and_call_original
expect(shared.errors).to be_empty
restored_project_json
expect(shared.errors).to be_empty
end
end
end
end
context 'when there is an existing build with build token' do
it 'restores project json correctly' do
create(:ci_build, token: 'abcd')
context 'when there is an existing build with build token' do
before do
create(:ci_build, token: 'abcd')
end
expect(restored_project_json).to be_truthy
it_behaves_like 'restores project successfully'
end
end
context 'with group' do
context 'with a project that has a group' do
let!(:project) do
create(:project,
:builds_disabled,
......@@ -227,43 +286,22 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
end
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
end
it 'correctly restores project' do
expect(shared.errors).to be_empty
expect(restored_project_json).to be_truthy
end
it_behaves_like 'restores project successfully'
it_behaves_like 'restores project correctly',
issues: 2,
labels: 1,
milestones: 1,
first_issue_labels: 1
it 'has labels' do
expect(project.labels.count).to eq(2)
end
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
it_behaves_like 'restores group correctly',
labels: 1,
milestones: 1,
first_issue_labels: 1
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