2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::JiraImport::LabelsImporter 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(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, group: group) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:jira_service) { create(:jira_service, project: project) }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:importer) { described_class.new(project) }
|
|
|
|
|
|
|
|
subject { importer.execute }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_feature_flags(jira_issue_import: true)
|
2020-05-24 23:13:21 +05:30
|
|
|
stub_const('Gitlab::JiraImport::LabelsImporter::MAX_LABELS', 2)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute', :clean_gitlab_redis_cache do
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
|
|
|
stub_jira_service_test
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'when label is missing from jira import' do
|
|
|
|
let_it_be(:no_label_jira_import) { create(:jira_import_state, label: nil, project: project) }
|
|
|
|
|
|
|
|
it 'raises error' do
|
2020-05-24 23:13:21 +05:30
|
|
|
expect { subject }.to raise_error(Projects::ImportService::Error, 'Failed to find import label for Jira import.')
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when jira import label exists' do
|
|
|
|
let_it_be(:label) { create(:label) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:jira_import_with_label) { create(:jira_import_state, label: label, project: project) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:issue_label) { create(:label, project: project, title: 'bug') }
|
|
|
|
|
|
|
|
let(:jira_labels_1) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "isLast" => false, "values" => %w(backend bug) } }
|
|
|
|
let(:jira_labels_2) { { "maxResults" => 2, "startAt" => 2, "total" => 3, "isLast" => true, "values" => %w(feature) } }
|
|
|
|
|
|
|
|
context 'when labels are returned from jira' do
|
|
|
|
before do
|
|
|
|
client = double
|
|
|
|
expect(importer).to receive(:client).twice.and_return(client)
|
|
|
|
allow(client).to receive(:get).twice.and_return(jira_labels_1, jira_labels_2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'caches import label' do
|
|
|
|
expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.import_label_cache_key(project.id))).to be nil
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Gitlab::JiraImport.get_import_label_id(project.id).to_i).to eq(label.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls Gitlab::JiraImport::HandleLabelsService' do
|
|
|
|
expect(Gitlab::JiraImport::HandleLabelsService).to receive(:new).with(project, %w(backend bug)).and_return(double(execute: [1, 2]))
|
|
|
|
expect(Gitlab::JiraImport::HandleLabelsService).to receive(:new).with(project, %w(feature)).and_return(double(execute: [3]))
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no labels to be handled' do
|
|
|
|
shared_examples 'no labels handling' do
|
|
|
|
it 'does not call Gitlab::JiraImport::HandleLabelsService' do
|
|
|
|
expect(Gitlab::JiraImport::HandleLabelsService).not_to receive(:new)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "values" => [] } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
client = double
|
|
|
|
expect(importer).to receive(:client).and_return(client)
|
|
|
|
allow(client).to receive(:get).and_return(jira_labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the labels field is empty' do
|
|
|
|
let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "isLast" => true, "total" => 3, "values" => [] } }
|
|
|
|
|
|
|
|
it_behaves_like 'no labels handling'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the labels field is missing' do
|
|
|
|
let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "isLast" => true, "total" => 3 } }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it_behaves_like 'no labels handling'
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when the isLast argument is missing' do
|
|
|
|
let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "values" => %w(bug dev) } }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it_behaves_like 'no labels handling'
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|