diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
commit | b595cb0c1dec83de5bdee18284abe86614bed33b (patch) | |
tree | 8c3d4540f193c5ff98019352f554e921b3a41a72 /spec/lib/gitlab/github_import/importer/events | |
parent | 2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff) | |
download | gitlab-ce-b595cb0c1dec83de5bdee18284abe86614bed33b.tar.gz |
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'spec/lib/gitlab/github_import/importer/events')
5 files changed, 353 insertions, 0 deletions
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 new file mode 100644 index 00000000000..b773598853d --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::ChangedLabel do + subject(:importer) { described_class.new(project, user.id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:issue) { create(:issue, project: project) } + let!(:label) { create(:label, 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, + 'label_title' => label.title, + 'issue_db_id' => issue.id, + 'created_at' => '2022-04-26 18:30:53 UTC' + ) + 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 + end + + shared_examples 'new event' do + it 'creates a new label event' do + expect { importer.execute(issue_event) }.to change { issue.resource_label_events.count } + .from(0).to(1) + expect(issue.resource_label_events.last) + .to have_attributes(expected_event_attrs) + end + end + + before do + allow(Gitlab::Cache::Import::Caching).to receive(:read_integer).and_return(label.id) + end + + context 'when importing a labeled event' do + let(:event_type) { 'labeled' } + let(:expected_event_attrs) { event_attrs.merge(action: 'add') } + + it_behaves_like 'new event' + end + + context 'when importing an unlabeled event' do + let(:event_type) { 'unlabeled' } + let(:expected_event_attrs) { event_attrs.merge(action: 'remove') } + + it_behaves_like 'new event' + 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 new file mode 100644 index 00000000000..116917d3e06 --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/closed_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::Closed do + subject(:importer) { described_class.new(project, user.id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:issue) { create(:issue, 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' => 4, 'login' => 'alice' }, + 'event' => 'closed', + 'created_at' => '2022-04-26 18:30:53 UTC', + 'commit_id' => commit_id, + 'issue_db_id' => issue.id + ) + end + + let(:expected_event_attrs) do + { + project_id: project.id, + author_id: user.id, + target_id: issue.id, + target_type: Issue.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 + + 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(issue.resource_state_events.count).to eq 1 + expect(issue.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 } + + it 'creates expected event and state event' do + importer.execute(issue_event) + + 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] + end + 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 new file mode 100644 index 00000000000..118c482a7d9 --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_gitlab_redis_cache do + subject(:importer) { described_class.new(project, user.id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:sawyer_stub) { Struct.new(:iid, :issuable_type, keyword_init: true) } + + let(:issue) { create(:issue, project: project) } + let(:referenced_in) { build_stubbed(:issue, 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' => 4, 'login' => 'alice' }, + 'event' => 'cross-referenced', + 'source' => { + 'type' => 'issue', + 'issue' => { + 'number' => referenced_in.iid, + 'pull_request' => pull_request_resource + } + }, + 'created_at' => '2022-04-26 18:30:53 UTC', + 'issue_db_id' => issue.id + ) + end + + let(:pull_request_resource) { nil } + let(:expected_note_attrs) do + { + system: true, + noteable_type: Issue.name, + noteable_id: issue_event.issue_db_id, + project_id: project.id, + author_id: user.id, + note: expected_note_body, + created_at: issue_event.created_at + }.stringify_keys + end + + context 'when referenced in other issue' do + let(:expected_note_body) { "mentioned in issue ##{issue.iid}" } + + before do + other_issue_resource = sawyer_stub.new(iid: referenced_in.iid, issuable_type: 'Issue') + Gitlab::GithubImport::IssuableFinder.new(project, other_issue_resource) + .cache_database_id(referenced_in.iid) + end + + 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' + 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 } } + + let(:expected_note_body) { "mentioned in merge request !#{referenced_in.iid}" } + + before do + other_issue_resource = + sawyer_stub.new(iid: referenced_in.iid, issuable_type: 'MergeRequest') + Gitlab::GithubImport::IssuableFinder.new(project, other_issue_resource) + .cache_database_id(referenced_in.iid) + end + + 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' + end + end + + context 'when referenced in out of project issue/pull_request' do + it 'creates expected note' do + importer.execute(issue_event) + + expect(issue.notes.count).to eq 0 + end + 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 new file mode 100644 index 00000000000..a8c3fbcb05d --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/renamed_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::Renamed do + subject(:importer) { described_class.new(project, user.id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:issue) { create(:issue, project: project) } + + let(:issue_event) do + Gitlab::GithubImport::Representation::IssueEvent.from_json_hash( + 'id' => 6501124486, + 'actor' => { 'id' => 4, 'login' => 'alice' }, + 'event' => 'renamed', + 'commit_id' => nil, + 'created_at' => '2022-04-26 18:30:53 UTC', + 'old_title' => 'old title', + 'new_title' => 'new title', + 'issue_db_id' => issue.id + ) + end + + let(:expected_note_attrs) do + { + noteable_id: issue.id, + noteable_type: Issue.name, + project_id: project.id, + author_id: user.id, + note: "changed title from **{-old-} title** to **{+new+} title**", + 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: "title", + created_at: issue_event.created_at, + updated_at: issue_event.created_at + }.stringify_keys + end + + describe '#execute' do + it 'creates expected note' do + expect { importer.execute(issue_event) }.to change { issue.notes.count } + .from(0).to(1) + + expect(issue.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 +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 new file mode 100644 index 00000000000..81653b0ecdc --- /dev/null +++ b/spec/lib/gitlab/github_import/importer/events/reopened_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::GithubImport::Importer::Events::Reopened, :aggregate_failures do + subject(:importer) { described_class.new(project, user.id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:issue) { create(:issue, project: project) } + + 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' => 4, 'login' => 'alice' }, + 'event' => 'reopened', + 'created_at' => '2022-04-26 18:30:53 UTC', + 'issue_db_id' => issue.id + ) + end + + let(:expected_event_attrs) do + { + project_id: project.id, + author_id: user.id, + target_id: issue.id, + target_type: Issue.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 + + 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(issue.resource_state_events.count).to eq 1 + expect(issue.resource_state_events[0].attributes) + .to include expected_state_event_attrs + end +end |