diff options
Diffstat (limited to 'spec/lib/gitlab/github_import/importer/events')
8 files changed, 377 insertions, 164 deletions
diff --git a/spec/lib/gitlab/github_import/importer/events/changed_assignee_spec.rb b/spec/lib/gitlab/github_import/importer/events/changed_assignee_spec.rb index 2f6f727dc38..dbc72574ec2 100644 --- a/spec/lib/gitlab/github_import/importer/events/changed_assignee_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/changed_assignee_spec.rb @@ -6,31 +6,30 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedAssignee do subject(:importer) { described_class.new(project, client) } let_it_be(:project) { create(:project, :repository) } + let_it_be(:author) { create(:user) } let_it_be(:assignee) { create(:user) } - let_it_be(:assigner) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let(:issue_event) do Gitlab::GithubImport::Representation::IssueEvent.from_json_hash( 'id' => 6501124486, - 'actor' => { 'id' => 4, 'login' => 'alice' }, + 'actor' => { 'id' => author.id, 'login' => author.username }, 'event' => event_type, 'commit_id' => nil, 'created_at' => '2022-04-26 18:30:53 UTC', - 'assigner' => { 'id' => assigner.id, 'login' => assigner.username }, 'assignee' => { 'id' => assignee.id, 'login' => assignee.username }, - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end let(:note_attrs) do { - noteable_id: issue.id, - noteable_type: Issue.name, + noteable_id: issuable.id, + noteable_type: issuable.class.name, project_id: project.id, - author_id: assigner.id, + author_id: author.id, system: true, created_at: issue_event.created_at, updated_at: issue_event.created_at @@ -45,12 +44,12 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedAssignee do }.stringify_keys end - shared_examples 'new note' do + shared_examples 'create expected notes' do it 'creates expected note' do - expect { importer.execute(issue_event) }.to change { issue.notes.count } + expect { importer.execute(issue_event) }.to change { issuable.notes.count } .from(0).to(1) - expect(issue.notes.last) + expect(issuable.notes.last) .to have_attributes(expected_note_attrs) end @@ -67,29 +66,41 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedAssignee do end end + shared_examples 'process assigned & unassigned events' do + context 'when importing an assigned event' do + let(:event_type) { 'assigned' } + let(:expected_note_attrs) { note_attrs.merge(note: "assigned to @#{assignee.username}") } + + it_behaves_like 'create expected notes' + end + + context 'when importing an unassigned event' do + let(:event_type) { 'unassigned' } + let(:expected_note_attrs) { note_attrs.merge(note: "unassigned @#{assignee.username}") } + + it_behaves_like 'create expected notes' + end + end + describe '#execute' do before do allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.id) end allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder| + allow(finder).to receive(:find).with(author.id, author.username).and_return(author.id) allow(finder).to receive(:find).with(assignee.id, assignee.username).and_return(assignee.id) - allow(finder).to receive(:find).with(assigner.id, assigner.username).and_return(assigner.id) end end - context 'when importing an assigned event' do - let(:event_type) { 'assigned' } - let(:expected_note_attrs) { note_attrs.merge(note: "assigned to @#{assignee.username}") } - - it_behaves_like 'new note' + context 'with Issue' do + it_behaves_like 'process assigned & unassigned events' end - context 'when importing an unassigned event' do - let(:event_type) { 'unassigned' } - let(:expected_note_attrs) { note_attrs.merge(note: "unassigned @#{assigner.username}") } + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } - it_behaves_like 'new note' + it_behaves_like 'process assigned & unassigned events' end end end diff --git a/spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb b/spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb index e21672aa430..4476b4123ee 100644 --- a/spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedLabel do let_it_be(:user) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let!(:label) { create(:label, project: project) } let(:issue_event) do @@ -19,16 +19,14 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedLabel do 'event' => event_type, 'commit_id' => nil, 'label_title' => label.title, - 'issue_db_id' => issue.id, 'created_at' => '2022-04-26 18:30:53 UTC', - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end let(:event_attrs) do { user_id: user.id, - issue_id: issue.id, label_id: label.id, created_at: issue_event.created_at }.stringify_keys @@ -36,9 +34,9 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedLabel do shared_examples 'new event' do it 'creates a new label event' do - expect { importer.execute(issue_event) }.to change { issue.resource_label_events.count } + expect { importer.execute(issue_event) }.to change { issuable.resource_label_events.count } .from(0).to(1) - expect(issue.resource_label_events.last) + expect(issuable.resource_label_events.last) .to have_attributes(expected_event_attrs) end end @@ -46,24 +44,44 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedLabel do before do allow(Gitlab::Cache::Import::Caching).to receive(:read_integer).and_return(label.id) allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.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 - context 'when importing a labeled event' do - let(:event_type) { 'labeled' } - let(:expected_event_attrs) { event_attrs.merge(action: 'add') } + context 'with Issue' do + context 'when importing a labeled event' do + let(:event_type) { 'labeled' } + let(:expected_event_attrs) { event_attrs.merge(issue_id: issuable.id, action: 'add') } - it_behaves_like 'new event' + it_behaves_like 'new event' + end + + context 'when importing an unlabeled event' do + let(:event_type) { 'unlabeled' } + let(:expected_event_attrs) { event_attrs.merge(issue_id: issuable.id, action: 'remove') } + + it_behaves_like 'new event' + end end - context 'when importing an unlabeled event' do - let(:event_type) { 'unlabeled' } - let(:expected_event_attrs) { event_attrs.merge(action: 'remove') } + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + + context 'when importing a labeled event' do + let(:event_type) { 'labeled' } + let(:expected_event_attrs) { event_attrs.merge(merge_request_id: issuable.id, action: 'add') } - it_behaves_like 'new event' + it_behaves_like 'new event' + end + + context 'when importing an unlabeled event' do + let(:event_type) { 'unlabeled' } + let(:expected_event_attrs) { event_attrs.merge(merge_request_id: issuable.id, action: 'remove') } + + it_behaves_like 'new event' + end end end 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 2687627fc23..bc14b81bd91 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 @@ -9,7 +9,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do let_it_be(:user) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let!(:milestone) { create(:milestone, project: project) } let(:issue_event) do @@ -19,16 +19,15 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do 'event' => event_type, 'commit_id' => nil, 'milestone_title' => milestone.title, - 'issue_db_id' => issue.id, + 'issue_db_id' => issuable.id, 'created_at' => '2022-04-26 18:30:53 UTC', - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end let(:event_attrs) do { user_id: user.id, - issue_id: issue.id, milestone_id: milestone.id, state: 'opened', created_at: issue_event.created_at @@ -37,9 +36,9 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do shared_examples 'new event' do it 'creates a new milestone event' do - expect { importer.execute(issue_event) }.to change { issue.resource_milestone_events.count } + expect { importer.execute(issue_event) }.to change { issuable.resource_milestone_events.count } .from(0).to(1) - expect(issue.resource_milestone_events.last) + expect(issuable.resource_milestone_events.last) .to have_attributes(expected_event_attrs) end end @@ -48,25 +47,45 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedMilestone do before do allow(Gitlab::Cache::Import::Caching).to receive(:read_integer).and_return(milestone.id) allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.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 - context 'when importing a milestoned event' do - let(:event_type) { 'milestoned' } - let(:expected_event_attrs) { event_attrs.merge(action: 'add') } + context 'with Issue' do + context 'when importing a milestoned event' do + let(:event_type) { 'milestoned' } + let(:expected_event_attrs) { event_attrs.merge(issue_id: issuable.id, action: 'add') } - it_behaves_like 'new event' + it_behaves_like 'new event' + end + + context 'when importing demilestoned event' do + let(:event_type) { 'demilestoned' } + let(:expected_event_attrs) { event_attrs.merge(issue_id: issuable.id, action: 'remove') } + + it_behaves_like 'new event' + end end - context 'when importing demilestoned event' do - let(:event_type) { 'demilestoned' } - let(:expected_event_attrs) { event_attrs.merge(action: 'remove') } + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + + context 'when importing a milestoned event' do + let(:event_type) { 'milestoned' } + let(:expected_event_attrs) { event_attrs.merge(merge_request_id: issuable.id, action: 'add') } - it_behaves_like 'new event' + it_behaves_like 'new event' + end + + context 'when importing demilestoned event' do + let(:event_type) { 'demilestoned' } + let(:expected_event_attrs) { event_attrs.merge(merge_request_id: issuable.id, action: 'remove') } + + it_behaves_like 'new event' + end end end end diff --git a/spec/lib/gitlab/github_import/importer/events/changed_reviewer_spec.rb b/spec/lib/gitlab/github_import/importer/events/changed_reviewer_spec.rb new file mode 100644 index 00000000000..ff813dd41d9 --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/changed_reviewer_spec.rb @@ -0,0 +1,101 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedReviewer do + subject(:importer) { described_class.new(project, client) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:requested_reviewer) { create(:user) } + let_it_be(:review_requester) { create(:user) } + + let(:client) { instance_double('Gitlab::GithubImport::Client') } + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + + let(:issue_event) do + Gitlab::GithubImport::Representation::IssueEvent.from_json_hash( + 'id' => 6501124486, + 'actor' => { 'id' => 4, 'login' => 'alice' }, + 'event' => event_type, + 'commit_id' => nil, + 'created_at' => '2022-04-26 18:30:53 UTC', + 'review_requester' => { 'id' => review_requester.id, 'login' => review_requester.username }, + 'requested_reviewer' => { 'id' => requested_reviewer.id, 'login' => requested_reviewer.username }, + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } + ) + end + + let(:note_attrs) do + { + noteable_id: issuable.id, + noteable_type: issuable.class.name, + project_id: project.id, + author_id: review_requester.id, + system: true, + created_at: issue_event.created_at, + updated_at: issue_event.created_at + }.stringify_keys + end + + let(:expected_system_note_metadata_attrs) do + { + action: 'reviewer', + created_at: issue_event.created_at, + updated_at: issue_event.created_at + }.stringify_keys + end + + shared_examples 'create expected notes' do + it 'creates expected note' do + expect { importer.execute(issue_event) }.to change { issuable.notes.count } + .from(0).to(1) + + expect(issuable.notes.last) + .to have_attributes(expected_note_attrs) + end + + it 'creates expected system note metadata' do + expect { importer.execute(issue_event) }.to change(SystemNoteMetadata, :count) + .from(0).to(1) + + expect(SystemNoteMetadata.last) + .to have_attributes( + expected_system_note_metadata_attrs.merge( + note_id: Note.last.id + ) + ) + end + end + + shared_examples 'process review_requested & review_request_removed MR events' do + context 'when importing a review_requested event' do + let(:event_type) { 'review_requested' } + let(:expected_note_attrs) { note_attrs.merge(note: "requested review from @#{requested_reviewer.username}") } + + it_behaves_like 'create expected notes' + end + + context 'when importing a review_request_removed event' do + let(:event_type) { 'review_request_removed' } + let(:expected_note_attrs) { note_attrs.merge(note: "removed review request for @#{requested_reviewer.username}") } + + it_behaves_like 'create expected notes' + end + end + + describe '#execute' do + before do + allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| + allow(finder).to receive(:database_id).and_return(issuable.id) + end + allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder| + allow(finder).to receive(:find).with(requested_reviewer.id, requested_reviewer.username) + .and_return(requested_reviewer.id) + allow(finder).to receive(:find).with(review_requester.id, review_requester.username) + .and_return(review_requester.id) + end + end + + it_behaves_like 'process review_requested & review_request_removed MR events' + end +end diff --git a/spec/lib/gitlab/github_import/importer/events/closed_spec.rb b/spec/lib/gitlab/github_import/importer/events/closed_spec.rb index 9a49d80a8bb..f7e38f373c0 100644 --- a/spec/lib/gitlab/github_import/importer/events/closed_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/closed_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Closed do let_it_be(:user) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let(:commit_id) { nil } let(:issue_event) do @@ -21,7 +21,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Closed do 'event' => 'closed', 'created_at' => '2022-04-26 18:30:53 UTC', 'commit_id' => commit_id, - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end @@ -29,54 +29,74 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Closed do { project_id: project.id, author_id: user.id, - target_id: issue.id, - target_type: Issue.name, + target_id: issuable.id, + target_type: issuable.class.name, action: 'closed', created_at: issue_event.created_at, updated_at: issue_event.created_at }.stringify_keys end - let(:expected_state_event_attrs) do - { - user_id: user.id, - issue_id: issue.id, - state: 'closed', - created_at: issue_event.created_at - }.stringify_keys - end - before do allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.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) + shared_examples 'new event' do + it 'creates expected event and state event' do + importer.execute(issue_event) + + expect(issuable.events.count).to eq 1 + expect(issuable.events[0].attributes) + .to include expected_event_attrs + + expect(issuable.resource_state_events.count).to eq 1 + expect(issuable.resource_state_events[0].attributes) + .to include expected_state_event_attrs + end + + context 'when closed by commit' do + let!(:closing_commit) { create(:commit, project: project) } + let(:commit_id) { closing_commit.id } - expect(issue.events.count).to eq 1 - expect(issue.events[0].attributes) - .to include expected_event_attrs + it 'creates expected event and state event' do + importer.execute(issue_event) - expect(issue.resource_state_events.count).to eq 1 - expect(issue.resource_state_events[0].attributes) - .to include expected_state_event_attrs + expect(issuable.events.count).to eq 1 + state_event = issuable.resource_state_events.last + expect(state_event.source_commit).to eq commit_id[0..40] + end + end end - context 'when closed by commit' do - let!(:closing_commit) { create(:commit, project: project) } - let(:commit_id) { closing_commit.id } + context 'with Issue' do + let(:expected_state_event_attrs) do + { + user_id: user.id, + issue_id: issuable.id, + state: 'closed', + created_at: issue_event.created_at + }.stringify_keys + end - it 'creates expected event and state event' do - importer.execute(issue_event) + it_behaves_like 'new event' + end - expect(issue.events.count).to eq 1 - state_event = issue.resource_state_events.last - expect(state_event.source_commit).to eq commit_id[0..40] + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + let(:expected_state_event_attrs) do + { + user_id: user.id, + merge_request_id: issuable.id, + state: 'closed', + created_at: issue_event.created_at + }.stringify_keys end + + it_behaves_like 'new event' end end diff --git a/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb b/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb index 68e001c7364..bf19147d4c8 100644 --- a/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb @@ -9,9 +9,8 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_g let_it_be(:user) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue_iid) { 999 } - let(:issue) { create(:issue, project: project, iid: issue_iid) } + let(:issuable) { create(:issue, project: project, iid: issue_iid) } let(:referenced_in) { build_stubbed(:issue, project: project, iid: issue_iid + 1) } let(:commit_id) { nil } @@ -30,7 +29,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_g } }, 'created_at' => '2022-04-26 18:30:53 UTC', - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end @@ -38,8 +37,8 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_g let(:expected_note_attrs) do { system: true, - noteable_type: Issue.name, - noteable_id: issue.id, + noteable_type: issuable.class.name, + noteable_id: issuable.id, project_id: project.id, author_id: user.id, note: expected_note_body, @@ -47,58 +46,70 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_g }.stringify_keys end - context 'when referenced in other issue' do - let(:expected_note_body) { "mentioned in issue ##{referenced_in.iid}" } - - before do - allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(referenced_in.iid) - allow(finder).to receive(:database_id).and_return(issue.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) + shared_examples 'import cross-referenced event' do + context 'when referenced in other issue' do + let(:expected_note_body) { "mentioned in issue ##{referenced_in.iid}" } + + before do + allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| + allow(finder).to receive(:database_id).and_return(referenced_in.iid) + allow(finder).to receive(:database_id).and_return(issuable.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 - end - it 'creates expected note' do - importer.execute(issue_event) + it 'creates expected note' do + importer.execute(issue_event) - expect(issue.notes.count).to eq 1 - expect(issue.notes[0]).to have_attributes expected_note_attrs - expect(issue.notes[0].system_note_metadata.action).to eq 'cross_reference' + expect(issuable.notes.count).to eq 1 + expect(issuable.notes[0]).to have_attributes expected_note_attrs + expect(issuable.notes[0].system_note_metadata.action).to eq 'cross_reference' + end end - end - context 'when referenced in pull request' do - let(:referenced_in) { build_stubbed(:merge_request, project: project) } - let(:pull_request_resource) { { 'id' => referenced_in.iid } } + context 'when referenced in pull request' do + let(:referenced_in) { build_stubbed(:merge_request, project: project) } + let(:pull_request_resource) { { 'id' => referenced_in.iid } } - let(:expected_note_body) { "mentioned in merge request !#{referenced_in.iid}" } + let(:expected_note_body) { "mentioned in merge request !#{referenced_in.iid}" } - before do - allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(referenced_in.iid) - allow(finder).to receive(:database_id).and_return(issue.id) + before do + allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| + allow(finder).to receive(:database_id).and_return(referenced_in.iid) + allow(finder).to receive(:database_id).and_return(issuable.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 - allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder| - allow(finder).to receive(:find).with(user.id, user.username).and_return(user.id) + + it 'creates expected note' do + importer.execute(issue_event) + + expect(issuable.notes.count).to eq 1 + expect(issuable.notes[0]).to have_attributes expected_note_attrs + expect(issuable.notes[0].system_note_metadata.action).to eq 'cross_reference' end end - it 'creates expected note' do - importer.execute(issue_event) + context 'when referenced in out of project issue/pull_request' do + it 'does not create expected note' do + importer.execute(issue_event) - expect(issue.notes.count).to eq 1 - expect(issue.notes[0]).to have_attributes expected_note_attrs - expect(issue.notes[0].system_note_metadata.action).to eq 'cross_reference' + expect(issuable.notes.count).to eq 0 + end end end - context 'when referenced in out of project issue/pull_request' do - it 'does not create expected note' do - importer.execute(issue_event) + context 'with Issue' do + it_behaves_like 'import cross-referenced event' + end - expect(issue.notes.count).to eq 0 - end + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + + it_behaves_like 'import cross-referenced event' end end diff --git a/spec/lib/gitlab/github_import/importer/events/renamed_spec.rb b/spec/lib/gitlab/github_import/importer/events/renamed_spec.rb index 316ea798965..29598cb4354 100644 --- a/spec/lib/gitlab/github_import/importer/events/renamed_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/renamed_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Renamed do let_it_be(:project) { create(:project, :repository) } let_it_be(:user) { create(:user) } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let(:client) { instance_double('Gitlab::GithubImport::Client') } let(:issue_event) do @@ -20,14 +20,14 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Renamed do 'created_at' => '2022-04-26 18:30:53 UTC', 'old_title' => 'old title', 'new_title' => 'new title', - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end let(:expected_note_attrs) do { - noteable_id: issue.id, - noteable_type: Issue.name, + noteable_id: issuable.id, + noteable_type: issuable.class.name, project_id: project.id, author_id: user.id, note: "changed title from **{-old-} title** to **{+new+} title**", @@ -48,31 +48,43 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Renamed do describe '#execute' do before do allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.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 note' do - expect { importer.execute(issue_event) }.to change { issue.notes.count } - .from(0).to(1) + shared_examples 'import renamed event' do + it 'creates expected note' do + expect { importer.execute(issue_event) }.to change { issuable.notes.count } + .from(0).to(1) - expect(issue.notes.last) - .to have_attributes(expected_note_attrs) - end + expect(issuable.notes.last) + .to have_attributes(expected_note_attrs) + end - it 'creates expected system note metadata' do - expect { importer.execute(issue_event) }.to change { SystemNoteMetadata.count } - .from(0).to(1) + it 'creates expected system note metadata' do + expect { importer.execute(issue_event) }.to change { SystemNoteMetadata.count } + .from(0).to(1) - expect(SystemNoteMetadata.last) - .to have_attributes( - expected_system_note_metadata_attrs.merge( - note_id: Note.last.id + expect(SystemNoteMetadata.last) + .to have_attributes( + expected_system_note_metadata_attrs.merge( + note_id: Note.last.id + ) ) - ) + end + end + + context 'with Issue' do + it_behaves_like 'import renamed event' + end + + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + + it_behaves_like 'import renamed event' end end end diff --git a/spec/lib/gitlab/github_import/importer/events/reopened_spec.rb b/spec/lib/gitlab/github_import/importer/events/reopened_spec.rb index 2461dbb9701..354003fc997 100644 --- a/spec/lib/gitlab/github_import/importer/events/reopened_spec.rb +++ b/spec/lib/gitlab/github_import/importer/events/reopened_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Reopened, :aggregate_fail let_it_be(:user) { create(:user) } let(:client) { instance_double('Gitlab::GithubImport::Client') } - let(:issue) { create(:issue, project: project) } + let(:issuable) { create(:issue, project: project) } let(:issue_event) do Gitlab::GithubImport::Representation::IssueEvent.from_json_hash( @@ -19,7 +19,7 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Reopened, :aggregate_fail 'actor' => { 'id' => user.id, 'login' => user.username }, 'event' => 'reopened', 'created_at' => '2022-04-26 18:30:53 UTC', - 'issue' => { 'number' => issue.iid } + 'issue' => { 'number' => issuable.iid, pull_request: issuable.is_a?(MergeRequest) } ) end @@ -27,40 +27,61 @@ RSpec.describe Gitlab::GithubImport::Importer::Events::Reopened, :aggregate_fail { project_id: project.id, author_id: user.id, - target_id: issue.id, - target_type: Issue.name, + target_id: issuable.id, + target_type: issuable.class.name, action: 'reopened', created_at: issue_event.created_at, updated_at: issue_event.created_at }.stringify_keys end - let(:expected_state_event_attrs) do - { - user_id: user.id, - state: 'reopened', - created_at: issue_event.created_at - }.stringify_keys - end - before do allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder| - allow(finder).to receive(:database_id).and_return(issue.id) + allow(finder).to receive(:database_id).and_return(issuable.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) + shared_examples 'new event' do + it 'creates expected event and state event' do + importer.execute(issue_event) - expect(issue.events.count).to eq 1 - expect(issue.events[0].attributes) - .to include expected_event_attrs + expect(issuable.events.count).to eq 1 + expect(issuable.events[0].attributes) + .to include expected_event_attrs + + expect(issuable.resource_state_events.count).to eq 1 + expect(issuable.resource_state_events[0].attributes) + .to include expected_state_event_attrs + end + end + + context 'with Issue' do + let(:expected_state_event_attrs) do + { + user_id: user.id, + issue_id: issuable.id, + state: 'reopened', + created_at: issue_event.created_at + }.stringify_keys + end + + it_behaves_like 'new event' + end + + context 'with MergeRequest' do + let(:issuable) { create(:merge_request, source_project: project, target_project: project) } + let(:expected_state_event_attrs) do + { + user_id: user.id, + merge_request_id: issuable.id, + state: 'reopened', + created_at: issue_event.created_at + }.stringify_keys + end - expect(issue.resource_state_events.count).to eq 1 - expect(issue.resource_state_events[0].attributes) - .to include expected_state_event_attrs + it_behaves_like 'new event' end end |