2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2021-03-10 16:11:14 +00:00
|
|
|
|
2021-08-23 14:51:16 +00:00
|
|
|
require 'uri'
|
|
|
|
|
2021-03-10 16:11:14 +00:00
|
|
|
class GitLab
|
2021-03-24 16:41:05 +00:00
|
|
|
class HttpClient
|
|
|
|
attr_reader :api_token, :endpoint
|
2021-03-10 16:11:14 +00:00
|
|
|
|
|
|
|
def initialize(endpoint, api_token)
|
|
|
|
raise 'api_token required' if api_token.blank?
|
2021-08-23 14:51:16 +00:00
|
|
|
raise 'endpoint required' if endpoint.blank? || endpoint.exclude?('/graphql') || endpoint.scan(URI::DEFAULT_PARSER.make_regexp).blank?
|
2021-03-10 16:11:14 +00:00
|
|
|
|
|
|
|
@api_token = api_token
|
2021-03-24 16:41:05 +00:00
|
|
|
@endpoint = endpoint
|
|
|
|
end
|
|
|
|
|
2021-11-04 10:10:56 +00:00
|
|
|
# returns path of the subfolder of the endpoint if exists
|
|
|
|
def endpoint_path
|
|
|
|
path = URI.parse(endpoint).path
|
|
|
|
return if path.blank?
|
|
|
|
return if path == '/api/graphql'
|
|
|
|
|
|
|
|
if path.start_with?('/')
|
|
|
|
path = path[1..]
|
|
|
|
end
|
|
|
|
|
|
|
|
path.sub('api/graphql', '')
|
|
|
|
end
|
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
def perform(payload)
|
|
|
|
response = UserAgent.post(
|
|
|
|
endpoint,
|
|
|
|
payload,
|
|
|
|
{
|
|
|
|
headers: headers,
|
|
|
|
json: true,
|
|
|
|
open_timeout: 6,
|
|
|
|
read_timeout: 16,
|
|
|
|
log: {
|
|
|
|
facility: 'GitLab',
|
|
|
|
},
|
2021-07-20 13:31:46 +00:00
|
|
|
verify_ssl: true,
|
2021-03-24 16:41:05 +00:00
|
|
|
},
|
|
|
|
)
|
2021-03-10 16:11:14 +00:00
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
if !response.success?
|
|
|
|
Rails.logger.error response.error
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('GitLab request failed! Please have a look at the log file for details')
|
2021-03-24 16:41:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
response.data
|
2021-03-10 16:11:14 +00:00
|
|
|
end
|
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def headers
|
2021-03-10 16:11:14 +00:00
|
|
|
{
|
2021-06-01 11:38:34 +00:00
|
|
|
'PRIVATE-TOKEN': @api_token
|
2021-03-10 16:11:14 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|