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

client.rb « zentao « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9e89b99a277811495fbf46642c2c8bb1dda26fc (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true

module Gitlab
  module Zentao
    class Client
      Error = Class.new(StandardError)
      ConfigError = Class.new(Error)
      RequestError = Class.new(Error)

      CACHE_MAX_SET_SIZE = 5_000
      CACHE_TTL = 1.month.freeze

      attr_reader :integration

      def initialize(integration)
        raise ConfigError, 'Please check your integration configuration.' unless integration

        @integration = integration
      end

      def ping
        response = begin
          fetch_product(zentao_product_xid)
        rescue StandardError
          {}
        end
        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).tap do |response|
          mark_issues_as_seen_in_product(response['issues'])
        end
      end

      def fetch_issue(issue_id)
        raise Error, 'invalid issue id' unless issue_id_pattern.match(issue_id)

        # Only return issues that are associated with the product configured in
        # the integration. Due to a lack of available data in the ZenTao APIs, we
        # can only determine if an issue belongs to a product if the issue was
        # previously returned in the `#fetch_issues` call.
        #
        # See https://gitlab.com/gitlab-org/gitlab/-/issues/360372#note_1016963713
        raise RequestError unless issue_seen_in_product?(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 RequestError unless response.success?

        Gitlab::Json.parse(response.body)
      rescue JSON::ParserError
        raise Error, 'invalid response format'
      end

      def url(path)
        URI.parse(Gitlab::Utils.append_path(integration.client_url, "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

      def issue_ids_cache_key
        @issue_ids_cache_key ||= [
          :zentao_product_issues,
          OpenSSL::Digest::SHA256.hexdigest(integration.client_url),
          zentao_product_xid
        ].join(':')
      end

      def issue_ids_cache
        @issue_ids_cache ||= ::Gitlab::SetCache.new(expires_in: CACHE_TTL)
      end

      def mark_issues_as_seen_in_product(issues)
        return unless issues && issue_ids_cache.count(issue_ids_cache_key) < CACHE_MAX_SET_SIZE

        ids = issues.map { _1['id'] }

        issue_ids_cache.write(issue_ids_cache_key, ids)
      end

      def issue_seen_in_product?(id)
        issue_ids_cache.include?(issue_ids_cache_key, id)
      end
    end
  end
end