summaryrefslogtreecommitdiff
path: root/rubocop/cop/gitlab/httparty.rb
blob: 215f18b6993f2ebe3fce105720b9dca21cec3bdf (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
require_relative '../../spec_helpers'

module RuboCop
  module Cop
    module Gitlab
      class HTTParty < RuboCop::Cop::Cop
        include SpecHelpers

        MSG_SEND = <<~EOL.freeze
          Avoid calling `HTTParty` directly. Instead, use the Gitlab::HTTP
          wrapper. To allow request to localhost or the private network set
          the option :allow_local_requests in the request call.
        EOL

        MSG_INCLUDE = <<~EOL.freeze
          Avoid including `HTTParty` directly. Instead, use the Gitlab::HTTP
          wrapper. To allow request to localhost or the private network set
          the option :allow_local_requests in the request call.
        EOL

        def_node_matcher :includes_httparty?, <<~PATTERN
          (send nil? :include (const nil? :HTTParty))
        PATTERN

        def_node_matcher :httparty_node?, <<~PATTERN
          (send (const nil? :HTTParty)...)
        PATTERN

        def on_send(node)
          return if in_spec?(node)

          add_offense(node, location: :expression, message: MSG_SEND) if httparty_node?(node)
          add_offense(node, location: :expression, message: MSG_INCLUDE) if includes_httparty?(node)
        end

        def autocorrect(node)
          if includes_httparty?(node)
            autocorrect_includes_httparty(node)
          else
            autocorrect_httparty_node(node)
          end
        end

        def autocorrect_includes_httparty(node)
          lambda do |corrector|
            corrector.remove(node.source_range)
          end
        end

        def autocorrect_httparty_node(node)
          _, method_name, *arg_nodes = *node

          replacement = "Gitlab::HTTP.#{method_name}(#{arg_nodes.map(&:source).join(', ')})"

          lambda do |corrector|
            corrector.replace(node.source_range, replacement)
          end
        end
      end
    end
  end
end