diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-14 21:13:23 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-14 21:13:23 +0300 |
commit | 4db74ea1477fe70d132bf75f1bf3f1a728fc72e4 (patch) | |
tree | 2d03a0671637feb98849037f283c36ae60a6cd97 /rubocop | |
parent | 1123408ec8922d4a6908ef4b85aa0ff4fbea08fe (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/gitlab/event_store_subscriber.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/event_store_subscriber.rb b/rubocop/cop/gitlab/event_store_subscriber.rb new file mode 100644 index 00000000000..0b2dd94bc42 --- /dev/null +++ b/rubocop/cop/gitlab/event_store_subscriber.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Gitlab + # Cop that checks the implementation of Gitlab::EventStore::Subscriber + # + # A worker that implements Gitlab::EventStore::Subscriber + # must implement the method #handle_event(event) and + # must not override the method #perform(*args) + # + # # bad + # class MySubscriber + # include Gitlab::EventStore::Subscriber + # + # def perform(*args) + # end + # end + # + # # bad + # class MySubscriber + # include Gitlab::EventStore::Subscriber + # end + # + # # good + # class MySubscriber + # include Gitlab::EventStore::Subscriber + # + # def handle_event(event) + # end + # end + # + class EventStoreSubscriber < RuboCop::Cop::Cop + SUBSCRIBER_MODULE_NAME = 'Gitlab::EventStore::Subscriber' + FORBID_PERFORM_OVERRIDE = "Do not override `perform` in a `#{SUBSCRIBER_MODULE_NAME}`." + REQUIRE_HANDLE_EVENT = "A `#{SUBSCRIBER_MODULE_NAME}` must implement `#handle_event(event)`." + + def_node_matcher :includes_subscriber?, <<~PATTERN + (send nil? :include (const (const (const nil? :Gitlab) :EventStore) :Subscriber)) + PATTERN + + def on_send(node) + return unless includes_subscriber?(node) + + self.is_subscriber ||= true + self.include_subscriber_node ||= node + end + + def on_def(node) + if is_subscriber && node.method_name == :perform + add_offense(node, message: FORBID_PERFORM_OVERRIDE) + end + + self.implements_handle_event ||= true if node.method_name == :handle_event + end + + def on_investigation_end + super + + if is_subscriber && !implements_handle_event + add_offense(include_subscriber_node, message: REQUIRE_HANDLE_EVENT) + end + end + + private + + attr_accessor :is_subscriber, :include_subscriber_node, :implements_handle_event + end + end + end +end |