summaryrefslogtreecommitdiff
path: root/lib/gitlab/zentao/client.rb
blob: bdfa4b3a308e853188261641dab898abf66013af (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
# 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
        response = fetch_product(zentao_product_xid)

        active = response.fetch('deleted') == '0' rescue false

        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 = {})
        get("products/#{zentao_product_xid}/issues",
            params.reverse_merge(page: 1, limit: 20))
      end

      def fetch_issue(issue_id)
        get("issues/#{issue_id}")
      end

      private

      def get(path, params = {})
        options = { headers: headers, query: params }
        response = Gitlab::HTTP.get(url(path), options)

        return {} unless response.success?

        Gitlab::Json.parse(response.body)
      rescue JSON::ParserError
        {}
      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