summaryrefslogtreecommitdiff
path: root/lib/gitlab/zentao/client.rb
blob: 4da4631eecff305382cdecb6a62321e9eac8c33b (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
# 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) rescue {}
        active = response['deleted'] == '0'
        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)
      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)

        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)
        host = integration.api_url.presence || integration.url

        URI.parse(Gitlab::Utils.append_path(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