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

avoid_uploaded_file_from_params.rb « gitlab « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 599371aa5a18fe2044fff0b16b85b59e65b9d437 (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
# frozen_string_literal: true

module RuboCop
  module Cop
    module Gitlab
      # This cop checks for `UploadedFile.from_params` usage.
      # See https://docs.gitlab.com/ee/development/uploads.html#how-to-add-a-new-upload-route
      #
      # @example
      #
      #   # bad
      #   class MyAwfulApi < Grape::API::Instance
      #     params do
      #       optional 'file.path', type: String
      #       optional 'file.name', type: String
      #       optional 'file.type', type: String
      #       optional 'file.size', type: Integer
      #       optional 'file.md5', type: String
      #       optional 'file.sha1', type: String
      #       optional 'file.sha256', type: String
      #     end
      #     put '/files' do
      #       uploaded_file = UploadedFile.from_params(params, :file, FileUploader.workhorse_local_upload_path)
      #     end
      #   end
      #
      #   # good
      #   class MyMuchBetterApi < Grape::API::Instance
      #     params do
      #       requires :file, type: ::API::Validations::Types::WorkhorseFile
      #     end
      #     put '/files' do
      #       uploaded_file = declared_params[:file]
      #     end
      #   end
      class AvoidUploadedFileFromParams < RuboCop::Cop::Cop
        MSG = 'Use the `UploadedFile` set by `multipart.rb` instead of calling `UploadedFile.from_params` directly. See https://docs.gitlab.com/ee/development/uploads.html#how-to-add-a-new-upload-route'

        def_node_matcher :calling_uploaded_file_from_params?, <<~PATTERN
          (send (const nil? :UploadedFile) :from_params ...)
        PATTERN

        def on_send(node)
          return unless calling_uploaded_file_from_params?(node)

          add_offense(node, location: :expression)
        end
      end
    end
  end
end