debian-mirror-gitlab/rubocop/cop/gitlab/httparty.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2018-03-26 14:24:53 +05:30
module RuboCop
module Cop
module Gitlab
2022-10-11 01:57:18 +05:30
class HTTParty < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-03-11 19:13:27 +05:30
MSG_SEND = <<~EOL
2018-03-26 14:24:53 +05:30
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
2021-03-11 19:13:27 +05:30
MSG_INCLUDE = <<~EOL
2018-03-26 14:24:53 +05:30
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)
2022-10-11 01:57:18 +05:30
if httparty_node?(node)
add_offense(node, message: MSG_SEND) do |corrector|
_, method_name, *arg_nodes = *node
replacement = "Gitlab::HTTP.#{method_name}(#{arg_nodes.map(&:source).join(', ')})"
corrector.replace(node.source_range, replacement)
end
elsif includes_httparty?(node)
add_offense(node, message: MSG_INCLUDE) do |corrector|
corrector.remove(node.source_range)
end
2018-03-26 14:24:53 +05:30
end
end
end
end
end
end