Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query.rb « zentao « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d40ee80939a9342ceea15de1753371103410a49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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