diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-11 18:10:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-11 18:10:03 +0300 |
commit | 14ae125e1c59ca3e9b535938707831c986dbbc43 (patch) | |
tree | 6cb2244b7aafe63586d5e1c538ab8e1385db4163 /rubocop/cop/sidekiq_api_usage.rb | |
parent | fd31bd1fc7954f69025d8e6bbe7f772ea9fb4bb0 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop/cop/sidekiq_api_usage.rb')
-rw-r--r-- | rubocop/cop/sidekiq_api_usage.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/rubocop/cop/sidekiq_api_usage.rb b/rubocop/cop/sidekiq_api_usage.rb new file mode 100644 index 00000000000..35e5ec474cd --- /dev/null +++ b/rubocop/cop/sidekiq_api_usage.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + class SidekiqApiUsage < RuboCop::Cop::Base + MSG = 'Refrain from directly using Sidekiq APIs.' \ + 'Only permitted in migrations, administrations and Sidekiq middlewares.' + + ALLOWED_WORKER_METHODS = [ + :skipping_transaction_check, + :raise_inside_transaction_exception, + :raise_exception_for_being_inside_a_transaction? + ].freeze + + def_node_matcher :using_sidekiq_api?, <<~PATTERN + (send (const (const nil? :Sidekiq) $_ ) $... ) + PATTERN + + def on_send(node) + using_sidekiq_api?(node) do |klass, methods_called| + next if klass == :Testing + + # allow methods defined in config/initializers/forbid_sidekiq_in_transactions.rb + next if klass == :Worker && ALLOWED_WORKER_METHODS.include?(methods_called[0]) + + add_offense(node, message: MSG) + end + end + end + end +end |