summaryrefslogtreecommitdiff
path: root/app/services/jira
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-06-18 11:18:50 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-06-18 11:18:50 +0000
commit8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch)
treea77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/services/jira
parent00b35af3db1abfe813a778f643dad221aad51fca (diff)
downloadgitlab-ce-8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781.tar.gz
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/services/jira')
-rw-r--r--app/services/jira/requests/base.rb52
-rw-r--r--app/services/jira/requests/projects.rb32
2 files changed, 84 insertions, 0 deletions
diff --git a/app/services/jira/requests/base.rb b/app/services/jira/requests/base.rb
new file mode 100644
index 00000000000..7521c7610cb
--- /dev/null
+++ b/app/services/jira/requests/base.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+module Jira
+ module Requests
+ class Base
+ include ProjectServicesLoggable
+
+ PER_PAGE = 50
+
+ attr_reader :jira_service, :project, :limit, :start_at, :query
+
+ def initialize(jira_service, limit: PER_PAGE, start_at: 0, query: nil)
+ @project = jira_service&.project
+ @jira_service = jira_service
+
+ @limit = limit
+ @start_at = start_at
+ @query = query
+ end
+
+ def execute
+ return ServiceResponse.error(message: _('Jira service not configured.')) unless jira_service&.active?
+ return ServiceResponse.success(payload: empty_payload) if limit.to_i <= 0
+
+ request
+ end
+
+ private
+
+ def client
+ @client ||= jira_service.client
+ end
+
+ def request
+ response = client.get(url)
+ build_service_response(response)
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
+ error_message = "Jira request error: #{error.message}"
+ log_error("Error sending message", client_url: client.options[:site], error: error_message)
+ ServiceResponse.error(message: error_message)
+ end
+
+ def url
+ raise NotImplementedError
+ end
+
+ def build_service_response(response)
+ raise NotImplementedError
+ end
+ end
+ end
+end
diff --git a/app/services/jira/requests/projects.rb b/app/services/jira/requests/projects.rb
new file mode 100644
index 00000000000..da464503211
--- /dev/null
+++ b/app/services/jira/requests/projects.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+module Jira
+ module Requests
+ class Projects < Base
+ extend ::Gitlab::Utils::Override
+
+ private
+
+ override :url
+ def url
+ '/rest/api/2/project/search?query=%{query}&maxResults=%{limit}&startAt=%{start_at}' %
+ { query: CGI.escape(query.to_s), limit: limit.to_i, start_at: start_at.to_i }
+ end
+
+ override :build_service_response
+ def build_service_response(response)
+ return ServiceResponse.success(payload: empty_payload) unless response['values'].present?
+
+ ServiceResponse.success(payload: { projects: map_projects(response), is_last: response['isLast'] })
+ end
+
+ def map_projects(response)
+ response['values'].map { |v| JIRA::Resource::Project.build(client, v) }
+ end
+
+ def empty_payload
+ { projects: [], is_last: true }
+ end
+ end
+ end
+end