diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-04 09:09:18 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-04 09:09:18 +0000 |
commit | 0d8bcdf77d609b3624541de767a0129aa0b7e8d2 (patch) | |
tree | 40a5aebae63c322c38660537adc433fc80dbb46d /rubocop | |
parent | c99b40d5a7f93e2d51c3716676ff7c345ca19f06 (diff) | |
download | gitlab-ce-0d8bcdf77d609b3624541de767a0129aa0b7e8d2.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/redis_queue_usage.rb | 38 | ||||
-rw-r--r-- | rubocop/cop/sidekiq_redis_call.rb | 18 |
2 files changed, 56 insertions, 0 deletions
diff --git a/rubocop/cop/redis_queue_usage.rb b/rubocop/cop/redis_queue_usage.rb new file mode 100644 index 00000000000..d993abc6327 --- /dev/null +++ b/rubocop/cop/redis_queue_usage.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + # This class complements Rubocop::Cop::SidekiqRedisCall by disallowing the use of + # Gitlab::Redis::Queues with the exception of initialising Sidekiq and monitoring. + class RedisQueueUsage < RuboCop::Cop::Base + MSG = 'Gitlab::Redis::Queues should only be used by Sidekiq initializers. '\ + 'Assignments or using its params to initialise another connection is not allowed.' + + def_node_matcher :calling_redis_queue_module_methods?, <<~PATTERN + (send (const (const (const nil? :Gitlab) :Redis) :Queues) ...) + PATTERN + + def_node_matcher :using_redis_queue_module_as_parameter?, <<~PATTERN + (send ... (const (const (const nil? :Gitlab) :Redis) :Queues)) + PATTERN + + def_node_matcher :redis_queue_assignment?, <<~PATTERN + ({lvasgn | ivasgn | cvasgn | gvasgn | casgn | masgn | op_asgn | or_asgn | and_asgn } ... + `(const (const (const nil? :Gitlab) :Redis) :Queues)) + PATTERN + + def on_send(node) + return unless using_redis_queue_module_as_parameter?(node) || calling_redis_queue_module_methods?(node) + + add_offense(node, message: MSG) + end + + # offenses caught in assignment may overlap with on_send + %i[on_lvasgn on_ivasgn on_cvasgn on_gvasgn on_casgn on_masgn on_op_asgn on_or_asgn on_and_asgn].each do |name| + define_method(name) do |node| + add_offense(node, message: MSG) if redis_queue_assignment?(node) + end + end + end + end +end diff --git a/rubocop/cop/sidekiq_redis_call.rb b/rubocop/cop/sidekiq_redis_call.rb new file mode 100644 index 00000000000..e4ae430f7c7 --- /dev/null +++ b/rubocop/cop/sidekiq_redis_call.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + # Cop that prevents manually setting a queue in Sidekiq workers. + class SidekiqRedisCall < RuboCop::Cop::Base + MSG = 'Refrain from directly using Sidekiq.redis unless for migration. For admin operations, use Sidekiq APIs.' + + def_node_matcher :using_sidekiq_redis?, <<~PATTERN + (send (const nil? :Sidekiq) :redis) + PATTERN + + def on_send(node) + add_offense(node, message: MSG) if using_sidekiq_redis?(node) + end + end + end +end |