2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::JiraImport::IssuesImporter do
|
2020-05-24 23:13:21 +05:30
|
|
|
include JiraServiceHelper
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:current_user) { create(:user) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:jira_import) { create(:jira_import_state, project: project, user: current_user) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:jira_service) { create(:jira_service, project: project) }
|
|
|
|
|
|
|
|
subject { described_class.new(project) }
|
|
|
|
|
|
|
|
before do
|
2020-05-24 23:13:21 +05:30
|
|
|
stub_jira_service_test
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#imported_items_cache_key' do
|
|
|
|
it_behaves_like 'raise exception if not implemented'
|
|
|
|
it { expect(subject.imported_items_cache_key).to eq("jira-importer/already-imported/#{project.id}/issues") }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute', :clean_gitlab_redis_cache do
|
|
|
|
context 'when no returned issues' do
|
|
|
|
it 'does not schedule any import jobs' do
|
|
|
|
expect(subject).to receive(:fetch_issues).with(0).and_return([])
|
|
|
|
expect(subject).not_to receive(:already_imported?)
|
|
|
|
expect(subject).not_to receive(:mark_as_imported)
|
|
|
|
expect(Gitlab::JiraImport::ImportIssueWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
job_waiter = subject.execute
|
|
|
|
|
|
|
|
expect(job_waiter.jobs_remaining).to eq(0)
|
|
|
|
expect(Gitlab::JiraImport.get_issues_next_start_at(project.id)).to eq(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with results returned' do
|
2020-06-23 00:09:42 +05:30
|
|
|
jira_issue = Struct.new(:id)
|
|
|
|
let_it_be(:jira_issues) { [jira_issue.new(1), jira_issue.new(2)] }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def mock_issue_serializer(count, raise_exception_on_even_mocks: false)
|
2020-05-24 23:13:21 +05:30
|
|
|
serializer = instance_double(Gitlab::JiraImport::IssueSerializer, execute: { key: 'data' })
|
2020-06-23 00:09:42 +05:30
|
|
|
next_iid = project.issues.maximum(:iid).to_i
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
count.times do |i|
|
2020-06-23 00:09:42 +05:30
|
|
|
if raise_exception_on_even_mocks && i.even?
|
|
|
|
expect(Gitlab::JiraImport::IssueSerializer).to receive(:new)
|
|
|
|
.with(project, jira_issues[i], current_user.id, { iid: next_iid + 1 }).and_raise('Some error')
|
|
|
|
else
|
|
|
|
next_iid += 1
|
|
|
|
expect(Gitlab::JiraImport::IssueSerializer).to receive(:new)
|
|
|
|
.with(project, jira_issues[i], current_user.id, { iid: next_iid }).and_return(serializer)
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
context 'when single page of results is returned' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class.name}::BATCH_SIZE", 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules 2 import jobs' do
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[1]])
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).twice
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).twice.and_call_original
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.and_call_original
|
2020-05-24 23:13:21 +05:30
|
|
|
mock_issue_serializer(2)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
job_waiter = subject.execute
|
|
|
|
|
|
|
|
expect(job_waiter.jobs_remaining).to eq(2)
|
|
|
|
expect(Gitlab::JiraImport.get_issues_next_start_at(project.id)).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when importing some issue raises an exception' do
|
2020-04-22 19:07:51 +05:30
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
stub_const("#{described_class.name}::BATCH_SIZE", 3)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'schedules 2 import jobs' do
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[1]])
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).once
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).once.and_call_original
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.and_call_original
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception).once
|
|
|
|
mock_issue_serializer(2, raise_exception_on_even_mocks: true)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
job_waiter = subject.execute
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(job_waiter.jobs_remaining).to eq(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Gitlab::JiraImport.get_issues_next_start_at(project.id)).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when duplicate results are returned' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class.name}::BATCH_SIZE", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules 2 import jobs' do
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[0]])
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).once
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).once.and_call_original
|
|
|
|
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.times.and_call_original
|
2020-05-24 23:13:21 +05:30
|
|
|
mock_issue_serializer(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
job_waiter = subject.execute
|
|
|
|
|
|
|
|
expect(job_waiter.jobs_remaining).to eq(1)
|
|
|
|
expect(Gitlab::JiraImport.get_issues_next_start_at(project.id)).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|