diff --git a/lib/gitlab/fogbugz_import/importer.rb b/lib/gitlab/fogbugz_import/importer.rb index b3f4f4fb933fc6ec784f7ad051505f680a8cc62f..6ff4a6b308fc22eb5ffb27954eeab231aa2e6a0b 100644 --- a/lib/gitlab/fogbugz_import/importer.rb +++ b/lib/gitlab/fogbugz_import/importer.rb @@ -158,42 +158,40 @@ def opened_content(comments) end def import_issue_comments(issue, comments) - Note.transaction do - while comment = comments.shift - verb = comment['sVerb'] - - next if verb == 'Opened' - - content = format_content(comment['s']) - attachments = format_attachments(comment['rgAttachments']) - updates = format_updates(comment) - - next if content.blank? && attachments.empty? && updates.empty? - - author = user_info(comment['ixPerson'])[:name] - author_id = user_info(comment['ixPerson'])[:gitlab_id] || project.creator_id - date = DateTime.parse(comment['dt']) - - body = format_issue_comment_body( - comment['ixBugEvent'], - author, - date, - content, - attachments, - updates - ) - - note = Note.create!( - project_id: project.id, - noteable_type: "Issue", - noteable_id: issue.id, - author_id: author_id, - note: body - ) - - note.update_attribute(:created_at, date) - note.update_attribute(:updated_at, date) - end + while comment = comments.shift + verb = comment['sVerb'] + + next if verb == 'Opened' + + content = format_content(comment['s']) + attachments = format_attachments(comment['rgAttachments']) + updates = format_updates(comment) + + next if content.blank? && attachments.empty? && updates.empty? + + author = user_info(comment['ixPerson'])[:name] + author_id = user_info(comment['ixPerson'])[:gitlab_id] || project.creator_id + date = DateTime.parse(comment['dt']) + + body = format_issue_comment_body( + comment['ixBugEvent'], + author, + date, + content, + attachments, + updates + ) + + Note.create!( + importing: true, + project_id: project.id, + noteable_type: "Issue", + noteable_id: issue.id, + author_id: author_id, + note: body, + created_at: date, + updated_at: date + ) end end diff --git a/spec/lib/gitlab/fogbugz_import/importer_spec.rb b/spec/lib/gitlab/fogbugz_import/importer_spec.rb index a42468097257132c9b21ee0a2af65bd09c7287c0..8fc12229363b48beff2df89688a9c94aa8b12bdb 100644 --- a/spec/lib/gitlab/fogbugz_import/importer_spec.rb +++ b/spec/lib/gitlab/fogbugz_import/importer_spec.rb @@ -10,29 +10,20 @@ let(:token) { 'token' } let(:credentials) { { 'fb_session' => { 'uri' => base_url, 'token' => token } } } - let(:closed_bug) do + let(:bug_fopen) { 'false' } + let(:bug_events) { [] } + let(:bug) do { - fOpen: 'false', - sTitle: 'Closed bug', + fOpen: bug_fopen, + sTitle: 'Bug title', sLatestTextSummary: "", dtOpened: Time.now.to_s, dtLastUpdated: Time.now.to_s, - events: { event: [] } + events: { event: bug_events } }.with_indifferent_access end - let(:opened_bug) do - { - fOpen: 'true', - sTitle: 'Opened bug', - sLatestTextSummary: "", - dtOpened: Time.now.to_s, - dtLastUpdated: Time.now.to_s, - events: { event: [] } - }.with_indifferent_access - end - - let(:fogbugz_bugs) { [opened_bug, closed_bug] } + let(:fogbugz_bugs) { [bug] } subject(:importer) { described_class.new(project) } @@ -44,24 +35,56 @@ stub_fogbugz('search', cases: { case: fogbugz_bugs, count: fogbugz_bugs.size }) end - it 'imports bugs' do - expect { subject.execute }.to change { Issue.count }.by(2) + it 'imports the bug', :aggregate_failures do + expect { subject.execute }.to change { Issue.count }.by(1) + + issue = Issue.where(project_id: project.id).find_by_title(bug[:sTitle]) + + expect(issue.state_id).to eq(Issue.available_states[:closed]) end - it 'imports opened bugs' do - subject.execute + context 'when importing an opened bug' do + let(:bug_fopen) { 'true' } + + it 'imports the bug' do + expect { subject.execute }.to change { Issue.count }.by(1) - issue = Issue.where(project_id: project.id).find_by_title(opened_bug[:sTitle]) + issue = Issue.where(project_id: project.id).find_by_title(bug[:sTitle]) - expect(issue.state_id).to eq(Issue.available_states[:opened]) + expect(issue.state_id).to eq(Issue.available_states[:opened]) + end end - it 'imports closed bugs' do - subject.execute + context 'when importing multiple bugs' do + let(:fogbugz_bugs) { [bug, bug] } - issue = Issue.where(project_id: project.id).find_by_title(closed_bug[:sTitle]) + it 'imports the bugs' do + expect { subject.execute }.to change { Issue.count }.by(2) + end + end - expect(issue.state_id).to eq(Issue.available_states[:closed]) + context 'when imported bug contains events' do + let(:event_time) { Time.now.to_s } + let(:bug_events) do + [ + { sVerb: 'Opened', s: 'First event', dt: event_time }, + { sVerb: 'Assigned', s: 'Second event', dt: event_time } + ] + end + + let(:expected_note_timestamp) { DateTime.parse(event_time) } + + it 'imports the correct event', :aggregate_failures do + expect { subject.execute }.to change { Note.count }.by(1) + + note = Note.where(project_id: project.id).last + + expect(note).to have_attributes( + note: "*By on #{expected_note_timestamp} (imported from FogBugz)*\n\n---\n\n#{bug_events[1][:s]}", + created_at: expected_note_timestamp, + updated_at: expected_note_timestamp + ) + end end context 'verify url' do