debian-mirror-gitlab/lib/gitlab/zentao/client.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
3.1 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
module Gitlab
module Zentao
class Client
Error = Class.new(StandardError)
ConfigError = Class.new(Error)
2022-09-01 20:07:04 +05:30
RequestError = Class.new(Error)
CACHE_MAX_SET_SIZE = 5_000
CACHE_TTL = 1.month.freeze
2021-11-11 11:23:49 +05:30
attr_reader :integration
def initialize(integration)
raise ConfigError, 'Please check your integration configuration.' unless integration
@integration = integration
end
def ping
2022-08-27 11:52:29 +05:30
response = begin
fetch_product(zentao_product_xid)
rescue StandardError
{}
end
2021-12-11 22:18:48 +05:30
active = response['deleted'] == '0'
2021-11-11 11:23:49 +05:30
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 = {})
2022-09-01 20:07:04 +05:30
get("products/#{zentao_product_xid}/issues", params).tap do |response|
mark_issues_as_seen_in_product(response['issues'])
end
2021-11-11 11:23:49 +05:30
end
def fetch_issue(issue_id)
2022-09-01 20:07:04 +05:30
raise Error, 'invalid issue id' unless issue_id_pattern.match(issue_id)
# Only return issues that are associated with the product configured in
# the integration. Due to a lack of available data in the ZenTao APIs, we
# can only determine if an issue belongs to a product if the issue was
# previously returned in the `#fetch_issues` call.
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/360372#note_1016963713
raise RequestError unless issue_seen_in_product?(issue_id)
2021-12-11 22:18:48 +05:30
2021-11-11 11:23:49 +05:30
get("issues/#{issue_id}")
end
private
2021-12-11 22:18:48 +05:30
def issue_id_pattern
/\A\S+-\d+\z/
end
2021-11-11 11:23:49 +05:30
def get(path, params = {})
options = { headers: headers, query: params }
response = Gitlab::HTTP.get(url(path), options)
2022-09-01 20:07:04 +05:30
raise RequestError unless response.success?
2021-11-11 11:23:49 +05:30
Gitlab::Json.parse(response.body)
rescue JSON::ParserError
2022-09-01 20:07:04 +05:30
raise Error, 'invalid response format'
2021-11-11 11:23:49 +05:30
end
def url(path)
2022-09-01 20:07:04 +05:30
URI.parse(Gitlab::Utils.append_path(integration.client_url, "api.php/v1/#{path}"))
2021-11-11 11:23:49 +05:30
end
def headers
{
'Content-Type': 'application/json',
'Token': integration.api_token
}
end
def zentao_product_xid
integration.zentao_product_xid
end
2022-09-01 20:07:04 +05:30
def issue_ids_cache_key
@issue_ids_cache_key ||= [
:zentao_product_issues,
OpenSSL::Digest::SHA256.hexdigest(integration.client_url),
zentao_product_xid
].join(':')
end
def issue_ids_cache
@issue_ids_cache ||= ::Gitlab::SetCache.new(expires_in: CACHE_TTL)
end
def mark_issues_as_seen_in_product(issues)
return unless issues && issue_ids_cache.count(issue_ids_cache_key) < CACHE_MAX_SET_SIZE
ids = issues.map { _1['id'] }
issue_ids_cache.write(issue_ids_cache_key, ids)
end
def issue_seen_in_product?(id)
issue_ids_cache.include?(issue_ids_cache_key, id)
end
2021-11-11 11:23:49 +05:30
end
end
end