Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-09-20 02:18:09 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-09-20 02:18:09 +0300
commit6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch)
treedc4d20fe6064752c0bd323187252c77e0a89144b /spec/lib/gitlab/github_import
parent9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff)
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'spec/lib/gitlab/github_import')
-rw-r--r--spec/lib/gitlab/github_import/attachments_downloader_spec.rb97
-rw-r--r--spec/lib/gitlab/github_import/client_spec.rb62
-rw-r--r--spec/lib/gitlab/github_import/importer/events/changed_assignee_spec.rb55
-rw-r--r--spec/lib/gitlab/github_import/importer/events/changed_label_spec.rb48
-rw-r--r--spec/lib/gitlab/github_import/importer/events/changed_milestone_spec.rb49
-rw-r--r--spec/lib/gitlab/github_import/importer/events/changed_reviewer_spec.rb101
-rw-r--r--spec/lib/gitlab/github_import/importer/events/closed_spec.rb80
-rw-r--r--spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb95
-rw-r--r--spec/lib/gitlab/github_import/importer/events/renamed_spec.rb50
-rw-r--r--spec/lib/gitlab/github_import/importer/events/reopened_spec.rb63
-rw-r--r--spec/lib/gitlab/github_import/importer/issue_and_label_links_importer_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/importer/issue_event_importer_spec.rb18
-rw-r--r--spec/lib/gitlab/github_import/importer/issue_events_importer_spec.rb4
-rw-r--r--spec/lib/gitlab/github_import/importer/protected_branch_importer_spec.rb91
-rw-r--r--spec/lib/gitlab/github_import/importer/protected_branches_importer_spec.rb225
-rw-r--r--spec/lib/gitlab/github_import/importer/release_attachments_importer_spec.rb57
-rw-r--r--spec/lib/gitlab/github_import/importer/releases_attachments_importer_spec.rb74
-rw-r--r--spec/lib/gitlab/github_import/importer/repository_importer_spec.rb8
-rw-r--r--spec/lib/gitlab/github_import/importer/single_endpoint_issue_events_importer_spec.rb74
-rw-r--r--spec/lib/gitlab/github_import/markdown_text_spec.rb28
-rw-r--r--spec/lib/gitlab/github_import/parallel_scheduling_spec.rb4
-rw-r--r--spec/lib/gitlab/github_import/representation/diff_notes/suggestion_formatter_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/expose_attribute_spec.rb32
-rw-r--r--spec/lib/gitlab/github_import/representation/issue_event_spec.rb56
-rw-r--r--spec/lib/gitlab/github_import/representation/lfs_object_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/note_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/protected_branch_spec.rb51
-rw-r--r--spec/lib/gitlab/github_import/representation/pull_request_review_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/pull_request_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/release_attachments_spec.rb49
-rw-r--r--spec/lib/gitlab/github_import/representation/to_hash_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation/user_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/representation_spec.rb2
-rw-r--r--spec/lib/gitlab/github_import/user_finder_spec.rb12
34 files changed, 1277 insertions, 224 deletions
diff --git a/spec/lib/gitlab/github_import/attachments_downloader_spec.rb b/spec/lib/gitlab/github_import/attachments_downloader_spec.rb
new file mode 100644
index 00000000000..57391e06192
--- /dev/null
+++ b/spec/lib/gitlab/github_import/attachments_downloader_spec.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::AttachmentsDownloader do
+ subject(:downloader) { described_class.new(file_url) }
+
+ let_it_be(:file_url) { 'https://example.com/avatar.png' }
+ let_it_be(:content_type) { 'application/octet-stream' }
+
+ let(:content_length) { 1000 }
+ let(:chunk_double) { instance_double(HTTParty::FragmentWithResponse, code: 200) }
+ let(:headers_double) do
+ instance_double(
+ HTTParty::Response,
+ code: 200,
+ success?: true,
+ parsed_response: {},
+ headers: {
+ 'content-length' => content_length,
+ 'content-type' => content_type
+ }
+ )
+ end
+
+ describe '#perform' do
+ before do
+ allow(Gitlab::HTTP).to receive(:perform_request)
+ .with(Net::HTTP::Get, file_url, stream_body: true).and_yield(chunk_double)
+ allow(Gitlab::HTTP).to receive(:perform_request)
+ .with(Net::HTTP::Head, file_url, {}).and_return(headers_double)
+ end
+
+ context 'when file valid' do
+ it 'downloads file' do
+ file = downloader.perform
+
+ expect(File.exist?(file.path)).to eq(true)
+ end
+ end
+
+ context 'when filename is malicious' do
+ let_it_be(:file_url) { 'https://example.com/ava%2F..%2Ftar.png' }
+
+ it 'raises expected exception' do
+ expect { downloader.perform }.to raise_exception(
+ Gitlab::Utils::PathTraversalAttackError,
+ 'Invalid path'
+ )
+ end
+ end
+
+ context 'when file size exceeds limit' do
+ let(:content_length) { 26.megabytes }
+
+ it 'raises expected exception' do
+ expect { downloader.perform }.to raise_exception(
+ Gitlab::GithubImport::AttachmentsDownloader::DownloadError,
+ 'File size 26 MB exceeds limit of 25 MB'
+ )
+ end
+ end
+
+ context 'when file name length exceeds limit' do
+ before do
+ stub_const('BulkImports::FileDownloads::FilenameFetch::FILENAME_SIZE_LIMIT', 2)
+ end
+
+ it 'chops filename' do
+ file = downloader.perform
+
+ expect(File.exist?(file.path)).to eq(true)
+ expect(File.basename(file)).to eq('av.png')
+ end
+ end
+ end
+
+ describe '#delete' do
+ let(:tmp_dir_path) { File.join(Dir.tmpdir, 'github_attachments_test') }
+ let(:file) do
+ downloader.mkdir_p(tmp_dir_path)
+ file = File.open("#{tmp_dir_path}/test.txt", 'wb')
+ file.write('foo')
+ file.close
+ file
+ end
+
+ before do
+ allow(downloader).to receive(:filepath).and_return(file.path)
+ end
+
+ it 'removes file with parent folder' do
+ downloader.delete
+ expect(Dir.exist?(tmp_dir_path)).to eq false
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/client_spec.rb b/spec/lib/gitlab/github_import/client_spec.rb
index 2bd3910ad87..c88bb6de859 100644
--- a/spec/lib/gitlab/github_import/client_spec.rb
+++ b/spec/lib/gitlab/github_import/client_spec.rb
@@ -40,6 +40,22 @@ RSpec.describe Gitlab::GithubImport::Client do
end
end
+ describe '#repos' do
+ it 'returns the user\'s repositories as a hash' do
+ client = described_class.new('foo')
+
+ stub_request(:get, 'https://api.github.com/rate_limit')
+ .to_return(status: 200, headers: { 'X-RateLimit-Limit' => 5000, 'X-RateLimit-Remaining' => 5000 })
+
+ stub_request(:get, 'https://api.github.com/user/repos?page=1&page_length=10&per_page=100')
+ .to_return(status: 200, body: [{ id: 1 }, { id: 2 }].to_json, headers: { 'Content-Type' => 'application/json' })
+
+ repos = client.repos({ page: 1, page_length: 10 })
+
+ expect(repos).to match_array([{ id: 1 }, { id: 2 }])
+ end
+ end
+
describe '#repository' do
it 'returns the details of a repository' do
client = described_class.new('foo')
@@ -49,6 +65,20 @@ RSpec.describe Gitlab::GithubImport::Client do
client.repository('foo/bar')
end
+
+ it 'returns repository data as a hash' do
+ client = described_class.new('foo')
+
+ stub_request(:get, 'https://api.github.com/rate_limit')
+ .to_return(status: 200, headers: { 'X-RateLimit-Limit' => 5000, 'X-RateLimit-Remaining' => 5000 })
+
+ stub_request(:get, 'https://api.github.com/repos/foo/bar')
+ .to_return(status: 200, body: { id: 1 }.to_json, headers: { 'Content-Type' => 'application/json' })
+
+ repository = client.repository('foo/bar')
+
+ expect(repository).to eq({ id: 1 })
+ end
end
describe '#pull_request' do
@@ -98,6 +128,30 @@ RSpec.describe Gitlab::GithubImport::Client do
end
end
+ describe '#branches' do
+ it 'returns the branches' do
+ client = described_class.new('foo')
+
+ expect(client)
+ .to receive(:each_object)
+ .with(:branches, 'foo/bar')
+
+ client.branches('foo/bar')
+ end
+ end
+
+ describe '#branch_protection' do
+ it 'returns the protection details for the given branch' do
+ client = described_class.new('foo')
+
+ expect(client.octokit)
+ .to receive(:branch_protection).with('org/repo', 'bar')
+ expect(client).to receive(:with_rate_limit).and_yield
+
+ client.branch_protection('org/repo', 'bar')
+ end
+ end
+
describe '#each_page' do
let(:client) { described_class.new('foo') }
let(:object1) { double(:object1) }
@@ -234,7 +288,7 @@ RSpec.describe Gitlab::GithubImport::Client do
expect(client).to receive(:requests_remaining?).twice.and_return(true)
expect(Gitlab::Import::Logger).to receive(:info).with(hash_including(info_params)).once
- expect(client.with_rate_limit(&block_to_rate_limit)).to be(true)
+ expect(client.with_rate_limit(&block_to_rate_limit)).to eq({})
end
it 'retries and does not succeed' do
@@ -255,7 +309,7 @@ RSpec.describe Gitlab::GithubImport::Client do
expect(Gitlab::Import::Logger).to receive(:info).with(hash_including(info_params)).once
- expect(client.with_rate_limit(&block_to_rate_limit)).to be(true)
+ expect(client.with_rate_limit(&block_to_rate_limit)).to eq({})
end
it 'retries and does not succeed' do
@@ -559,7 +613,7 @@ RSpec.describe Gitlab::GithubImport::Client do
expect(Gitlab::Import::Logger).to receive(:info).with(hash_including(info_params)).once
- expect(client.search_repos_by_name('test')).to be(true)
+ expect(client.search_repos_by_name('test')).to eq({})
end
it 'retries and does not succeed' do
@@ -599,7 +653,7 @@ RSpec.describe Gitlab::GithubImport::Client do
call_count = 0
allow(client.octokit).to receive(method) do
call_count += 1
- call_count > 1 ? true : raise(described_class::CLIENT_CONNECTION_ERROR, 'execution expired')
+ call_count > 1 ? {} : raise(described_class::CLIENT_CONNECTION_ERROR, 'execution expired')
end
end
end
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
diff --git a/spec/lib/gitlab/github_import/importer/issue_and_label_links_importer_spec.rb b/spec/lib/gitlab/github_import/importer/issue_and_label_links_importer_spec.rb
index 49a76fb5e6b..d28640a4f07 100644
--- a/spec/lib/gitlab/github_import/importer/issue_and_label_links_importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer/issue_and_label_links_importer_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Importer::IssueAndLabelLinksImporter do
describe '#execute' do
diff --git a/spec/lib/gitlab/github_import/importer/issue_event_importer_spec.rb b/spec/lib/gitlab/github_import/importer/issue_event_importer_spec.rb
index 33d5fbf13a0..91121f3c3fc 100644
--- a/spec/lib/gitlab/github_import/importer/issue_event_importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer/issue_event_importer_spec.rb
@@ -42,10 +42,6 @@ RSpec.describe Gitlab::GithubImport::Importer::IssueEventImporter, :clean_gitlab
end
describe '#execute' do
- before do
- issue_event.attributes[:issue_db_id] = issue.id
- end
-
context "when it's closed issue event" do
let(:event_name) { 'closed' }
@@ -116,6 +112,20 @@ RSpec.describe Gitlab::GithubImport::Importer::IssueEventImporter, :clean_gitlab
Gitlab::GithubImport::Importer::Events::ChangedAssignee
end
+ context "when it's review_requested issue event" do
+ let(:event_name) { 'review_requested' }
+
+ it_behaves_like 'triggers specific event importer',
+ Gitlab::GithubImport::Importer::Events::ChangedReviewer
+ end
+
+ context "when it's review_request_removed issue event" do
+ let(:event_name) { 'review_request_removed' }
+
+ it_behaves_like 'triggers specific event importer',
+ Gitlab::GithubImport::Importer::Events::ChangedReviewer
+ end
+
context "when it's unknown issue event" do
let(:event_name) { 'fake' }
diff --git a/spec/lib/gitlab/github_import/importer/issue_events_importer_spec.rb b/spec/lib/gitlab/github_import/importer/issue_events_importer_spec.rb
index 8d4c1b01e50..2c1af4f8948 100644
--- a/spec/lib/gitlab/github_import/importer/issue_events_importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer/issue_events_importer_spec.rb
@@ -11,8 +11,8 @@ RSpec.describe Gitlab::GithubImport::Importer::IssueEventsImporter do
let(:parallel) { true }
let(:issue_event) do
struct = Struct.new(
- :id, :node_id, :url, :actor, :event, :commit_id, :commit_url, :label, :rename, :milestone,
- :source, :assignee, :assigner, :issue, :created_at, :performed_via_github_app,
+ :id, :node_id, :url, :actor, :event, :commit_id, :commit_url, :label, :rename, :milestone, :source,
+ :assignee, :assigner, :review_requester, :requested_reviewer, :issue, :created_at, :performed_via_github_app,
keyword_init: true
)
struct.new(id: rand(10), event: 'closed', created_at: '2022-04-26 18:30:53 UTC')
diff --git a/spec/lib/gitlab/github_import/importer/protected_branch_importer_spec.rb b/spec/lib/gitlab/github_import/importer/protected_branch_importer_spec.rb
new file mode 100644
index 00000000000..6dc6db739f4
--- /dev/null
+++ b/spec/lib/gitlab/github_import/importer/protected_branch_importer_spec.rb
@@ -0,0 +1,91 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Importer::ProtectedBranchImporter do
+ subject(:importer) { described_class.new(github_protected_branch, project, client) }
+
+ let(:allow_force_pushes_on_github) { true }
+ let(:github_protected_branch) do
+ Gitlab::GithubImport::Representation::ProtectedBranch.new(
+ id: 'protection',
+ allow_force_pushes: allow_force_pushes_on_github
+ )
+ end
+
+ let(:project) { create(:project, :repository) }
+ let(:client) { instance_double('Gitlab::GithubImport::Client') }
+
+ describe '#execute' do
+ let(:create_service) { instance_double('ProtectedBranches::CreateService') }
+
+ shared_examples 'create branch protection by the strictest ruleset' do
+ let(:expected_ruleset) do
+ {
+ name: 'protection',
+ push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
+ merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
+ allow_force_push: expected_allow_force_push
+ }
+ end
+
+ it 'calls service with the correct arguments' do
+ expect(ProtectedBranches::CreateService).to receive(:new).with(
+ project,
+ project.creator,
+ expected_ruleset
+ ).and_return(create_service)
+
+ expect(create_service).to receive(:execute).with(skip_authorization: true)
+ importer.execute
+ end
+
+ it 'creates protected branch and access levels for given github rule' do
+ expect { importer.execute }.to change(ProtectedBranch, :count).by(1)
+ .and change(ProtectedBranch::PushAccessLevel, :count).by(1)
+ .and change(ProtectedBranch::MergeAccessLevel, :count).by(1)
+ end
+ end
+
+ context 'when branch is protected on GitLab' do
+ before do
+ create(
+ :protected_branch,
+ project: project,
+ name: 'protect*',
+ allow_force_push: allow_force_pushes_on_gitlab
+ )
+ end
+
+ context 'when branch protection rule on Gitlab is stricter than on Github' do
+ let(:allow_force_pushes_on_github) { true }
+ let(:allow_force_pushes_on_gitlab) { false }
+ let(:expected_allow_force_push) { false }
+
+ it_behaves_like 'create branch protection by the strictest ruleset'
+ end
+
+ context 'when branch protection rule on Github is stricter than on Gitlab' do
+ let(:allow_force_pushes_on_github) { false }
+ let(:allow_force_pushes_on_gitlab) { true }
+ let(:expected_allow_force_push) { false }
+
+ it_behaves_like 'create branch protection by the strictest ruleset'
+ end
+
+ context 'when branch protection rules on Github and Gitlab are the same' do
+ let(:allow_force_pushes_on_github) { true }
+ let(:allow_force_pushes_on_gitlab) { true }
+ let(:expected_allow_force_push) { true }
+
+ it_behaves_like 'create branch protection by the strictest ruleset'
+ end
+ end
+
+ context 'when branch is not protected on GitLab' do
+ let(:expected_allow_force_push) { true }
+
+ it_behaves_like 'create branch protection by the strictest ruleset'
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/importer/protected_branches_importer_spec.rb b/spec/lib/gitlab/github_import/importer/protected_branches_importer_spec.rb
new file mode 100644
index 00000000000..4e9208be985
--- /dev/null
+++ b/spec/lib/gitlab/github_import/importer/protected_branches_importer_spec.rb
@@ -0,0 +1,225 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Importer::ProtectedBranchesImporter do
+ subject(:importer) { described_class.new(project, client, parallel: parallel) }
+
+ let(:project) { instance_double('Project', id: 4, import_source: 'foo/bar') }
+ let(:client) { instance_double('Gitlab::GithubImport::Client') }
+ let(:parallel) { true }
+
+ let(:branches) do
+ branch = Struct.new(:name, :protection, keyword_init: true)
+ protection = Struct.new(:enabled, keyword_init: true)
+
+ [
+ branch.new(name: 'main', protection: protection.new(enabled: false)),
+ branch.new(name: 'staging', protection: protection.new(enabled: true)),
+ branch.new(name: 'development', protection: nil) # when user has no admin right for this repo
+ ]
+ end
+
+ let(:github_protection_rule) do
+ response = Struct.new(:name, :url, :required_signatures, :enforce_admins, :required_linear_history,
+ :allow_force_pushes, :allow_deletion, :block_creations, :required_conversation_resolution,
+ keyword_init: true
+ )
+ required_signatures = Struct.new(:url, :enabled, keyword_init: true)
+ enforce_admins = Struct.new(:url, :enabled, keyword_init: true)
+ allow_option = Struct.new(:enabled, keyword_init: true)
+ response.new(
+ name: 'main',
+ url: 'https://example.com/branches/main/protection',
+ required_signatures: required_signatures.new(
+ url: 'https://example.com/branches/main/protection/required_signatures',
+ enabled: false
+ ),
+ enforce_admins: enforce_admins.new(
+ url: 'https://example.com/branches/main/protection/enforce_admins',
+ enabled: false
+ ),
+ required_linear_history: allow_option.new(
+ enabled: false
+ ),
+ allow_force_pushes: allow_option.new(
+ enabled: false
+ ),
+ allow_deletion: allow_option.new(
+ enabled: false
+ ),
+ block_creations: allow_option.new(
+ enabled: true
+ ),
+ required_conversation_resolution: allow_option.new(
+ enabled: false
+ )
+ )
+ end
+
+ describe '#parallel?' do
+ context 'when running in parallel mode' do
+ it { expect(importer).to be_parallel }
+ end
+
+ context 'when running in sequential mode' do
+ let(:parallel) { false }
+
+ it { expect(importer).not_to be_parallel }
+ end
+ end
+
+ describe '#execute' do
+ context 'when running in parallel mode' do
+ it 'imports protected branches in parallel' do
+ expect(importer).to receive(:parallel_import)
+
+ importer.execute
+ end
+ end
+
+ context 'when running in sequential mode' do
+ let(:parallel) { false }
+
+ it 'imports protected branches in sequence' do
+ expect(importer).to receive(:sequential_import)
+
+ importer.execute
+ end
+ end
+ end
+
+ describe '#sequential_import', :clean_gitlab_redis_cache do
+ let(:parallel) { false }
+
+ before do
+ allow(client).to receive(:branches).and_return(branches)
+ allow(client)
+ .to receive(:branch_protection)
+ .with(project.import_source, 'staging')
+ .and_return(github_protection_rule)
+ .once
+ end
+
+ it 'imports each protected branch in sequence' do
+ protected_branch_importer = instance_double('Gitlab::GithubImport::Importer::ProtectedBranchImporter')
+
+ expect(Gitlab::GithubImport::Importer::ProtectedBranchImporter)
+ .to receive(:new)
+ .with(
+ an_instance_of(Gitlab::GithubImport::Representation::ProtectedBranch),
+ project,
+ client
+ )
+ .and_return(protected_branch_importer)
+
+ expect(protected_branch_importer).to receive(:execute)
+ expect(Gitlab::GithubImport::ObjectCounter)
+ .to receive(:increment).with(project, :protected_branch, :fetched)
+
+ importer.sequential_import
+ end
+ end
+
+ describe '#parallel_import', :clean_gitlab_redis_cache do
+ before do
+ allow(client).to receive(:branches).and_return(branches)
+ allow(client)
+ .to receive(:branch_protection)
+ .with(project.import_source, 'staging')
+ .and_return(github_protection_rule)
+ .once
+ end
+
+ it 'imports each protected branch in parallel' do
+ expect(Gitlab::GithubImport::ImportProtectedBranchWorker)
+ .to receive(:bulk_perform_in)
+ .with(
+ 1.second,
+ [[project.id, an_instance_of(Hash), an_instance_of(String)]],
+ batch_delay: 1.minute,
+ batch_size: 1000
+ )
+ expect(Gitlab::GithubImport::ObjectCounter)
+ .to receive(:increment).with(project, :protected_branch, :fetched)
+
+ waiter = importer.parallel_import
+
+ expect(waiter).to be_an_instance_of(Gitlab::JobWaiter)
+ expect(waiter.jobs_remaining).to eq(1)
+ end
+ end
+
+ describe '#each_object_to_import', :clean_gitlab_redis_cache do
+ let(:branch_struct) { Struct.new(:protection, :name, :url, keyword_init: true) }
+ let(:protection_struct) { Struct.new(:enabled, keyword_init: true) }
+ let(:protected_branch) { branch_struct.new(name: 'main', protection: protection_struct.new(enabled: true)) }
+ let(:unprotected_branch) { branch_struct.new(name: 'staging', protection: protection_struct.new(enabled: false)) }
+ # when user has no admin rights on repo
+ let(:unknown_protection_branch) { branch_struct.new(name: 'development', protection: nil) }
+
+ let(:page_counter) { instance_double(Gitlab::GithubImport::PageCounter) }
+
+ before do
+ allow(client).to receive(:branches).with(project.import_source)
+ .and_return([protected_branch, unprotected_branch, unknown_protection_branch])
+ allow(client).to receive(:branch_protection)
+ .with(project.import_source, protected_branch.name).once
+ .and_return(github_protection_rule)
+ allow(Gitlab::GithubImport::ObjectCounter).to receive(:increment)
+ .with(project, :protected_branch, :fetched)
+ end
+
+ it 'imports each protected branch page by page' do
+ subject.each_object_to_import do |object|
+ expect(object).to eq github_protection_rule
+ end
+ expect(Gitlab::GithubImport::ObjectCounter).to have_received(:increment).once
+ end
+
+ context 'when protected branch is already processed' do
+ it "doesn't process this branch" do
+ subject.mark_as_imported(protected_branch)
+
+ subject.each_object_to_import {}
+ expect(Gitlab::GithubImport::ObjectCounter).not_to have_received(:increment)
+ end
+ end
+ end
+
+ describe '#importer_class' do
+ it { expect(importer.importer_class).to eq Gitlab::GithubImport::Importer::ProtectedBranchImporter }
+ end
+
+ describe '#representation_class' do
+ it { expect(importer.representation_class).to eq Gitlab::GithubImport::Representation::ProtectedBranch }
+ end
+
+ describe '#sidekiq_worker_class' do
+ it { expect(importer.sidekiq_worker_class).to eq Gitlab::GithubImport::ImportProtectedBranchWorker }
+ end
+
+ describe '#object_type' do
+ it { expect(importer.object_type).to eq :protected_branch }
+ end
+
+ describe '#collection_method' do
+ it { expect(importer.collection_method).to eq :protected_branches }
+ end
+
+ describe '#id_for_already_imported_cache' do
+ it 'returns the ID of the given protected branch' do
+ expect(importer.id_for_already_imported_cache(github_protection_rule)).to eq('main')
+ end
+ end
+
+ describe '#collection_options' do
+ it 'returns an empty Hash' do
+ # For large projects (e.g. kubernetes/kubernetes) GitHub's API may produce
+ # HTTP 500 errors when using explicit sorting options, regardless of what
+ # order you sort in. Not using any sorting options at all allows us to
+ # work around this.
+ expect(importer.collection_options).to eq({})
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/importer/release_attachments_importer_spec.rb b/spec/lib/gitlab/github_import/importer/release_attachments_importer_spec.rb
new file mode 100644
index 00000000000..4779f9c8982
--- /dev/null
+++ b/spec/lib/gitlab/github_import/importer/release_attachments_importer_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Importer::ReleaseAttachmentsImporter do
+ subject(:importer) { described_class.new(release_attachments, project, client) }
+
+ let_it_be(:project) { create(:project) }
+
+ let(:client) { instance_double('Gitlab::GithubImport::Client') }
+ let(:release) { create(:release, project: project, description: description) }
+ let(:release_attachments) do
+ Gitlab::GithubImport::Representation::ReleaseAttachments
+ .from_json_hash(release_db_id: release.id, description: release.description)
+ end
+
+ let(:doc_url) { 'https://github.com/nickname/public-test-repo/files/9020437/git-cheat-sheet.txt' }
+ let(:image_url) { 'https://user-images.githubusercontent.com/6833842/0cf366b61ef2.jpeg' }
+ let(:description) do
+ <<-TEXT.strip
+ Some text...
+
+ [special-doc](#{doc_url})
+ ![image.jpeg](#{image_url})
+ TEXT
+ end
+
+ describe '#execute' do
+ let(:downloader_stub) { instance_double(Gitlab::GithubImport::AttachmentsDownloader) }
+ let(:tmp_stub_doc) { Tempfile.create('attachment_download_test.txt') }
+ let(:tmp_stub_image) { Tempfile.create('image.jpeg') }
+
+ context 'when importing doc attachment' do
+ before do
+ allow(Gitlab::GithubImport::AttachmentsDownloader).to receive(:new).with(doc_url)
+ .and_return(downloader_stub)
+ allow(Gitlab::GithubImport::AttachmentsDownloader).to receive(:new).with(image_url)
+ .and_return(downloader_stub)
+ allow(downloader_stub).to receive(:perform).and_return(tmp_stub_doc, tmp_stub_image)
+ allow(downloader_stub).to receive(:delete).twice
+
+ allow(UploadService).to receive(:new)
+ .with(project, tmp_stub_doc, FileUploader).and_call_original
+ allow(UploadService).to receive(:new)
+ .with(project, tmp_stub_image, FileUploader).and_call_original
+ end
+
+ it 'updates release description with new attachment url' do
+ importer.execute
+
+ release.reload
+ expect(release.description).to start_with("Some text...\n\n [special-doc](/uploads/")
+ expect(release.description).to include('![image.jpeg](/uploads/')
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/importer/releases_attachments_importer_spec.rb b/spec/lib/gitlab/github_import/importer/releases_attachments_importer_spec.rb
new file mode 100644
index 00000000000..1aeb3462cd5
--- /dev/null
+++ b/spec/lib/gitlab/github_import/importer/releases_attachments_importer_spec.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Importer::ReleasesAttachmentsImporter do
+ subject { described_class.new(project, client) }
+
+ let_it_be(:project) { create(:project) }
+
+ let(:client) { instance_double(Gitlab::GithubImport::Client) }
+
+ describe '#each_object_to_import', :clean_gitlab_redis_cache do
+ let!(:release_1) { create(:release, project: project) }
+ let!(:release_2) { create(:release, project: project) }
+
+ it 'iterates each project release' do
+ list = []
+ subject.each_object_to_import do |object|
+ list << object
+ end
+ expect(list).to contain_exactly(release_1, release_2)
+ end
+
+ context 'when release is already processed' do
+ it "doesn't process this release" do
+ subject.mark_as_imported(release_1)
+
+ list = []
+ subject.each_object_to_import do |object|
+ list << object
+ end
+ expect(list).to contain_exactly(release_2)
+ end
+ end
+ end
+
+ describe '#representation_class' do
+ it { expect(subject.representation_class).to eq(Gitlab::GithubImport::Representation::ReleaseAttachments) }
+ end
+
+ describe '#importer_class' do
+ it { expect(subject.importer_class).to eq(Gitlab::GithubImport::Importer::ReleaseAttachmentsImporter) }
+ end
+
+ describe '#sidekiq_worker_class' do
+ it { expect(subject.sidekiq_worker_class).to eq(Gitlab::GithubImport::ImportReleaseAttachmentsWorker) }
+ end
+
+ describe '#collection_method' do
+ it { expect(subject.collection_method).to eq(:release_attachments) }
+ end
+
+ describe '#object_type' do
+ it { expect(subject.object_type).to eq(:release_attachment) }
+ end
+
+ describe '#id_for_already_imported_cache' do
+ let(:release) { build_stubbed(:release) }
+
+ it { expect(subject.id_for_already_imported_cache(release)).to eq(release.id) }
+ end
+
+ describe '#object_representation' do
+ let(:release) { build_stubbed(:release) }
+
+ it 'returns release attachments representation' do
+ representation = subject.object_representation(release)
+
+ expect(representation.class).to eq subject.representation_class
+ expect(representation.release_db_id).to eq release.id
+ expect(representation.description).to eq release.description
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb b/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb
index f2730ba74ec..0b8b1922d94 100644
--- a/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb
@@ -48,7 +48,7 @@ RSpec.describe Gitlab::GithubImport::Importer::RepositoryImporter do
describe '#import_wiki?' do
it 'returns true if the wiki should be imported' do
- repo = double(:repo, has_wiki: true)
+ repo = { has_wiki: true }
expect(client)
.to receive(:repository)
@@ -67,7 +67,7 @@ RSpec.describe Gitlab::GithubImport::Importer::RepositoryImporter do
end
it 'returns false if the GitHub wiki is disabled' do
- repo = double(:repo, has_wiki: false)
+ repo = { has_wiki: false }
expect(client)
.to receive(:repository)
@@ -78,7 +78,7 @@ RSpec.describe Gitlab::GithubImport::Importer::RepositoryImporter do
end
it 'returns false if the wiki has already been imported' do
- repo = double(:repo, has_wiki: true)
+ repo = { has_wiki: true }
expect(client)
.to receive(:repository)
@@ -186,7 +186,7 @@ RSpec.describe Gitlab::GithubImport::Importer::RepositoryImporter do
describe '#import_repository' do
it 'imports the repository' do
- repo = double(:repo, default_branch: 'develop')
+ repo = { default_branch: 'develop' }
expect(client)
.to receive(:repository)
diff --git a/spec/lib/gitlab/github_import/importer/single_endpoint_issue_events_importer_spec.rb b/spec/lib/gitlab/github_import/importer/single_endpoint_issue_events_importer_spec.rb
index bb1ee79ad93..4ed01fd7e0b 100644
--- a/spec/lib/gitlab/github_import/importer/single_endpoint_issue_events_importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer/single_endpoint_issue_events_importer_spec.rb
@@ -6,7 +6,8 @@ RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter
let(:client) { double }
let_it_be(:project) { create(:project, :import_started, import_source: 'http://somegithub.com') }
- let_it_be(:issue) { create(:issue, project: project) }
+
+ let!(:issuable) { create(:issue, project: project) }
subject { described_class.new(project, client, parallel: parallel) }
@@ -35,7 +36,7 @@ RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter
end
describe '#page_counter_id' do
- it { expect(subject.page_counter_id(issue)).to eq("issues/#{issue.iid}/issue_timeline") }
+ it { expect(subject.page_counter_id(issuable)).to eq("issues/#{issuable.iid}/issue_timeline") }
end
describe '#id_for_already_imported_cache' do
@@ -51,6 +52,39 @@ RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter
end
end
+ describe '#compose_associated_id!' do
+ let(:issuable) { build_stubbed(:issue, iid: 99) }
+ let(:event_resource) { Struct.new(:id, :event, :source, keyword_init: true) }
+
+ context 'when event type is cross-referenced' do
+ let(:event) do
+ source_resource = Struct.new(:issue, keyword_init: true)
+ issue_resource = Struct.new(:id, keyword_init: true)
+ event_resource.new(
+ id: nil,
+ event: 'cross-referenced',
+ source: source_resource.new(issue: issue_resource.new(id: '100500'))
+ )
+ end
+
+ it 'assigns event id' do
+ subject.compose_associated_id!(issuable, event)
+
+ expect(event.id).to eq 'cross-reference#99-in-100500'
+ end
+ end
+
+ context "when event type isn't cross-referenced" do
+ let(:event) { event_resource.new(id: nil, event: 'labeled') }
+
+ it "doesn't assign event id" do
+ subject.compose_associated_id!(issuable, event)
+
+ expect(event.id).to eq nil
+ end
+ end
+ end
+
describe '#each_object_to_import', :clean_gitlab_redis_cache do
let(:issue_event) do
struct = Struct.new(:id, :event, :created_at, :issue, keyword_init: true)
@@ -72,19 +106,37 @@ RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter
.with(
:issue_timeline,
project.import_source,
- issue.iid,
+ issuable.iid,
{ state: 'all', sort: 'created', direction: 'asc', page: 1 }
).and_yield(page)
end
- it 'imports each issue event page by page' do
- counter = 0
- subject.each_object_to_import do |object|
- expect(object).to eq issue_event
- expect(issue_event.issue['number']).to eq issue.iid
- counter += 1
+ context 'with issues' do
+ it 'imports each issue event page by page' do
+ counter = 0
+ subject.each_object_to_import do |object|
+ expect(object).to eq issue_event
+ expect(issue_event.issue['number']).to eq issuable.iid
+ expect(issue_event.issue['pull_request']).to eq false
+ counter += 1
+ end
+ expect(counter).to eq 1
+ end
+ end
+
+ context 'with merge requests' do
+ let!(:issuable) { create(:merge_request, source_project: project, target_project: project) }
+
+ it 'imports each merge request event page by page' do
+ counter = 0
+ subject.each_object_to_import do |object|
+ expect(object).to eq issue_event
+ expect(issue_event.issue['number']).to eq issuable.iid
+ expect(issue_event.issue['pull_request']).to eq true
+ counter += 1
+ end
+ expect(counter).to eq 1
end
- expect(counter).to eq 1
end
it 'triggers page number increment' do
@@ -103,7 +155,7 @@ RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter
context 'when page is already processed' do
before do
page_counter = Gitlab::GithubImport::PageCounter.new(
- project, subject.page_counter_id(issue)
+ project, subject.page_counter_id(issuable)
)
page_counter.set(page.number)
end
diff --git a/spec/lib/gitlab/github_import/markdown_text_spec.rb b/spec/lib/gitlab/github_import/markdown_text_spec.rb
index ad45469a4c3..1da6bb06403 100644
--- a/spec/lib/gitlab/github_import/markdown_text_spec.rb
+++ b/spec/lib/gitlab/github_import/markdown_text_spec.rb
@@ -60,6 +60,34 @@ RSpec.describe Gitlab::GithubImport::MarkdownText do
end
end
+ describe '.fetch_attachment_urls' do
+ let(:image_extension) { described_class::MEDIA_TYPES.sample }
+ let(:image_attachment) do
+ "![special-image](https://user-images.githubusercontent.com/6833862/"\
+ "176685788-e7a93168-7ded-406a-82b5-eb1c56685a93.#{image_extension})"
+ end
+
+ let(:doc_extension) { described_class::DOC_TYPES.sample }
+ let(:doc_attachment) do
+ "[some-doc](https://github.com/nickname/public-test-repo/"\
+ "files/9020437/git-cheat-sheet.#{doc_extension})"
+ end
+
+ let(:text) do
+ <<-TEXT
+ Comment with an attachment
+ #{image_attachment}
+ #{FFaker::Lorem.sentence}
+ #{doc_attachment}
+ TEXT
+ end
+
+ it 'fetches attachment urls' do
+ expect(described_class.fetch_attachment_urls(text))
+ .to contain_exactly(image_attachment, doc_attachment)
+ end
+ end
+
describe '#to_s' do
it 'returns the text when the author was found' do
author = double(:author, login: 'Alice')
diff --git a/spec/lib/gitlab/github_import/parallel_scheduling_spec.rb b/spec/lib/gitlab/github_import/parallel_scheduling_spec.rb
index 738e7c88d7d..860bb60f3ed 100644
--- a/spec/lib/gitlab/github_import/parallel_scheduling_spec.rb
+++ b/spec/lib/gitlab/github_import/parallel_scheduling_spec.rb
@@ -15,6 +15,10 @@ RSpec.describe Gitlab::GithubImport::ParallelScheduling do
Class
end
+ def sidekiq_worker_class
+ Class
+ end
+
def object_type
:dummy
end
diff --git a/spec/lib/gitlab/github_import/representation/diff_notes/suggestion_formatter_spec.rb b/spec/lib/gitlab/github_import/representation/diff_notes/suggestion_formatter_spec.rb
index bcb8575bdbf..5a24f929388 100644
--- a/spec/lib/gitlab/github_import/representation/diff_notes/suggestion_formatter_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/diff_notes/suggestion_formatter_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::DiffNotes::SuggestionFormatter do
it 'does nothing when there is any text before the suggestion tag' do
diff --git a/spec/lib/gitlab/github_import/representation/expose_attribute_spec.rb b/spec/lib/gitlab/github_import/representation/expose_attribute_spec.rb
index d40be0e841c..43f0198704f 100644
--- a/spec/lib/gitlab/github_import/representation/expose_attribute_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/expose_attribute_spec.rb
@@ -1,21 +1,41 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::ExposeAttribute do
- it 'defines a getter method that returns an attribute value' do
- klass = Class.new do
+ let(:klass) do
+ Class.new do
include Gitlab::GithubImport::Representation::ExposeAttribute
expose_attribute :number
attr_reader :attributes
- def initialize
- @attributes = { number: 42 }
+ def initialize(attributes)
+ @attributes = attributes
+ end
+ end
+ end
+
+ it 'defines a getter method that returns an attribute value' do
+ expect(klass.new({ number: 42 }).number).to eq(42)
+ end
+
+ describe '#[]' do
+ it 'returns exposed attributes value using array notation' do
+ expect(klass.new({ number: 42 })[:number]).to eq(42)
+ end
+
+ context 'when attribute does not exist' do
+ it 'returns nil' do
+ expect(klass.new({})[:number]).to eq(nil)
end
end
- expect(klass.new.number).to eq(42)
+ context 'when attribute is not exposed' do
+ it 'returns nil' do
+ expect(klass.new({ not_exposed_attribute: 42 })[:not_exposed_attribute]).to eq(nil)
+ end
+ end
end
end
diff --git a/spec/lib/gitlab/github_import/representation/issue_event_spec.rb b/spec/lib/gitlab/github_import/representation/issue_event_spec.rb
index d3a98035e73..0256858ecf1 100644
--- a/spec/lib/gitlab/github_import/representation/issue_event_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/issue_event_spec.rb
@@ -43,7 +43,7 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_actor) { false }
it 'does not return such info' do
- expect(issue_event.actor).to eq nil
+ expect(issue_event.actor).to be_nil
end
end
@@ -57,7 +57,7 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_label) { false }
it 'does not return such info' do
- expect(issue_event.label_title).to eq nil
+ expect(issue_event.label_title).to be_nil
end
end
@@ -72,8 +72,8 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_rename) { false }
it 'does not return such info' do
- expect(issue_event.old_title).to eq nil
- expect(issue_event.new_title).to eq nil
+ expect(issue_event.old_title).to be_nil
+ expect(issue_event.new_title).to be_nil
end
end
@@ -87,30 +87,47 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_milestone) { false }
it 'does not return such info' do
- expect(issue_event.milestone_title).to eq nil
+ expect(issue_event.milestone_title).to be_nil
end
end
- context 'when assignee and assigner data is present' do
- it 'includes assignee and assigner details' do
+ context 'when assignee data is present' do
+ it 'includes assignee details' do
expect(issue_event.assignee)
.to be_an_instance_of(Gitlab::GithubImport::Representation::User)
expect(issue_event.assignee.id).to eq(5)
expect(issue_event.assignee.login).to eq('tom')
+ end
+ end
+
+ context 'when assignee data is empty' do
+ let(:with_assignee) { false }
- expect(issue_event.assigner)
+ it 'does not return such info' do
+ expect(issue_event.assignee).to be_nil
+ end
+ end
+
+ context 'when requested_reviewer and review_requester data is present' do
+ it 'includes requested_reviewer and review_requester details' do
+ expect(issue_event.requested_reviewer)
.to be_an_instance_of(Gitlab::GithubImport::Representation::User)
- expect(issue_event.assigner.id).to eq(6)
- expect(issue_event.assigner.login).to eq('jerry')
+ expect(issue_event.requested_reviewer.id).to eq(6)
+ expect(issue_event.requested_reviewer.login).to eq('mickey')
+
+ expect(issue_event.review_requester)
+ .to be_an_instance_of(Gitlab::GithubImport::Representation::User)
+ expect(issue_event.review_requester.id).to eq(7)
+ expect(issue_event.review_requester.login).to eq('minnie')
end
end
- context 'when assignee and assigner data is empty' do
- let(:with_assignee) { false }
+ context 'when requested_reviewer and review_requester data is empty' do
+ let(:with_reviewer) { false }
it 'does not return such info' do
- expect(issue_event.assignee).to eq nil
- expect(issue_event.assigner).to eq nil
+ expect(issue_event.requested_reviewer).to be_nil
+ expect(issue_event.review_requester).to be_nil
end
end
@@ -148,7 +165,8 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:response) do
event_resource = Struct.new(
:id, :node_id, :url, :actor, :event, :commit_id, :commit_url, :label, :rename, :milestone,
- :source, :assignee, :assigner, :issue, :created_at, :performed_via_github_app,
+ :source, :assignee, :requested_reviewer, :review_requester, :issue, :created_at,
+ :performed_via_github_app,
keyword_init: true
)
user_resource = Struct.new(:id, :login, keyword_init: true)
@@ -166,7 +184,8 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
milestone: with_milestone ? { title: 'milestone title' } : nil,
source: { type: 'issue', id: 123456 },
assignee: with_assignee ? user_resource.new(id: 5, login: 'tom') : nil,
- assigner: with_assignee ? user_resource.new(id: 6, login: 'jerry') : nil,
+ requested_reviewer: with_reviewer ? user_resource.new(id: 6, login: 'mickey') : nil,
+ review_requester: with_reviewer ? user_resource.new(id: 7, login: 'minnie') : nil,
issue: { 'number' => 2, 'pull_request' => pull_request },
created_at: '2022-04-26 18:30:53 UTC',
performed_via_github_app: nil
@@ -178,6 +197,7 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_rename) { true }
let(:with_milestone) { true }
let(:with_assignee) { true }
+ let(:with_reviewer) { true }
let(:pull_request) { nil }
it_behaves_like 'an IssueEvent' do
@@ -203,7 +223,8 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
'milestone_title' => (with_milestone ? 'milestone title' : nil),
'source' => { 'type' => 'issue', 'id' => 123456 },
'assignee' => (with_assignee ? { 'id' => 5, 'login' => 'tom' } : nil),
- 'assigner' => (with_assignee ? { 'id' => 6, 'login' => 'jerry' } : nil),
+ 'requested_reviewer' => (with_reviewer ? { 'id' => 6, 'login' => 'mickey' } : nil),
+ 'review_requester' => (with_reviewer ? { 'id' => 7, 'login' => 'minnie' } : nil),
'issue' => { 'number' => 2, 'pull_request' => pull_request },
'created_at' => '2022-04-26 18:30:53 UTC',
'performed_via_github_app' => nil
@@ -215,6 +236,7 @@ RSpec.describe Gitlab::GithubImport::Representation::IssueEvent do
let(:with_rename) { true }
let(:with_milestone) { true }
let(:with_assignee) { true }
+ let(:with_reviewer) { true }
let(:pull_request) { nil }
let(:issue_event) { described_class.from_json_hash(hash) }
diff --git a/spec/lib/gitlab/github_import/representation/lfs_object_spec.rb b/spec/lib/gitlab/github_import/representation/lfs_object_spec.rb
index b59ea513436..6663a7366a5 100644
--- a/spec/lib/gitlab/github_import/representation/lfs_object_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/lfs_object_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::LfsObject do
describe '#github_identifiers' do
diff --git a/spec/lib/gitlab/github_import/representation/note_spec.rb b/spec/lib/gitlab/github_import/representation/note_spec.rb
index 97addcc1c98..9f416eb3c02 100644
--- a/spec/lib/gitlab/github_import/representation/note_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/note_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::Note do
let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
diff --git a/spec/lib/gitlab/github_import/representation/protected_branch_spec.rb b/spec/lib/gitlab/github_import/representation/protected_branch_spec.rb
new file mode 100644
index 00000000000..e762dc469c1
--- /dev/null
+++ b/spec/lib/gitlab/github_import/representation/protected_branch_spec.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Representation::ProtectedBranch do
+ shared_examples 'a ProtectedBranch rule' do
+ it 'returns an instance of ProtectedBranch' do
+ expect(protected_branch).to be_an_instance_of(described_class)
+ end
+
+ context 'with ProtectedBranch' do
+ it 'includes the protected branch ID (name)' do
+ expect(protected_branch.id).to eq 'main'
+ end
+
+ it 'includes the protected branch allow_force_pushes' do
+ expect(protected_branch.allow_force_pushes).to eq true
+ end
+ end
+ end
+
+ describe '.from_api_response' do
+ let(:response) do
+ response = Struct.new(:url, :allow_force_pushes, keyword_init: true)
+ allow_force_pushes = Struct.new(:enabled, keyword_init: true)
+ response.new(
+ url: 'https://example.com/branches/main/protection',
+ allow_force_pushes: allow_force_pushes.new(
+ enabled: true
+ )
+ )
+ end
+
+ it_behaves_like 'a ProtectedBranch rule' do
+ let(:protected_branch) { described_class.from_api_response(response) }
+ end
+ end
+
+ describe '.from_json_hash' do
+ it_behaves_like 'a ProtectedBranch rule' do
+ let(:hash) do
+ {
+ 'id' => 'main',
+ 'allow_force_pushes' => true
+ }
+ end
+
+ let(:protected_branch) { described_class.from_json_hash(hash) }
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/representation/pull_request_review_spec.rb b/spec/lib/gitlab/github_import/representation/pull_request_review_spec.rb
index f812fd85fbc..d6e7a8172f7 100644
--- a/spec/lib/gitlab/github_import/representation/pull_request_review_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/pull_request_review_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::PullRequestReview do
let(:submitted_at) { Time.new(2017, 1, 1, 12, 00).utc }
diff --git a/spec/lib/gitlab/github_import/representation/pull_request_spec.rb b/spec/lib/gitlab/github_import/representation/pull_request_spec.rb
index 925dba5b5a7..deb9535a845 100644
--- a/spec/lib/gitlab/github_import/representation/pull_request_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/pull_request_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::PullRequest do
let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
diff --git a/spec/lib/gitlab/github_import/representation/release_attachments_spec.rb b/spec/lib/gitlab/github_import/representation/release_attachments_spec.rb
new file mode 100644
index 00000000000..0ef9dad6a13
--- /dev/null
+++ b/spec/lib/gitlab/github_import/representation/release_attachments_spec.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::GithubImport::Representation::ReleaseAttachments do
+ shared_examples 'a Release attachments data' do
+ it 'returns an instance of ReleaseAttachments' do
+ expect(representation).to be_an_instance_of(described_class)
+ end
+
+ it 'includes release DB id' do
+ expect(representation.release_db_id).to eq 42
+ end
+
+ it 'includes release description' do
+ expect(representation.description).to eq 'Some text here..'
+ end
+ end
+
+ describe '.from_db_record' do
+ let(:release) { build_stubbed(:release, id: 42, description: 'Some text here..') }
+
+ it_behaves_like 'a Release attachments data' do
+ let(:representation) { described_class.from_db_record(release) }
+ end
+ end
+
+ describe '.from_json_hash' do
+ it_behaves_like 'a Release attachments data' do
+ let(:hash) do
+ {
+ 'release_db_id' => 42,
+ 'description' => 'Some text here..'
+ }
+ end
+
+ let(:representation) { described_class.from_json_hash(hash) }
+ end
+ end
+
+ describe '#github_identifiers' do
+ it 'returns a hash with needed identifiers' do
+ release_id = rand(100)
+ representation = described_class.new(release_db_id: release_id, description: 'text')
+
+ expect(representation.github_identifiers).to eq({ db_id: release_id })
+ end
+ end
+end
diff --git a/spec/lib/gitlab/github_import/representation/to_hash_spec.rb b/spec/lib/gitlab/github_import/representation/to_hash_spec.rb
index 2770e5c5397..739c832025c 100644
--- a/spec/lib/gitlab/github_import/representation/to_hash_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/to_hash_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::ToHash do
describe '#to_hash' do
diff --git a/spec/lib/gitlab/github_import/representation/user_spec.rb b/spec/lib/gitlab/github_import/representation/user_spec.rb
index 14204886e9b..d7219556ada 100644
--- a/spec/lib/gitlab/github_import/representation/user_spec.rb
+++ b/spec/lib/gitlab/github_import/representation/user_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::User do
shared_examples 'a User' do
diff --git a/spec/lib/gitlab/github_import/representation_spec.rb b/spec/lib/gitlab/github_import/representation_spec.rb
index 58c10c4a775..9a0ef45fc1d 100644
--- a/spec/lib/gitlab/github_import/representation_spec.rb
+++ b/spec/lib/gitlab/github_import/representation_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'fast_spec_helper'
RSpec.describe Gitlab::GithubImport::Representation do
describe '.symbolize_hash' do
diff --git a/spec/lib/gitlab/github_import/user_finder_spec.rb b/spec/lib/gitlab/github_import/user_finder_spec.rb
index d85e298785c..8ebbff31f64 100644
--- a/spec/lib/gitlab/github_import/user_finder_spec.rb
+++ b/spec/lib/gitlab/github_import/user_finder_spec.rb
@@ -68,10 +68,16 @@ RSpec.describe Gitlab::GithubImport::UserFinder, :clean_gitlab_redis_cache do
it_behaves_like 'user ID finder', :assignee
end
- context 'when the author_key parameter is :assigner' do
- let(:issue_event) { double('Gitlab::GithubImport::Representation::IssueEvent', assigner: user) }
+ context 'when the author_key parameter is :requested_reviewer' do
+ let(:issue_event) { double('Gitlab::GithubImport::Representation::IssueEvent', requested_reviewer: user) }
- it_behaves_like 'user ID finder', :assigner
+ it_behaves_like 'user ID finder', :requested_reviewer
+ end
+
+ context 'when the author_key parameter is :review_requester' do
+ let(:issue_event) { double('Gitlab::GithubImport::Representation::IssueEvent', review_requester: user) }
+
+ it_behaves_like 'user ID finder', :review_requester
end
end
end