summaryrefslogtreecommitdiff
path: root/rubocop/cop/gitlab/avoid_feature_category_not_owned.rb
blob: fb790f44a96c4c8dc895c10a4fae4d9a19aadbbb (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
# frozen_string_literal: true

require_relative '../../code_reuse_helpers'

module RuboCop
  module Cop
    module Gitlab
      class AvoidFeatureCategoryNotOwned < RuboCop::Cop::Cop
        include ::RuboCop::CodeReuseHelpers

        MSG = 'Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization'
        RESTRICT_ON_SEND = %i[feature_category get post put patch delete].freeze

        def_node_matcher :feature_category_not_owned?, <<~PATTERN
          (send _ :feature_category (sym :not_owned) ...)
        PATTERN

        def_node_matcher :feature_category_not_owned_api?, <<~PATTERN
          (send nil? {:get :post :put :patch :delete} _
            (hash <(pair (sym :feature_category) (sym :not_owned)) ...>)
          )
        PATTERN

        def on_send(node)
          return unless file_needs_feature_category?(node)
          return unless setting_not_owned?(node)

          add_offense(node, location: :expression)
        end

        private

        def file_needs_feature_category?(node)
          in_controller?(node) || in_worker?(node) || in_api?(node)
        end

        def setting_not_owned?(node)
          feature_category_not_owned?(node) || feature_category_not_owned_api?(node)
        end
      end
    end
  end
end