diff options
Diffstat (limited to 'spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb')
-rw-r--r-- | spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb | 132 |
1 files changed, 127 insertions, 5 deletions
diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index c7e91c340b0..914ebefdb8f 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporter, feature_category: :importers do include AfterNextHelpers - let_it_be(:project) do + let_it_be_with_reload(:project) do create(:project, :repository, :import_started, import_data_attributes: { data: { 'project_key' => 'key', 'repo_slug' => 'slug' }, @@ -18,21 +18,36 @@ RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporte let_it_be(:pull_request) { BitbucketServer::Representation::PullRequest.new(pull_request_data) } let_it_be(:note_author) { create(:user, username: 'note_author', email: 'note_author@example.org') } - let_it_be(:pull_request_author) do + let!(:pull_request_author) do create(:user, username: 'pull_request_author', email: 'pull_request_author@example.org') end let(:merge_event) do instance_double( BitbucketServer::Representation::Activity, + id: 3, comment?: false, merge_event?: true, + approved_event?: false, committer_email: pull_request_author.email, merge_timestamp: now, merge_commit: '12345678' ) end + let(:approved_event) do + instance_double( + BitbucketServer::Representation::Activity, + id: 4, + comment?: false, + merge_event?: false, + approved_event?: true, + approver_username: pull_request_author.username, + approver_email: pull_request_author.email, + created_at: now + ) + end + let(:pr_note) do instance_double( BitbucketServer::Representation::Comment, @@ -48,6 +63,7 @@ RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporte let(:pr_comment) do instance_double( BitbucketServer::Representation::Activity, + id: 5, comment?: true, inline_comment?: false, merge_event?: false, @@ -63,9 +79,9 @@ RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporte .to receive(:info).with(include(import_stage: stage, message: message)) end - subject(:importer) { described_class.new(project, pull_request.to_hash) } + subject(:importer) { described_class.new(project.reload, pull_request.to_hash) } - describe '#execute', :clean_gitlab_redis_cache do + describe '#execute' do context 'when a matching merge request is not found' do it 'does nothing' do expect { importer.execute }.not_to change { Note.count } @@ -79,7 +95,7 @@ RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporte end end - context 'when a matching merge request is found' do + context 'when a matching merge request is found', :clean_gitlab_redis_cache do let_it_be(:merge_request) { create(:merge_request, iid: pull_request.iid, source_project: project) } it 'logs its progress' do @@ -211,6 +227,112 @@ RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporte expect(merge_request.merge_commit_sha).to eq(merge_event.merge_commit) end end + + context 'when PR has an approved event' do + before do + allow_next(BitbucketServer::Client).to receive(:activities).and_return([approved_event]) + end + + it 'creates the approval, reviewer and approval note' do + expect { importer.execute } + .to change { merge_request.approvals.count }.from(0).to(1) + .and change { merge_request.notes.count }.from(0).to(1) + .and change { merge_request.reviewers.count }.from(0).to(1) + + approval = merge_request.approvals.first + + expect(approval.user).to eq(pull_request_author) + expect(approval.created_at).to eq(now) + + note = merge_request.notes.first + + expect(note.note).to eq('approved this merge request') + expect(note.author).to eq(pull_request_author) + expect(note.system).to be_truthy + expect(note.created_at).to eq(now) + + reviewer = merge_request.reviewers.first + + expect(reviewer.id).to eq(pull_request_author.id) + end + + context 'when a user with a matching username does not exist' do + before do + pull_request_author.update!(username: 'another_username') + end + + it 'finds the user based on email' do + importer.execute + + approval = merge_request.approvals.first + + expect(approval.user).to eq(pull_request_author) + end + + context 'when no users match email or username' do + let_it_be(:another_author) { create(:user) } + + before do + pull_request_author.destroy! + end + + it 'does not set an approver' do + expect { importer.execute } + .to not_change { merge_request.approvals.count } + .and not_change { merge_request.notes.count } + .and not_change { merge_request.reviewers.count } + + expect(merge_request.approvals).to be_empty + end + end + end + + context 'if the reviewer already existed' do + before do + merge_request.reviewers = [pull_request_author] + merge_request.save! + end + + it 'does not create the reviewer record' do + expect { importer.execute }.not_to change { merge_request.reviewers.count } + end + end + end + end + + shared_examples 'import is skipped' do + it 'does not log and does not import notes' do + expect(Gitlab::BitbucketServerImport::Logger) + .not_to receive(:info).with(include(import_stage: 'import_pull_request_notes', message: 'starting')) + + expect { importer.execute }.not_to change { Note.count } + end + end + + context 'when the project has been marked as failed' do + before do + project.import_state.mark_as_failed('error') + end + + include_examples 'import is skipped' + end + + context 'when the import data does not have credentials' do + before do + project.import_data.credentials = nil + project.import_data.save! + end + + include_examples 'import is skipped' + end + + context 'when the import data does not have data' do + before do + project.import_data.data = nil + project.import_data.save! + end + + include_examples 'import is skipped' end end end |