2020-06-02 14:08:32 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jira
|
|
|
|
module Requests
|
|
|
|
class Base
|
|
|
|
include ProjectServicesLoggable
|
|
|
|
|
2020-07-01 11:08:45 -04:00
|
|
|
JIRA_API_VERSION = 2
|
2020-06-02 14:08:32 -04:00
|
|
|
|
2020-07-01 11:08:45 -04:00
|
|
|
def initialize(jira_service, params = {})
|
2020-06-02 14:08:32 -04:00
|
|
|
@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-01-28 10:09:06 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :jira_service, :project
|
|
|
|
|
2020-12-15 13:10:06 -05:00
|
|
|
# We have to add the context_path here because the Jira client is not taking it into account
|
2020-07-01 11:08:45 -04:00
|
|
|
def base_api_url
|
2020-12-15 13:10:06 -05:00
|
|
|
"#{context_path}/rest/api/#{api_version}"
|
2020-07-01 11:08:45 -04:00
|
|
|
end
|
|
|
|
|
2020-12-15 13:10:06 -05:00
|
|
|
def context_path
|
|
|
|
client.options[:context_path].to_s
|
|
|
|
end
|
|
|
|
|
2020-07-01 11:08:45 -04:00
|
|
|
# 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-02 14:08:32 -04:00
|
|
|
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
|
2020-06-08 20:08:47 -04:00
|
|
|
error_message = "Jira request error: #{error.message}"
|
2020-10-13 14:08:58 -04:00
|
|
|
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-02 14:08:32 -04:00
|
|
|
ServiceResponse.error(message: error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_service_response(response)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|