Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/gitlab/service_response.rb')
-rw-r--r--rubocop/cop/gitlab/service_response.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/service_response.rb b/rubocop/cop/gitlab/service_response.rb
new file mode 100644
index 00000000000..edde662a038
--- /dev/null
+++ b/rubocop/cop/gitlab/service_response.rb
@@ -0,0 +1,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