Commit f1c58811 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'refactor-jira-import-validation' into 'master'

Move jira settings validation to JiraImport

See merge request gitlab-org/gitlab!33367
parents 5a94623a ed5048c9
...@@ -902,17 +902,6 @@ class Project < ApplicationRecord ...@@ -902,17 +902,6 @@ class Project < ApplicationRecord
latest_jira_import&.status || 'initial' latest_jira_import&.status || 'initial'
end end
def validate_jira_import_settings!(user: nil)
raise Projects::ImportService::Error, _('Jira integration not configured.') unless jira_service&.active?
if user
raise Projects::ImportService::Error, _('Cannot import because issues are not available in this project.') unless feature_available?(:issues, user)
raise Projects::ImportService::Error, _('You do not have permissions to run the import.') unless user.can?(:admin_project, self)
end
raise Projects::ImportService::Error, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless jira_service.test(nil)[:success]
end
def human_import_status_name def human_import_status_name
import_state&.human_status_name || 'none' import_state&.human_status_name || 'none'
end end
......
...@@ -62,7 +62,7 @@ module JiraImport ...@@ -62,7 +62,7 @@ module JiraImport
end end
def validate def validate
project.validate_jira_import_settings!(user: user) Gitlab::JiraImport.validate_project_settings!(project, user: user)
return build_error_response(_('Unable to find Jira project to import data from.')) if jira_project_key.blank? return build_error_response(_('Unable to find Jira project to import data from.')) if jira_project_key.blank?
return build_error_response(_('Jira import is already running.')) if import_in_progress? return build_error_response(_('Jira import is already running.')) if import_in_progress?
......
...@@ -13,7 +13,7 @@ module JiraImport ...@@ -13,7 +13,7 @@ module JiraImport
end end
def execute def execute
project.validate_jira_import_settings!(user: user) Gitlab::JiraImport.validate_project_settings!(project, user: user)
return ServiceResponse.success(payload: nil) if users.blank? return ServiceResponse.success(payload: nil) if users.blank?
......
...@@ -10,6 +10,18 @@ module Gitlab ...@@ -10,6 +10,18 @@ module Gitlab
ITEMS_MAPPER_CACHE_KEY = 'jira-import/items-mapper/%{project_id}/%{collection_type}/%{jira_isssue_id}' ITEMS_MAPPER_CACHE_KEY = 'jira-import/items-mapper/%{project_id}/%{collection_type}/%{jira_isssue_id}'
ALREADY_IMPORTED_ITEMS_CACHE_KEY = 'jira-importer/already-imported/%{project}/%{collection_type}' ALREADY_IMPORTED_ITEMS_CACHE_KEY = 'jira-importer/already-imported/%{project}/%{collection_type}'
def self.validate_project_settings!(project, user: nil)
if user
raise Projects::ImportService::Error, _('Cannot import because issues are not available in this project.') unless project.feature_available?(:issues, user)
raise Projects::ImportService::Error, _('You do not have permissions to run the import.') unless user.can?(:admin_project, project)
end
jira_service = project.jira_service
raise Projects::ImportService::Error, _('Jira integration not configured.') unless jira_service&.active?
raise Projects::ImportService::Error, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless jira_service.test(nil)[:success]
end
def self.jira_issue_cache_key(project_id, jira_issue_id) def self.jira_issue_cache_key(project_id, jira_issue_id)
ITEMS_MAPPER_CACHE_KEY % { project_id: project_id, collection_type: :issues, jira_isssue_id: jira_issue_id } ITEMS_MAPPER_CACHE_KEY % { project_id: project_id, collection_type: :issues, jira_isssue_id: jira_issue_id }
end end
......
...@@ -6,7 +6,7 @@ module Gitlab ...@@ -6,7 +6,7 @@ module Gitlab
attr_reader :project, :client, :formatter, :jira_project_key, :running_import attr_reader :project, :client, :formatter, :jira_project_key, :running_import
def initialize(project) def initialize(project)
project.validate_jira_import_settings! Gitlab::JiraImport.validate_project_settings!(project)
@running_import = project.latest_jira_import @running_import = project.latest_jira_import
@jira_project_key = running_import&.jira_project_key @jira_project_key = running_import&.jira_project_key
......
...@@ -14,7 +14,7 @@ describe Gitlab::JiraImport::BaseImporter do ...@@ -14,7 +14,7 @@ describe Gitlab::JiraImport::BaseImporter do
before do before do
stub_jira_service_test stub_jira_service_test
allow(project).to receive(:validate_jira_import_settings!) allow(Gitlab::JiraImport).to receive(:validate_project_settings!)
end end
context 'when Jira service exists' do context 'when Jira service exists' do
......
...@@ -5,6 +5,95 @@ require 'spec_helper' ...@@ -5,6 +5,95 @@ require 'spec_helper'
describe Gitlab::JiraImport do describe Gitlab::JiraImport do
let(:project_id) { 321 } let(:project_id) { 321 }
describe '.validate_project_settings!' do
include JiraServiceHelper
let_it_be(:project, reload: true) { create(:project) }
shared_examples 'raise Jira import error' do |message|
it 'returns error' do
expect { subject }.to raise_error(Projects::ImportService::Error, message)
end
end
shared_examples 'jira configuration base checks' do
context 'when Jira service was not setup' do
it_behaves_like 'raise Jira import error', 'Jira integration not configured.'
end
context 'when Jira service exists' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when Jira connection is not valid' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
.to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
end
it_behaves_like 'raise Jira import error', 'Unable to connect to the Jira instance. Please check your Jira integration configuration.'
end
end
end
before do
stub_jira_service_test
end
context 'without user param' do
subject { described_class.validate_project_settings!(project) }
it_behaves_like 'jira configuration base checks'
context 'when jira connection is valid' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
context 'with user param provided' do
let_it_be(:user) { create(:user) }
subject { described_class.validate_project_settings!(project, user: user) }
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
it_behaves_like 'jira configuration base checks'
context 'when jira service is configured' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when issues feature is disabled' do
let_it_be(:project, reload: true) { create(:project, :issues_disabled) }
it_behaves_like 'raise Jira import error', 'Cannot import because issues are not available in this project.'
end
context 'when everything is ok' do
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
end
context 'when user does not have permissions to run the import' do
before do
create(:jira_service, project: project, active: true)
project.add_developer(user)
end
it_behaves_like 'raise Jira import error', 'You do not have permissions to run the import.'
end
end
end
describe '.jira_issue_cache_key' do describe '.jira_issue_cache_key' do
it 'returns cache key for Jira issue imported to given project' do it 'returns cache key for Jira issue imported to given project' do
expect(described_class.jira_issue_cache_key(project_id, 'DEMO-123')).to eq("jira-import/items-mapper/#{project_id}/issues/DEMO-123") expect(described_class.jira_issue_cache_key(project_id, 'DEMO-123')).to eq("jira-import/items-mapper/#{project_id}/issues/DEMO-123")
......
...@@ -5999,99 +5999,6 @@ describe Project do ...@@ -5999,99 +5999,6 @@ describe Project do
end end
end end
describe '#validate_jira_import_settings!' do
include JiraServiceHelper
let_it_be(:project, reload: true) { create(:project) }
shared_examples 'raise Jira import error' do |message|
it 'returns error' do
expect { subject }.to raise_error(Projects::ImportService::Error, message)
end
end
shared_examples 'jira configuration base checks' do
context 'when Jira service was not setup' do
it_behaves_like 'raise Jira import error', 'Jira integration not configured.'
end
context 'when Jira service exists' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when Jira connection is not valid' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
.to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
end
it_behaves_like 'raise Jira import error', 'Unable to connect to the Jira instance. Please check your Jira integration configuration.'
end
end
end
before do
stub_jira_service_test
end
context 'without user param' do
subject { project.validate_jira_import_settings! }
it_behaves_like 'jira configuration base checks'
context 'when jira connection is valid' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
context 'with user param provided' do
let_it_be(:user) { create(:user) }
subject { project.validate_jira_import_settings!(user: user) }
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
it_behaves_like 'jira configuration base checks'
end
context 'when user does not have permissions to run the import' do
before do
create(:jira_service, project: project, active: true)
project.add_developer(user)
end
it_behaves_like 'raise Jira import error', 'You do not have permissions to run the import.'
end
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when issues feature is disabled' do
let_it_be(:project, reload: true) { create(:project, :issues_disabled) }
it_behaves_like 'raise Jira import error', 'Cannot import because issues are not available in this project.'
end
context 'when everything is ok' do
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
end
end
describe '#design_management_enabled?' do describe '#design_management_enabled?' do
let(:project) { build(:project) } let(:project) { build(:project) }
......
...@@ -13,7 +13,7 @@ describe JiraImport::StartImportService do ...@@ -13,7 +13,7 @@ describe JiraImport::StartImportService do
context 'when an error is returned from the project validation' do context 'when an error is returned from the project validation' do
before do before do
allow(project).to receive(:validate_jira_import_settings!) allow(Gitlab::JiraImport).to receive(:validate_project_settings!)
.and_raise(Projects::ImportService::Error, 'Jira import feature is disabled.') .and_raise(Projects::ImportService::Error, 'Jira import feature is disabled.')
end end
...@@ -25,7 +25,7 @@ describe JiraImport::StartImportService do ...@@ -25,7 +25,7 @@ describe JiraImport::StartImportService do
before do before do
stub_jira_service_test stub_jira_service_test
allow(project).to receive(:validate_jira_import_settings!) allow(Gitlab::JiraImport).to receive(:validate_project_settings!)
end end
context 'when Jira project key is not provided' do context 'when Jira project key is not provided' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment