diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
commit | 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 (patch) | |
tree | 07e7870bca8aed6d61fdcc810731c50d2c40af47 /lib/gitlab/zentao | |
parent | 27909cef6c4170ed9205afa7426b8d3de47cbb0c (diff) |
Add latest changes from gitlab-org/gitlab@14-5-stable-eev14.5.0-rc42
Diffstat (limited to 'lib/gitlab/zentao')
-rw-r--r-- | lib/gitlab/zentao/client.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/zentao/query.rb | 78 |
2 files changed, 89 insertions, 8 deletions
diff --git a/lib/gitlab/zentao/client.rb b/lib/gitlab/zentao/client.rb index bdfa4b3a308..8acfb4913f3 100644 --- a/lib/gitlab/zentao/client.rb +++ b/lib/gitlab/zentao/client.rb @@ -15,10 +15,8 @@ module Gitlab end def ping - response = fetch_product(zentao_product_xid) - - active = response.fetch('deleted') == '0' rescue false - + response = fetch_product(zentao_product_xid) rescue {} + active = response['deleted'] == '0' if active { success: true } else @@ -31,25 +29,30 @@ module Gitlab end def fetch_issues(params = {}) - get("products/#{zentao_product_xid}/issues", - params.reverse_merge(page: 1, limit: 20)) + get("products/#{zentao_product_xid}/issues", params) end def fetch_issue(issue_id) + raise Gitlab::Zentao::Client::Error, 'invalid issue id' unless issue_id_pattern.match(issue_id) + get("issues/#{issue_id}") end private + def issue_id_pattern + /\A\S+-\d+\z/ + end + def get(path, params = {}) options = { headers: headers, query: params } response = Gitlab::HTTP.get(url(path), options) - return {} unless response.success? + raise Gitlab::Zentao::Client::Error, 'request error' unless response.success? Gitlab::Json.parse(response.body) rescue JSON::ParserError - {} + raise Gitlab::Zentao::Client::Error, 'invalid response format' end def url(path) diff --git a/lib/gitlab/zentao/query.rb b/lib/gitlab/zentao/query.rb new file mode 100644 index 00000000000..d40ee80939a --- /dev/null +++ b/lib/gitlab/zentao/query.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +module Gitlab + module Zentao + class Query + STATUSES = %w[all opened closed].freeze + ISSUES_DEFAULT_LIMIT = 20 + ISSUES_MAX_LIMIT = 50 + + def initialize(integration, params) + @client = Client.new(integration) + @params = params + end + + def issues + issues_response = client.fetch_issues(query_options) + return [] if issues_response.blank? + + Kaminari.paginate_array( + issues_response['issues'], + limit: issues_response['limit'], + total_count: issues_response['total'] + ) + end + + def issue + issue_response = client.fetch_issue(params[:id]) + issue_response['issue'] + end + + private + + attr_reader :client, :params + + def query_options + { + order: query_order, + status: query_status, + labels: query_labels, + page: query_page, + limit: query_limit, + search: query_search + } + end + + def query_page + params[:page].presence || 1 + end + + def query_limit + limit = params[:limit].presence || ISSUES_DEFAULT_LIMIT + [limit.to_i, ISSUES_MAX_LIMIT].min + end + + def query_search + params[:search] || '' + end + + def query_order + key, order = params['sort'].to_s.split('_', 2) + zentao_key = (key == 'created' ? 'openedDate' : 'lastEditedDate') + zentao_order = (order == 'asc' ? 'asc' : 'desc') + + "#{zentao_key}_#{zentao_order}" + end + + def query_status + return params[:state] if params[:state].present? && params[:state].in?(STATUSES) + + 'opened' + end + + def query_labels + (params[:labels].presence || []).join(',') + end + end + end +end |