summaryrefslogtreecommitdiff
path: root/lib/atlassian/jira_connect/client.rb
blob: 3922499f3a7bcfeb52a45ef9fa4d24f0fec6d718 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true

module Atlassian
  module JiraConnect
    class Client < Gitlab::HTTP
      def self.generate_update_sequence_id
        (Time.now.utc.to_f * 1000).round
      end

      def initialize(base_uri, shared_secret)
        @base_uri = base_uri
        @shared_secret = shared_secret
      end

      def send_info(project:, update_sequence_id: nil, **args)
        common = { project: project, update_sequence_id: update_sequence_id }
        dev_info = args.slice(:commits, :branches, :merge_requests)
        build_info = args.slice(:pipelines)
        deploy_info = args.slice(:deployments)
        ff_info = args.slice(:feature_flags)

        responses = []

        responses << store_dev_info(**common, **dev_info) if dev_info.present?
        responses << store_build_info(**common, **build_info) if build_info.present?
        responses << store_deploy_info(**common, **deploy_info) if deploy_info.present?
        responses << store_ff_info(**common, **ff_info) if ff_info.present?
        raise ArgumentError, 'Invalid arguments' if responses.empty?

        responses.compact
      end

      # Fetch user information for the given account.
      # https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-get
      def user_info(account_id)
        r = get('/rest/api/3/user', { accountId: account_id, expand: 'groups' })

        JiraUser.new(r.parsed_response) if r.code == 200
      end

      private

      def get(path, query_params)
        uri = URI.join(@base_uri, path)
        uri.query = URI.encode_www_form(query_params)

        self.class.get(uri, headers: headers(uri, 'GET'))
      end

      def store_ff_info(project:, feature_flags:, **opts)
        items = feature_flags.map { |flag| ::Atlassian::JiraConnect::Serializers::FeatureFlagEntity.represent(flag, opts) }
        items.reject! { |item| item.issue_keys.empty? }

        return if items.empty?

        r = post('/rest/featureflags/0.1/bulk', {
          flags: items,
          properties: { projectId: "project-#{project.id}" }
        })

        handle_response(r, 'feature flags') do |data|
          failed = data['failedFeatureFlags']
          if failed.present?
            errors = failed.flat_map do |k, errs|
              errs.map { |e| "#{k}: #{e['message']}" }
            end
            { 'errorMessages' => errors }
          end
        end
      end

      def store_deploy_info(project:, deployments:, **opts)
        items = deployments.map { |d| ::Atlassian::JiraConnect::Serializers::DeploymentEntity.represent(d, opts) }
        items.reject! { |d| d.issue_keys.empty? }

        return if items.empty?

        r = post('/rest/deployments/0.1/bulk', { deployments: items })
        handle_response(r, 'deployments') { |data| errors(data, 'rejectedDeployments', r) }
      end

      def store_build_info(project:, pipelines:, update_sequence_id: nil)
        builds = pipelines.map do |pipeline|
          build = ::Atlassian::JiraConnect::Serializers::BuildEntity.represent(
            pipeline,
            update_sequence_id: update_sequence_id
          )
          next if build.issue_keys.empty?

          build
        end.compact
        return if builds.empty?

        r = post('/rest/builds/0.1/bulk', { builds: builds })
        handle_response(r, 'builds') { |data| errors(data, 'rejectedBuilds', r) }
      end

      def store_dev_info(project:, commits: nil, branches: nil, merge_requests: nil, update_sequence_id: nil)
        repo = ::Atlassian::JiraConnect::Serializers::RepositoryEntity.represent(
          project,
          commits: commits,
          branches: branches,
          merge_requests: merge_requests,
          user_notes_count: user_notes_count(merge_requests),
          update_sequence_id: update_sequence_id
        )

        post('/rest/devinfo/0.10/bulk', { repositories: [repo] })
      end

      def post(path, payload)
        uri = URI.join(@base_uri, path)

        self.class.post(uri, headers: headers(uri), body: metadata.merge(payload).to_json)
      end

      def headers(uri, http_method = 'POST')
        {
          'Authorization' => "JWT #{jwt_token(http_method, uri)}",
          'Content-Type' => 'application/json',
          'Accept' => 'application/json'
        }
      end

      def metadata
        { providerMetadata: { product: "GitLab #{Gitlab::VERSION}" } }
      end

      def handle_response(response, name, &block)
        data = response.parsed_response

        if [200, 202].include?(response.code)
          yield data
        else
          case response.code
          when 400 then { 'errorMessages' => data.map { |e| e['message'] } }
          when 401 then { 'errorMessages' => ['Invalid JWT'] }
          when 403 then { 'errorMessages' => ["App does not support #{name}"] }
          when 413 then { 'errorMessages' => ['Data too large'] + data.map { |e| e['message'] } }
          when 429 then { 'errorMessages' => ['Rate limit exceeded'] }
          when 503 then { 'errorMessages' => ['Service unavailable'] }
          else
            { 'errorMessages' => ['Unknown error'], 'response' => data }
          end.merge('responseCode' => response.code)
        end
      end

      def errors(data, key, response)
        messages = if data[key].present?
                     data[key].flat_map do |rejection|
                       rejection['errors'].map { |e| e['message'] }
                     end
                   else
                     []
                   end

        { 'errorMessages' => messages, 'responseCode' => response.code, 'requestBody' => request_body_schema(response) }
      end

      def request_body_schema(response)
        Oj.load(response.request.raw_body)
      rescue Oj::ParseError, EncodingError, Encoding::UndefinedConversionError
        'Request body includes invalid JSON'
      end

      def user_notes_count(merge_requests)
        return unless merge_requests

        Note.count_for_collection(merge_requests.map(&:id), 'MergeRequest').to_h do |count_group|
          [count_group.noteable_id, count_group.count]
        end
      end

      def jwt_token(http_method, uri)
        claims = Atlassian::Jwt.build_claims(
          Atlassian::JiraConnect.app_key,
          uri,
          http_method,
          @base_uri
        )

        Atlassian::Jwt.encode(claims, @shared_secret)
      end
    end
  end
end