2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jira
|
|
|
|
module Requests
|
|
|
|
class Base
|
|
|
|
include ProjectServicesLoggable
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
JIRA_API_VERSION = 2
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def initialize(jira_service, params = {})
|
2020-06-23 00:09:42 +05:30
|
|
|
@project = jira_service&.project
|
|
|
|
@jira_service = jira_service
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return ServiceResponse.error(message: _('Jira service not configured.')) unless jira_service&.active?
|
|
|
|
|
|
|
|
request
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :jira_service, :project
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
# We have to add the context_path here because the Jira client is not taking it into account
|
2020-07-28 23:09:34 +05:30
|
|
|
def base_api_url
|
2021-02-22 17:27:13 +05:30
|
|
|
"#{context_path}/rest/api/#{api_version}"
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def context_path
|
|
|
|
client.options[:context_path].to_s
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# override this method in the specific request class implementation if a differnt API version is required
|
|
|
|
def api_version
|
|
|
|
JIRA_API_VERSION
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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}"
|
2021-01-03 14:25:43 +05:30
|
|
|
log_error("Error sending message", client_url: client.options[:site],
|
|
|
|
error: {
|
|
|
|
exception_class: error.class.name,
|
|
|
|
exception_message: error.message,
|
|
|
|
exception_backtrace: Gitlab::BacktraceCleaner.clean_backtrace(error.backtrace)
|
|
|
|
})
|
2020-06-23 00:09:42 +05:30
|
|
|
ServiceResponse.error(message: error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_service_response(response)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|