summaryrefslogtreecommitdiff
path: root/rubocop/cop/gitlab/service_response.rb
blob: edde662a0384bb44ba7a4df2f74de55e67625dc5 (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
# frozen_string_literal: true

require_relative '../../code_reuse_helpers'

module RuboCop
  module Cop
    module Gitlab
      class ServiceResponse < ::RuboCop::Cop::Base
        include CodeReuseHelpers

        # This cop checks that ServiceResponse object is not used with the
        # deprecated attribute `http_status`.
        #
        # @example
        #
        #   # bad
        #   ServiceResponse.error(message: "...", http_status: :forbidden)
        #
        #   # good
        #   ServiceResponse.error(message: "...", reason: :insufficient_permissions)
        MSG = 'Use `reason` instead of the deprecated `http_status`: https://gitlab.com/gitlab-org/gitlab/-/issues/356036'

        RESTRICT_ON_SEND = %i[error success new].freeze
        METHOD_NAMES = RESTRICT_ON_SEND.map(&:inspect).join(' ').freeze

        def_node_matcher :service_response_with_http_status, <<~PATTERN
          (send
            (const {nil? cbase} :ServiceResponse)
            {#{METHOD_NAMES}}
            (hash <$(pair (sym :http_status) _) ...>)
          )
        PATTERN

        def on_send(node)
          pair = service_response_with_http_status(node)
          return unless pair

          add_offense(pair)
        end
      end
    end
  end
end