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
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def base_api_url
|
|
|
|
"/rest/api/#{api_version}"
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
attr_reader :jira_service, :project
|
|
|
|
|
|
|
|
# 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
|