diff options
Diffstat (limited to 'spec/lib/gitlab/github_import/importer/events')
-rw-r--r-- | spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb | 24 | ||||
-rw-r--r-- | spec/lib/gitlab/github_import/importer/events/merged_spec.rb | 74 |
2 files changed, 92 insertions, 6 deletions
diff --git a/spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb b/spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb index bc14b81bd91..371e76efb75 100644 --- a/spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do 'actor' => { 'id' => user.id, 'login' => user.username }, 'event' => event_type, 'commit_id' => nil, - 'milestone_title' => milestone.title, + 'milestone_title' => milestone_title, 'issue_db_id' => issuable.id, 'created_at' => '2022-04-26 18:30:53 UTC', 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } @@ -35,11 +35,23 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do end shared_examples 'new event' do - it 'creates a new milestone event' do - expect { importer.execute(issue_event) }.to change { issuable.resource_milestone_events.count } - .from(0).to(1) - expect(issuable.resource_milestone_events.last) - .to have_attributes(expected_event_attrs) + context 'when a matching milestone exists in GitLab' do + let(:milestone_title) { milestone.title } + + it 'creates a new milestone event' do + expect { importer.execute(issue_event) }.to change { issuable.resource_milestone_events.count } + .from(0).to(1) + expect(issuable.resource_milestone_events.last) + .to have_attributes(expected_event_attrs) + end + end + + context 'when a matching milestone does not exist in GitLab' do + let(:milestone_title) { 'A deleted milestone title' } + + it 'does not create a new milestone event without a milestone' do + expect { importer.execute(issue_event) }.not_to change { issuable.resource_milestone_events.count } + end end end diff --git a/spec/lib/gitlab/github_import/importer/events/merged_spec.rb b/spec/lib/gitlab/github_import/importer/events/merged_spec.rb new file mode 100644 index 00000000000..4ea62557dd6 --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/merged_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::Merged, feature_category: :importers do + subject(:importer) { described_class.new(project, client) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:client) { instance_double('Gitlab::GithubImport::Client') } + let(:merge_request) { create(:merge_request, source_project: project, target_project: project) } + let(:commit_id) { nil } + + let(:issue_event) do + Gitlab::GithubImport::Representation::IssueEvent.from_json_hash( + 'id' => 6501124486, + 'node_id' => 'CE_lADOHK9fA85If7x0zwAAAAGDf0mG', + 'url' => 'https://api.github.com/repos/elhowm/test-import/issues/events/6501124486', + 'actor' => { 'id' => user.id, 'login' => user.username }, + 'event' => 'merged', + 'created_at' => '2022-04-26 18:30:53 UTC', + 'commit_id' => commit_id, + 'issue' => { 'number' => merge_request.iid, pull_request: true } + ) + end + + before do + allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| + allow(finder).to receive(:database_id).and_return(merge_request.id) + end + allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder| + allow(finder).to receive(:find).with(user.id, user.username).and_return(user.id) + end + end + + it 'creates expected event and state event' do + importer.execute(issue_event) + + expect(merge_request.events.count).to eq 1 + expect(merge_request.events.first).to have_attributes( + project_id: project.id, + author_id: user.id, + target_id: merge_request.id, + target_type: merge_request.class.name, + action: 'merged', + created_at: issue_event.created_at, + updated_at: issue_event.created_at + ) + + expect(merge_request.resource_state_events.count).to eq 1 + expect(merge_request.resource_state_events.first).to have_attributes( + user_id: user.id, + merge_request_id: merge_request.id, + state: 'merged', + created_at: issue_event.created_at, + close_after_error_tracking_resolve: false, + close_auto_resolve_prometheus_alert: false + ) + end + + context 'when commit ID is present' do + let!(:commit) { create(:commit, project: project) } + let(:commit_id) { commit.id } + + it 'creates expected event and state event' do + importer.execute(issue_event) + + expect(merge_request.events.count).to eq 1 + state_event = merge_request.resource_state_events.last + expect(state_event.source_commit).to eq commit_id[0..40] + end + end +end |