diff options
Diffstat (limited to 'rubocop/cop/migration/background_migration_base_class.rb')
-rw-r--r-- | rubocop/cop/migration/background_migration_base_class.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/rubocop/cop/migration/background_migration_base_class.rb b/rubocop/cop/migration/background_migration_base_class.rb new file mode 100644 index 00000000000..50cbe3a69c3 --- /dev/null +++ b/rubocop/cop/migration/background_migration_base_class.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Migration + class BackgroundMigrationBaseClass < RuboCop::Cop::Cop + MSG = 'Batched background migration jobs should inherit from Gitlab::BackgroundMigration::BatchedMigrationJob' + + def_node_search :top_level_module?, <<~PATTERN + (module (const nil? :Gitlab) (module (const nil? :BackgroundMigration) ...)) + PATTERN + + def_node_matcher :matching_parent_namespace?, <<~PATTERN + {nil? (const (const {cbase nil?} :Gitlab) :BackgroundMigration)} + PATTERN + + def_node_search :inherits_batched_migration_job?, <<~PATTERN + (class _ (const #matching_parent_namespace? :BatchedMigrationJob) ...) + PATTERN + + def on_module(module_node) + return unless top_level_module?(module_node) + + top_level_class_node = module_node.each_descendant(:class).first + + return if top_level_class_node.nil? || inherits_batched_migration_job?(top_level_class_node) + + add_offense(top_level_class_node, location: :expression) + end + end + end + end +end |