diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-19 03:10:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-19 03:10:29 +0300 |
commit | 7e1b27cedb9aa52f13f3d4120dac8cdb6cfc0dda (patch) | |
tree | 00fadf81a71b6d3e69c3bdbb2103158a6a913cc8 /lib/gitlab/zentao | |
parent | 27f5b61b11bd3381662ab89e09492b6afdca57dd (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/zentao')
-rw-r--r-- | lib/gitlab/zentao/client.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/zentao/query.rb | 78 |
2 files changed, 88 insertions, 7 deletions
diff --git a/lib/gitlab/zentao/client.rb b/lib/gitlab/zentao/client.rb index bdfa4b3a308..e22d57faeca 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) - + response = fetch_product(zentao_product_xid) rescue {} active = response.fetch('deleted') == '0' rescue false - 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 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 unless response.success? Gitlab::Json.parse(response.body) rescue JSON::ParserError - {} + raise Gitlab::Zentao::Client::Error end def url(path) diff --git a/lib/gitlab/zentao/query.rb b/lib/gitlab/zentao/query.rb new file mode 100644 index 00000000000..8f3f7b6d316 --- /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 + + attr_reader :client, :params + + 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 + + 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 |