From 11dd390c116f163a2e8a46af83e0d1bb194fb1f7 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 15 Jul 2018 22:07:39 -0700 Subject: [PATCH] Add tests for pull request comments --- .../bitbucket_server_import/importer.rb | 15 ++++--- .../bitbucket_server_import/importer_spec.rb | 40 +++++++++++++++---- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importer.rb b/lib/gitlab/bitbucket_server_import/importer.rb index 36069f0f168..c07525cef96 100644 --- a/lib/gitlab/bitbucket_server_import/importer.rb +++ b/lib/gitlab/bitbucket_server_import/importer.rb @@ -55,7 +55,10 @@ module Gitlab return users[email] if users.key?(email) - users[email] = User.find_by_any_email(email) + user = User.find_by_any_email(email) + users[email] = user&.id if user + + user&.id end def repo @@ -163,7 +166,7 @@ module Gitlab target_branch_sha = pull_request.target_branch_sha source_branch_sha = project.repository.commit(source_branch_sha)&.sha || source_branch_sha target_branch_sha = project.repository.commit(target_branch_sha)&.sha || target_branch_sha - author = gitlab_user_id(project, pull_request.author_email) || User.ghost + author_id = gitlab_user_id(project, pull_request.author_email) || User.ghost.id project.merge_requests.find_by(iid: pull_request.iid)&.destroy @@ -178,7 +181,7 @@ module Gitlab target_branch: Gitlab::Git.ref_name(pull_request.target_branch_name), target_branch_sha: target_branch_sha, state: pull_request.state, - author_id: author.id, + author_id: author_id, assignee_id: nil, created_at: pull_request.created_at, updated_at: pull_request.updated_at @@ -204,11 +207,11 @@ module Gitlab def import_merge_event(merge_request, merge_event) committer = merge_event.committer_email - user = find_user_id(committer) if committer - user ||= User.ghost + user_id = find_user_id(committer) if committer + user_id ||= User.ghost.id timestamp = merge_event.merge_timestamp metric = MergeRequest::Metrics.find_or_initialize_by(merge_request: merge_request) - metric.update_attributes(merged_by: user, merged_at: timestamp) + metric.update(merged_by_id: user_id, merged_at: timestamp) end def import_inline_comments(inline_comments, pull_request, merge_request) diff --git a/spec/lib/gitlab/bitbucket_server_import/importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importer_spec.rb index a05fd53ecf9..aee84a67eec 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importer_spec.rb @@ -56,25 +56,36 @@ describe Gitlab::BitbucketServerImport::Importer do updated_at: Time.now, merged?: true) - expect(subject.client).to receive(:pull_requests).and_return([pull_request]) + allow(subject.client).to receive(:pull_requests).and_return([pull_request]) @merge_event = instance_double( BitbucketServer::Representation::Activity, comment?: false, merge_event?: true, committer_email: project.owner.email, - merge_timestamp: Time.now) + merge_timestamp: Time.now.utc.change(usec: 0)) @inline_comment = instance_double( BitbucketServer::Representation::Activity, comment?: true, + inline_comment?: true, merge_event?: false) + + @pr_note = instance_double( + BitbucketServer::Representation::Comment, + note: 'Hello world', + author_email: 'unknown@gmail.com', + comments: [], + created_at: Time.now.utc.change(usec: 0), + updated_at: Time.now.utc.change(usec: 0)) @pr_comment = instance_double( BitbucketServer::Representation::Activity, comment?: true, - merge_event?: false) + inline_comment?: false, + merge_event?: false, + comment: @pr_note) end - it 'handles merge event' do + it 'imports merge event' do expect(subject.client).to receive(:activities).and_return([@merge_event]) expect { subject.execute }.to change { MergeRequest.count }.by(1) @@ -84,19 +95,32 @@ describe Gitlab::BitbucketServerImport::Importer do expect(merge_request.metrics.merged_at).to eq(@merge_event.merge_timestamp) end - context 'handles comments' do + it 'imports comments' do + expect(subject.client).to receive(:activities).and_return([@pr_comment]) + + expect { subject.execute }.to change { MergeRequest.count }.by(1) + + merge_request = MergeRequest.first + expect(merge_request.notes.count).to eq(1) + note = merge_request.notes.first + expect(note.note).to eq(@pr_note.note) + expect(note.author).to eq(project.owner) + expect(note.created_at).to eq(@pr_note.created_at) + expect(note.updated_at).to eq(@pr_note.created_at) end - context 'handles diff comments' do + it 'handles diff comments' do end - context 'falls back to comments if diff comments' do + it 'falls back to comments if diff comments' do end - context 'restores branches of inaccessible SHAs' do + it 'restores branches of inaccessible SHAs' do end end describe '#delete_temp_branches' do + it 'deletes branches' do + end end end