fixed import and spec is now passing!

This commit is contained in:
James Lopez 2016-04-13 17:47:14 +02:00
parent 36ba23d131
commit 267fd01ce8
3 changed files with 99 additions and 95 deletions

View file

@ -25,14 +25,13 @@ module Projects
def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash) def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash)
saved = [] saved = []
relation_list.each do |relation| relation_list.each do |relation|
next if !relation.is_a?(Hash) && tree_hash[relation.to_s].blank?
if relation.is_a?(Hash) if relation.is_a?(Hash)
create_sub_relations(relation, tree_hash) create_sub_relations(relation, tree_hash)
end end
relation_key = relation.is_a?(Hash) ? relation.keys.first : relation relation_key = relation.is_a?(Hash) ? relation.keys.first : relation
relation_hash = create_relation(relation_key, tree_hash[relation_key.to_s]) relation_hash = create_relation(relation_key, tree_hash[relation_key.to_s])
saved << project.update_attribute(relation_key, relation_hash) saved << project.update_attribute(relation_key, relation_hash)
# FIXME
# next if tree_hash[relation.to_s].blank?
end end
saved.all? saved.all?
end end

View file

@ -3,27 +3,21 @@ module Projects
module RelationFactory module RelationFactory
extend self extend self
OVERRIDES = { snippets: :project_snippets, commit: 'Ci::Commit' }.freeze OVERRIDES = { snippets: :project_snippets, ci_commits: 'Ci::Commit', statuses: 'commit_status' }.freeze
USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze
def create(relation_sym:, relation_hash:, members_map:) def create(relation_sym:, relation_hash:, members_map:)
relation_sym = parse_relation_sym(relation_sym) relation_sym = parse_relation_sym(relation_sym)
klass = parse_relation(relation_hash, relation_sym) klass = parse_relation(relation_hash, relation_sym)
handle_merge_requests(relation_hash) if relation_sym == :merge_requests
update_user_references(relation_hash, members_map) update_user_references(relation_hash, members_map)
update_project_references(relation_hash, klass)
imported_object(klass, relation_hash) imported_object(klass, relation_hash)
end end
private private
def handle_merge_requests(relation_hash)
relation_hash['target_project_id'] = relation_hash.delete('project_id')
relation_hash['source_project_id'] = -1
end
#TODO nice to have, optimize this to only get called for specific models
def update_user_references(relation_hash, members_map) def update_user_references(relation_hash, members_map)
USER_REFERENCES.each do |reference| USER_REFERENCES.each do |reference|
if relation_hash[reference] if relation_hash[reference]
@ -32,6 +26,16 @@ module Projects
end end
end end
def update_project_references(relation_hash, klass)
project_id = relation_hash.delete('project_id')
# project_id may not be part of the export, but we always need to populate it if required.
relation_hash['project_id'] = project_id if klass.column_names.include?('project_id')
relation_hash['gl_project_id'] = project_id if relation_hash ['gl_project_id']
relation_hash['target_project_id'] = project_id if relation_hash['target_project_id']
relation_hash['source_project_id'] = -1 if relation_hash['source_project_id']
end
def relation_class(relation_sym) def relation_class(relation_sym)
relation_sym.to_s.classify.constantize relation_sym.to_s.classify.constantize
end end
@ -49,7 +53,6 @@ module Projects
def parse_relation(relation_hash, relation_sym) def parse_relation(relation_hash, relation_sym)
klass = relation_class(relation_sym) klass = relation_class(relation_sym)
relation_hash.delete('id') relation_hash.delete('id')
relation_hash.delete('project_id') unless klass.column_names.include?('project_id')
klass klass
end end
end end

View file

