diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-30 21:08:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-30 21:08:07 +0300 |
commit | 2c72daf2f1744f2b8c8c6674c266907e9ef55558 (patch) | |
tree | e489b6e87557d3f6d8a94f2e7d4d47e633d646b5 /app/services/jira_import | |
parent | 4e9acbfba3682c552b3de707c535e6257ef41054 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/jira_import')
-rw-r--r-- | app/services/jira_import/start_import_service.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/app/services/jira_import/start_import_service.rb b/app/services/jira_import/start_import_service.rb new file mode 100644 index 00000000000..91a7956e585 --- /dev/null +++ b/app/services/jira_import/start_import_service.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module JiraImport + class StartImportService + attr_reader :user, :project, :jira_project_key + + def initialize(user, project, jira_project_key) + @user = user + @project = project + @jira_project_key = jira_project_key + end + + def execute + validation_response = validate + return validation_response if validation_response&.error? + + create_and_schedule_import + end + + private + + def create_and_schedule_import + import_data = project.create_or_update_import_data(data: {}).becomes(JiraImportData) + jira_project_details = JiraImportData::JiraProjectDetails.new( + jira_project_key, + Time.now.strftime('%Y-%m-%d %H:%M:%S'), + { user_id: user.id, name: user.name } + ) + import_data << jira_project_details + import_data.force_import! + + project.import_type = 'jira' + project.import_state.schedule if project.save! + + ServiceResponse.success(payload: { import_data: import_data } ) + rescue => ex + # in case project.save! raises an erorr + Gitlab::ErrorTracking.track_exception(ex, project_id: project.id) + build_error_response(ex.message) + end + + def validate + return build_error_response(_('Jira import feature is disabled.')) unless Feature.enabled?(:jira_issue_import, project) + return build_error_response(_('You do not have permissions to run the import.')) unless user.can?(:admin_project, project) + return build_error_response(_('Jira integration not configured.')) unless project.jira_service&.active? + 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? + end + + def build_error_response(message) + import_data = JiraImportData.new(project: project) + import_data.errors.add(:base, message) + ServiceResponse.error( + message: import_data.errors.full_messages.to_sentence, + http_status: 400, + payload: { import_data: import_data } + ) + end + + def import_in_progress? + import_state = project.import_state || project.create_import_state + import_state.in_progress? + end + end +end |