summaryrefslogtreecommitdiff
path: root/rubocop/cop/scalability/file_uploads.rb
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/scalability/file_uploads.rb')
-rw-r--r--rubocop/cop/scalability/file_uploads.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/rubocop/cop/scalability/file_uploads.rb b/rubocop/cop/scalability/file_uploads.rb
new file mode 100644
index 00000000000..83017217e32
--- /dev/null
+++ b/rubocop/cop/scalability/file_uploads.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module Scalability
+ # This cop checks for `File` params in API
+ #
+ # @example
+ #
+ # # bad
+ # params do
+ # requires :file, type: File
+ # end
+ #
+ # params do
+ # optional :file, type: File
+ # end
+ #
+ # # good
+ # params do
+ # requires :file, type: ::API::Validations::Types::WorkhorseFile
+ # end
+ #
+ # params do
+ # optional :file, type: ::API::Validations::Types::WorkhorseFile
+ # end
+ #
+ class FileUploads < RuboCop::Cop::Cop
+ MSG = 'Do not upload files without workhorse acceleration. Please refer to https://docs.gitlab.com/ee/development/uploads.html'
+
+ def_node_search :file_type_params?, <<~PATTERN
+ (send nil? {:requires :optional} (sym _) (hash <(pair (sym :type)(const nil? :File)) ...>))
+ PATTERN
+
+ def_node_search :file_types_params?, <<~PATTERN
+ (send nil? {:requires :optional} (sym _) (hash <(pair (sym :types)(array <(const nil? :File) ...>)) ...>))
+ PATTERN
+
+ def be_file_param_usage?(node)
+ file_type_params?(node) || file_types_params?(node)
+ end
+
+ def on_send(node)
+ return unless be_file_param_usage?(node)
+
+ add_offense(find_file_param(node), location: :expression)
+ end
+
+ private
+
+ def find_file_param(node)
+ node.each_descendant.find { |children| file_node_pattern.match(children) }
+ end
+
+ def file_node_pattern
+ @file_node_pattern ||= RuboCop::NodePattern.new("(const nil? :File)")
+ end
+ end
+ end
+ end
+end