summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-04 21:07:31 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-04 21:07:31 +0000
commit71221554dd9ddf30f73035c89f78164e001aa96d (patch)
treec56e0b2fc3dd16602183b78cb3f68aed211c5e77 /rubocop
parentb41cd8cb92d53454b2b160ba922d33801933a9cf (diff)
downloadgitlab-ce-71221554dd9ddf30f73035c89f78164e001aa96d.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/put_group_routes_under_scope.rb43
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 44 insertions, 0 deletions
diff --git a/rubocop/cop/put_group_routes_under_scope.rb b/rubocop/cop/put_group_routes_under_scope.rb
new file mode 100644
index 00000000000..bcdde01fdb5
--- /dev/null
+++ b/rubocop/cop/put_group_routes_under_scope.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # Checks for a group routes outside '/-/' scope.
+ # For more information see: https://gitlab.com/gitlab-org/gitlab/issues/29572
+ class PutGroupRoutesUnderScope < RuboCop::Cop::Cop
+ MSG = 'Put new group routes under /-/ scope'
+
+ def_node_matcher :dash_scope?, <<~PATTERN
+ (:send nil? :scope (hash <(pair (sym :path)(str "groups/*group_id/-")) ...>))
+ PATTERN
+
+ def on_send(node)
+ return unless in_group_routes?(node)
+ return unless resource?(node)
+ return unless outside_scope?(node)
+
+ add_offense(node)
+ end
+
+ def outside_scope?(node)
+ node.each_ancestor(:block).none? do |parent|
+ dash_scope?(parent.to_a.first)
+ end
+ end
+
+ def in_group_routes?(node)
+ path = node.location.expression.source_buffer.name
+ dirname = File.dirname(path)
+ filename = File.basename(path)
+
+ dirname.end_with?('config/routes') &&
+ filename.end_with?('group.rb')
+ end
+
+ def resource?(node)
+ node.method_name == :resource ||
+ node.method_name == :resources
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 9c9948e2a61..1465c73d570 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -15,6 +15,7 @@ require_relative 'cop/avoid_route_redirect_leading_slash'
require_relative 'cop/line_break_around_conditional_block'
require_relative 'cop/prefer_class_methods_over_module'
require_relative 'cop/put_project_routes_under_scope'
+require_relative 'cop/put_group_routes_under_scope'
require_relative 'cop/migration/add_column'
require_relative 'cop/migration/add_concurrent_foreign_key'
require_relative 'cop/migration/add_concurrent_index'