diff options
Diffstat (limited to 'rubocop/cop/gitlab/avoid_gitlab_instance_checks.rb')
-rw-r--r-- | rubocop/cop/gitlab/avoid_gitlab_instance_checks.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/avoid_gitlab_instance_checks.rb b/rubocop/cop/gitlab/avoid_gitlab_instance_checks.rb new file mode 100644 index 00000000000..962a58cfb4a --- /dev/null +++ b/rubocop/cop/gitlab/avoid_gitlab_instance_checks.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rubocop-rspec' + +module RuboCop + module Cop + module Gitlab + # This cop checks for use of gitlab instance specific checks. + # + # @example + # + # # bad + # if Gitlab.com? + # Ci::Runner::FORM_EDITABLE + Ci::Runner::MINUTES_COST_FACTOR_FIELDS + # else + # Ci::Runner::FORM_EDITABLE + # end + # + # # good + # if Gitlab::Saas.feature_available?('purchases/additional_minutes') + # Ci::Runner::FORM_EDITABLE + Ci::Runner::MINUTES_COST_FACTOR_FIELDS + # else + # Ci::Runner::FORM_EDITABLE + # end + # + class AvoidGitlabInstanceChecks < RuboCop::Cop::Base + MSG = 'Avoid the use of `%{name}`. Use Gitlab::Saas.feature_available?. ' \ + 'See https://docs.gitlab.com/ee/development/ee_features.html#saas-only-feature' + RESTRICT_ON_SEND = %i[ + com? com_except_jh? com_and_canary? com_but_not_canary? org_or_com? should_check_namespace_plan? + ].freeze + + # @!method gitlab?(node) + def_node_matcher :gitlab?, <<~PATTERN + (send (const {nil? (cbase)} :Gitlab) ...) + PATTERN + + # @!method should_check_namespace_plan?(node) + def_node_matcher :should_check_namespace_plan?, <<~PATTERN + (send + (const + (const + {nil? (cbase)} :Gitlab) :CurrentSettings) :should_check_namespace_plan?) + PATTERN + + def on_send(node) + return unless gitlab?(node) || should_check_namespace_plan?(node) + + add_offense(node, message: format(MSG, name: node.method_name)) + end + end + end + end +end |