Commit dde5da28 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Add a RuboCop to prevent new not_owned endpoints

I noticed that we still sometimes added these, so I figured a RuboCop
was the easiest way to prevent that in the future
parent ac64173e
# 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'
def_node_search :feature_category_not_owned?, <<~PATTERN
(send nil? :feature_category (sym :not_owned) $...)
PATTERN
def_node_search :feature_category_not_owned_api?, <<~PATTERN
(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
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/gitlab/avoid_feature_category_not_owned'
RSpec.describe RuboCop::Cop::Gitlab::AvoidFeatureCategoryNotOwned do
subject(:cop) { described_class.new }
shared_examples 'defining feature category on a class' do
it 'flags a method call on a class' do
expect_offense(<<~SOURCE)
feature_category :not_owned
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization
SOURCE
end
it 'flags a method call on a class with an array passed' do
expect_offense(<<~SOURCE)
feature_category :not_owned, [:index, :edit]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization
SOURCE
end
end
context 'in controllers' do
before do
allow(subject).to receive(:in_controller?).and_return(true)
end
it_behaves_like 'defining feature category on a class'
end
context 'in workers' do
before do
allow(subject).to receive(:in_worker?).and_return(true)
end
it_behaves_like 'defining feature category on a class'
end
context 'for grape endpoints' do
before do
expect(subject).to receive(:in_api?).and_return(true)
end
it_behaves_like 'defining feature category on a class'
it 'flags when passed as a hash for a Grape endpoint as keyword args' do
expect_offense(<<~SOURCE)
get :hello, feature_category: :not_owned
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization
SOURCE
end
it 'flags when passed as a hash for a Grape endpoint in a hash' do
expect_offense(<<~SOURCE)
get :hello, { feature_category: :not_owned, urgency: :low}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization
SOURCE
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment