2019-07-16 15:49:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Jira
|
|
|
|
# Gitlab JIRA HTTP client to be used with jira-ruby gem, this subclasses JIRA::HTTPClient.
|
|
|
|
# Uses Gitlab::HTTP to make requests to JIRA REST API.
|
2021-01-07 16:10:18 -05:00
|
|
|
# The parent class implementation can be found at: https://github.com/sumoheavy/jira-ruby/blob/master/lib/jira/http_client.rb
|
2019-07-16 15:49:47 -04:00
|
|
|
class HttpClient < JIRA::HttpClient
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
|
|
|
override :request
|
|
|
|
def request(*args)
|
|
|
|
result = make_request(*args)
|
|
|
|
|
2021-05-04 11:10:36 -04:00
|
|
|
raise JIRA::HTTPError, result.response unless result.response.is_a?(Net::HTTPSuccess)
|
2019-07-16 15:49:47 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
override :make_cookie_auth_request
|
|
|
|
def make_cookie_auth_request
|
|
|
|
body = {
|
|
|
|
username: @options.delete(:username),
|
|
|
|
password: @options.delete(:password)
|
|
|
|
}.to_json
|
|
|
|
|
2019-09-25 08:06:15 -04:00
|
|
|
make_request(:post, @options[:context_path] + '/rest/auth/1/session', body, 'Content-Type' => 'application/json')
|
2019-07-16 15:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :make_request
|
|
|
|
def make_request(http_method, path, body = '', headers = {})
|
|
|
|
request_params = { headers: headers }
|
|
|
|
request_params[:body] = body if body.present?
|
|
|
|
request_params[:headers][:Cookie] = get_cookies if options[:use_cookies]
|
|
|
|
request_params[:base_uri] = uri.to_s
|
|
|
|
request_params.merge!(auth_params)
|
|
|
|
|
2022-05-06 11:09:03 -04:00
|
|
|
if Feature.enabled?(:jira_raise_timeouts, type: :ops)
|
|
|
|
request_params[:open_timeout] = 2.minutes
|
|
|
|
request_params[:read_timeout] = 2.minutes
|
|
|
|
request_params[:write_timeout] = 2.minutes
|
|
|
|
end
|
|
|
|
|
2019-07-16 15:49:47 -04:00
|
|
|
result = Gitlab::HTTP.public_send(http_method, path, **request_params) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
@authenticated = result.response.is_a?(Net::HTTPOK)
|
|
|
|
store_cookies(result) if options[:use_cookies]
|
|
|
|
|
2021-08-12 02:10:10 -04:00
|
|
|
# This is needed to make response.to_s work. HTTParty::Response internal uses a Net::HTTPResponse as @response.
|
|
|
|
# When a block is used, Net::HTTPResponse#body will be a Net::ReadAdapter instead of a String.
|
|
|
|
# In this case HTTParty::Response.to_s will default to inspecting the Net::HTTPResponse class instead
|
|
|
|
# of returning the content of body.
|
|
|
|
# See https://github.com/jnunemaker/httparty/blob/v0.18.1/lib/httparty/response.rb#L86-L92
|
|
|
|
# See https://github.com/ruby/net-http/blob/v0.1.1/lib/net/http/response.rb#L346-L350
|
|
|
|
result.response.body = result.body
|
|
|
|
|
2019-07-16 15:49:47 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2021-01-07 16:10:18 -05:00
|
|
|
private
|
|
|
|
|
2019-07-16 15:49:47 -04:00
|
|
|
def auth_params
|
|
|
|
return {} unless @options[:username] && @options[:password]
|
|
|
|
|
|
|
|
{
|
|
|
|
basic_auth: {
|
|
|
|
username: @options[:username],
|
|
|
|
password: @options[:password]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cookies
|
|
|
|
cookie_array = @cookies.values.map { |cookie| "#{cookie.name}=#{cookie.value[0]}" }
|
|
|
|
cookie_array += Array(@options[:additional_cookies]) if @options.key?(:additional_cookies)
|
|
|
|
cookie_array.join('; ') if cookie_array.any?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|