2021-08-31 08:11:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Zentao
|
|
|
|
class Client
|
|
|
|
Error = Class.new(StandardError)
|
|
|
|
ConfigError = Class.new(Error)
|
|
|
|
|
|
|
|
attr_reader :integration
|
|
|
|
|
|
|
|
def initialize(integration)
|
|
|
|
raise ConfigError, 'Please check your integration configuration.' unless integration
|
|
|
|
|
|
|
|
@integration = integration
|
|
|
|
end
|
|
|
|
|
|
|
|
def ping
|
2021-10-18 20:10:29 -04:00
|
|
|
response = fetch_product(zentao_product_xid) rescue {}
|
2021-10-20 20:12:27 -04:00
|
|
|
active = response['deleted'] == '0'
|
2021-08-31 08:11:07 -04:00
|
|
|
if active
|
|
|
|
{ success: true }
|
|
|
|
else
|
|
|
|
{ success: false, message: 'Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_product(product_id)
|
|
|
|
get("products/#{product_id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_issues(params = {})
|
2021-10-18 20:10:29 -04:00
|
|
|
get("products/#{zentao_product_xid}/issues", params)
|
2021-08-31 08:11:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_issue(issue_id)
|
2021-10-21 20:13:18 -04:00
|
|
|
raise Gitlab::Zentao::Client::Error, 'invalid issue id' unless issue_id_pattern.match(issue_id)
|
2021-10-18 20:10:29 -04:00
|
|
|
|
2021-08-31 08:11:07 -04:00
|
|
|
get("issues/#{issue_id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-18 20:10:29 -04:00
|
|
|
def issue_id_pattern
|
|
|
|
/\A\S+-\d+\z/
|
|
|
|
end
|
|
|
|
|
2021-08-31 08:11:07 -04:00
|
|
|
def get(path, params = {})
|
|
|
|
options = { headers: headers, query: params }
|
|
|
|
response = Gitlab::HTTP.get(url(path), options)
|
|
|
|
|
2021-10-21 20:13:18 -04:00
|
|
|
raise Gitlab::Zentao::Client::Error, 'request error' unless response.success?
|
2021-08-31 08:11:07 -04:00
|
|
|
|
|
|
|
Gitlab::Json.parse(response.body)
|
|
|
|
rescue JSON::ParserError
|
2021-10-21 20:13:18 -04:00
|
|
|
raise Gitlab::Zentao::Client::Error, 'invalid response format'
|
2021-08-31 08:11:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def url(path)
|
|
|
|
host = integration.api_url.presence || integration.url
|
|
|
|
|
|
|
|
URI.join(host, '/api.php/v1/', path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def headers
|
|
|
|
{
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Token': integration.api_token
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def zentao_product_xid
|
|
|
|
integration.zentao_product_xid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|