From 26107e93548c0b9dee7df0a7e4003f1d55be1975 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 10 Aug 2019 18:43:52 -0700 Subject: [PATCH] Properly save suggestions in project exports Previously imports would fail if a merge request note included a suggestion with an `ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection` exception. This was happening because suggestions were listed as a descendant of merge requests, but this doesn't work because suggestions are directly associated with notes, not merge requests, and that association is lost. Rails also disallows creating intializing a has_many association through a different object. We fix this by making `suggestions` a child of `notes` within a merge request. This doesn't fix previously broken exported project exports, but new exports will work. Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/65880 --- .../sh-fix-import-export-suggestions.yml | 5 +++++ lib/gitlab/import_export/import_export.yml | 2 +- spec/lib/gitlab/import_export/project.json | 16 +++++++++++++++- .../import_export/project_tree_restorer_spec.rb | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/sh-fix-import-export-suggestions.yml diff --git a/changelogs/unreleased/sh-fix-import-export-suggestions.yml b/changelogs/unreleased/sh-fix-import-export-suggestions.yml new file mode 100644 index 00000000000..4b15fc3858f --- /dev/null +++ b/changelogs/unreleased/sh-fix-import-export-suggestions.yml @@ -0,0 +1,5 @@ +--- +title: Properly save suggestions in project exports +merge_request: 31690 +author: +type: fixed diff --git a/lib/gitlab/import_export/import_export.yml b/lib/gitlab/import_export/import_export.yml index f3888857bb6..1b7fc5fa10f 100644 --- a/lib/gitlab/import_export/import_export.yml +++ b/lib/gitlab/import_export/import_export.yml @@ -37,11 +37,11 @@ project_tree: - :user - merge_requests: - :metrics - - :suggestions - notes: - :author - events: - :push_event_payload + - :suggestions - merge_request_diff: - :merge_request_diff_commits - :merge_request_diff_files diff --git a/spec/lib/gitlab/import_export/project.json b/spec/lib/gitlab/import_export/project.json index 6d70b147666..a211675bbf2 100644 --- a/spec/lib/gitlab/import_export/project.json +++ b/spec/lib/gitlab/import_export/project.json @@ -2450,7 +2450,21 @@ "author": { "name": "Ottis Schuster II" }, - "events": [] + "events": [], + "suggestions": [ + { + "id": 1, + "note_id": 674, + "relative_order": 0, + "applied": false, + "commit_id": null, + "from_content": "Original line\n", + "to_content": "New line\n", + "lines_above": 0, + "lines_below": 0, + "outdated": false + } + ] }, { "id": 675, diff --git a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb index baec24590b4..d6e1fbaa979 100644 --- a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb @@ -125,6 +125,13 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do expect(MergeRequest.find_by(title: 'MR1').resource_label_events).not_to be_empty end + it 'restores suggestion' do + note = Note.find_by("note LIKE 'Saepe asperiores exercitationem non dignissimos laborum reiciendis et ipsum%'") + + expect(note.suggestions.count).to eq(1) + expect(note.suggestions.first.from_content).to eq("Original line\n") + end + context 'event at forth level of the tree' do let(:event) { Event.where(action: 6).first }