@ -12,12 +12,12 @@
"issues": [ "issues": [
{ {
"id": 1, "id": 1,
"title": "Ad est est quia inventore eius suscipit molestiae.", "title": "Debitis vero omnis cum accusamus nihil rerum cupiditate.",
"assignee_id": 1, "assignee_id": 1,
"author_id": 2, "author_id": 2,
"project_id": 7, "project_id": 6,
"created_at": "2016-04-12T13:26:48.974Z", "created_at": "2016-04-13T14:40:32.471Z",
"updated_at": "2016-04-12T13:26:55.496Z", "updated_at": "2016-04-13T14:40:38.956Z",
"position": 0, "position": 0,
"branch_name": null, "branch_name": null,
"description": null, "description": null,
@ -34,8 +34,8 @@
"note": ":+1: issue", "note": ":+1: issue",
"noteable_type": "Issue", "noteable_type": "Issue",
"author_id": 21, "author_id": 21,
"created_at": "2016-04-12T13:26:55.480Z", "created_at": "2016-04-13T14:40:38.944Z",
"updated_at": "2016-04-12T13:26:55.480Z", "updated_at": "2016-04-13T14:40:38.944Z",
"project_id": 8, "project_id": 8,
"attachment": { "attachment": {
"url": null "url": null
@ -56,9 +56,9 @@
"id": 1, "id": 1,
"title": "label1", "title": "label1",
"color": "#990000", "color": "#990000",
"project_id": 7, "project_id": 6,
"created_at": "2016-04-12T13:26:51.027Z", "created_at": "2016-04-13T14:40:34.704Z",
"updated_at": "2016-04-12T13:26:53.799Z", "updated_at": "2016-04-13T14:40:36.891Z",
"template": false, "template": false,
"description": null "description": null
} }
@ -67,11 +67,11 @@
{ {
"id": 1, "id": 1,
"title": "Milestone v1.2", "title": "Milestone v1.2",
"project_id": 7, "project_id": 6,
"description": null, "description": null,
"due_date": null, "due_date": null,
"created_at": "2016-04-12T13:26:53.993Z", "created_at": "2016-04-13T14:40:37.901Z",
"updated_at": "2016-04-12T13:26:53.993Z", "updated_at": "2016-04-13T14:40:37.901Z",
"state": "active", "state": "active",
"iid": 1 "iid": 1
} }
@ -79,13 +79,13 @@
"snippets": [ "snippets": [
{ {
"id": 1, "id": 1,
"title": "Possimus harum est mollitia fugiat in.", "title": "Illo ipsa maxime magni aut.",
"content": "Itaque ipsum culpa quibusdam mollitia.", "content": "Excepturi delectus ut harum est molestiae dolor.",
"author_id": 10, "author_id": 10,
"project_id": 7, "project_id": 6,
"created_at": "2016-04-12T13:26:51.821Z", "created_at": "2016-04-13T14:40:35.603Z",
"updated_at": "2016-04-12T13:26:53.811Z", "updated_at": "2016-04-13T14:40:36.903Z",
"file_name": "thomas_marquardt", "file_name": "daphne.mraz",
"visibility_level": 0 "visibility_level": 0
} }
], ],
@ -94,9 +94,9 @@
"id": 1, "id": 1,
"tag": "v1.1.0", "tag": "v1.1.0",
"description": "Awesome release", "description": "Awesome release",
"project_id": 7, "project_id": 6,
"created_at": "2016-04-12T13:26:52.353Z", "created_at": "2016-04-13T14:40:36.223Z",
"updated_at": "2016-04-12T13:26:53.823Z" "updated_at": "2016-04-13T14:40:36.913Z"
} }
], ],
"events": [ "events": [
@ -106,9 +106,9 @@
"target_id": null, "target_id": null,
"title": null, "title": null,
"data": null, "data": null,
"project_id": 7, "project_id": 6,
"created_at": "2016-04-12T13:26:57.139Z", "created_at": "2016-04-13T14:40:40.122Z",
"updated_at": "2016-04-12T13:26:57.139Z", "updated_at": "2016-04-13T14:40:40.122Z",
"action": 8, "action": 8,
"author_id": 1 "author_id": 1
} }
@ -118,19 +118,19 @@
"id": 1, "id": 1,
"user": { "user": {
"id": 1, "id": 1,
"email": "maybell_auer@gleasonolson.com", "email": "norval.gulgowski@schambergerboyle.co.uk",
"created_at": "2016-04-12T13:26:47.499Z", "created_at": "2016-04-13T14:40:30.963Z",
"updated_at": "2016-04-12T13:26:47.499Z", "updated_at": "2016-04-13T14:40:30.963Z",
"name": "Charles Nitzsche", "name": "Jalon Cormier DVM",
"admin": false, "admin": false,
"projects_limit": 42, "projects_limit": 42,
"skype": "", "skype": "",
"linkedin": "", "linkedin": "",
"twitter": "", "twitter": "",
"authentication_token": "nCBdoTPWP-5adipsFwEq", "authentication_token": "tt-mPSZFvRBu8QzkW1Ss",
"theme_id": 2, "theme_id": 2,
"bio": null, "bio": null,
"username": "koby.zieme1", "username": "vance.turner1",
"can_create_group": true, "can_create_group": true,
"can_create_team": false, "can_create_team": false,
"state": "active", "state": "active",
@ -144,7 +144,7 @@
}, },
"hide_no_ssh_key": false, "hide_no_ssh_key": false,
"website_url": "", "website_url": "",
"notification_email": "maybell_auer@gleasonolson.com", "notification_email": "norval.gulgowski@schambergerboyle.co.uk",
"hide_no_password": false, "hide_no_password": false,
"password_automatically_set": false, "password_automatically_set": false,
"location": null, "location": null,
@ -173,13 +173,13 @@
"source_project_id": 2, "source_project_id": 2,
"author_id": 5, "author_id": 5,
"assignee_id": null, "assignee_id": null,
"title": "Natus vel molestiae ab et dolorem odit ut.", "title": "Dignissimos officia sit aut id dolor iure voluptatem expedita.",
"created_at": "2016-04-12T13:26:49.813Z", "created_at": "2016-04-13T14:40:33.381Z",
"updated_at": "2016-04-12T13:26:56.806Z", "updated_at": "2016-04-13T14:40:39.850Z",
"milestone_id": null, "milestone_id": null,
"state": "opened", "state": "opened",
"merge_status": "can_be_merged", "merge_status": "can_be_merged",
"target_project_id": 7, "target_project_id": 6,
"iid": 1, "iid": 1,
"description": null, "description": null,
"position": 0, "position": 0,
@ -353,8 +353,8 @@
} }
], ],
"merge_request_id": 1, "merge_request_id": 1,
"created_at": "2016-04-12T13:26:49.896Z", "created_at": "2016-04-13T14:40:33.474Z",
"updated_at": "2016-04-12T13:26:50.189Z", "updated_at": "2016-04-13T14:40:33.834Z",
"base_commit_sha": "ae73cb07c9eeaf35924a10f713b364d32b2dd34f", "base_commit_sha": "ae73cb07c9eeaf35924a10f713b364d32b2dd34f",
"real_size": "8" "real_size": "8"
}, },
@ -364,8 +364,8 @@
"note": ":+1: merge_request", "note": ":+1: merge_request",
"noteable_type": "MergeRequest", "noteable_type": "MergeRequest",
"author_id": 24, "author_id": 24,
"created_at": "2016-04-12T13:26:56.790Z", "created_at": "2016-04-13T14:40:39.832Z",
"updated_at": "2016-04-12T13:26:56.790Z", "updated_at": "2016-04-13T14:40:39.832Z",
"project_id": 9, "project_id": 9,
"attachment": { "attachment": {
"url": null "url": null
@ -381,19 +381,33 @@
] ]
} }
], ],
"commit_statuses": [ "ci_commits": [
{
"id": 2,
"project_id": 6,
"ref": "master",
"sha": "5937ac0a7beb003549fc5fd26fc247adbce4a52e",
"before_sha": null,
"push_data": null,
"created_at": "2016-04-13T14:40:37.759Z",
"updated_at": "2016-04-13T14:40:37.759Z",
"tag": false,
"yaml_errors": null,
"committed_at": null,
"gl_project_id": 6,
"statuses": [
{ {
"id": 1, "id": 1,
"project_id": null, "project_id": null,
"status": "success", "status": "success",
"finished_at": "2016-01-26T07:23:42.000Z", "finished_at": "2016-01-26T07:23:42.000Z",
"trace": null, "trace": null,
"created_at": "2016-04-12T13:26:53.034Z", "created_at": "2016-04-13T14:40:37.717Z",
"updated_at": "2016-04-12T13:26:53.850Z", "updated_at": "2016-04-13T14:40:37.771Z",
"started_at": "2016-01-26T07:21:42.000Z", "started_at": "2016-01-26T07:21:42.000Z",
"runner_id": null, "runner_id": null,
"coverage": null, "coverage": null,
"commit_id": 1, "commit_id": 2,
"commands": null, "commands": null,
"job_id": null, "job_id": null,
"name": "default", "name": "default",
@ -412,21 +426,9 @@
"gl_project_id": 7, "gl_project_id": 7,
"artifacts_metadata": null, "artifacts_metadata": null,
"erased_by_id": null, "erased_by_id": null,
"erased_at": null, "erased_at": null
"commit": {
"id": 1,
"project_id": 6,
"ref": null,
"sha": "97de212e80737a608d939f648d959671fb0a0142",
"before_sha": null,
"push_data": null,
"created_at": "2016-04-12T13:26:53.010Z",
"updated_at": "2016-04-12T13:26:53.010Z",
"tag": false,
"yaml_errors": null,
"committed_at": null,
"gl_project_id": 6
} }
]
} }
] ]
} }