summaryrefslogtreecommitdiff
path: root/app/services/jira/requests/base.rb
blob: 4ed8df0f2350e720a160664c68549042fb857d0c (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
# frozen_string_literal: true

module Jira
  module Requests
    class Base
      include ProjectServicesLoggable

      JIRA_API_VERSION = 2

      def initialize(jira_service, params = {})
        @project = jira_service&.project
        @jira_service = jira_service
      end

      def execute
        return ServiceResponse.error(message: _('Jira service not configured.')) unless jira_service&.active?

        request
      end

      def base_api_url
        "/rest/api/#{api_version}"
      end

      private

      attr_reader :jira_service, :project

      # override this method in the specific request class implementation if a differnt API version is required
      def api_version
        JIRA_API_VERSION
      end

      def client
        @client ||= jira_service.client
      end

      def request
        response = client.get(url)
        build_service_response(response)
      rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
        error_message = "Jira request error: #{error.message}"
        log_error("Error sending message", client_url: client.options[:site],
                  error: {
                    exception_class: error.class.name,
                    exception_message: error.message,
                    exception_backtrace: Gitlab::BacktraceCleaner.clean_backtrace(error.backtrace)
                  })
        ServiceResponse.error(message: error_message)
      end

      def url
        raise NotImplementedError
      end

      def build_service_response(response)
        raise NotImplementedError
      end
    end
  end
end