diff options
Diffstat (limited to 'app/services/jira_import')
-rw-r--r-- | app/services/jira_import/start_import_service.rb | 4 | ||||
-rw-r--r-- | app/services/jira_import/users_importer.rb | 43 | ||||
-rw-r--r-- | app/services/jira_import/users_mapper.rb | 31 |
3 files changed, 76 insertions, 2 deletions
diff --git a/app/services/jira_import/start_import_service.rb b/app/services/jira_import/start_import_service.rb index 59fd463022f..a06cc6df719 100644 --- a/app/services/jira_import/start_import_service.rb +++ b/app/services/jira_import/start_import_service.rb @@ -28,8 +28,8 @@ module JiraImport rescue => ex # in case project.save! raises an erorr Gitlab::ErrorTracking.track_exception(ex, project_id: project.id) + jira_import&.do_fail!(error_message: ex.message) build_error_response(ex.message) - jira_import.do_fail! end def build_jira_import @@ -62,7 +62,7 @@ module JiraImport end 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(_('Jira import is already running.')) if import_in_progress? diff --git a/app/services/jira_import/users_importer.rb b/app/services/jira_import/users_importer.rb new file mode 100644 index 00000000000..579d3675073 --- /dev/null +++ b/app/services/jira_import/users_importer.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module JiraImport + class UsersImporter + attr_reader :user, :project, :start_at, :result + + MAX_USERS = 50 + + def initialize(user, project, start_at) + @project = project + @start_at = start_at + @user = user + end + + def execute + Gitlab::JiraImport.validate_project_settings!(project, user: user) + + return ServiceResponse.success(payload: nil) if users.blank? + + result = UsersMapper.new(project, users).execute + ServiceResponse.success(payload: result) + rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error + Gitlab::ErrorTracking.track_exception(error, project_id: project.id, request: url) + ServiceResponse.error(message: "There was an error when communicating to Jira: #{error.message}") + rescue Projects::ImportService::Error => error + ServiceResponse.error(message: error.message) + end + + private + + def users + @users ||= client.get(url) + end + + def url + "/rest/api/2/users?maxResults=#{MAX_USERS}&startAt=#{start_at.to_i}" + end + + def client + @client ||= project.jira_service.client + end + end +end diff --git a/app/services/jira_import/users_mapper.rb b/app/services/jira_import/users_mapper.rb new file mode 100644 index 00000000000..31a3f721556 --- /dev/null +++ b/app/services/jira_import/users_mapper.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module JiraImport + class UsersMapper + attr_reader :project, :jira_users + + def initialize(project, jira_users) + @project = project + @jira_users = jira_users + end + + def execute + jira_users.to_a.map do |jira_user| + { + jira_account_id: jira_user['accountId'], + jira_display_name: jira_user['displayName'], + jira_email: jira_user['emailAddress'], + gitlab_id: match_user(jira_user) + } + end + end + + private + + # TODO: Matching user by email and displayName will be done as the part + # of follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/219023 + def match_user(jira_user) + nil + end + end +end |