blob: 2c96e5f29ded5e6b7c97d8447cd97f93cb1820f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# frozen_string_literal: true
require_relative '../../migration_helpers'
require_relative '../../code_reuse_helpers'
module RuboCop
module Cop
module BackgroundMigration
# Cop that checks if a valid 'feature_category' is defined in the batched background migration job
class FeatureCategory < RuboCop::Cop::Base
include MigrationHelpers
FEATURE_CATEGORIES_FILE_PATH = "config/feature_categories.yml"
MSG = "'feature_category' should be defined to better assign the ownership for batched migration jobs. "\
"For more details refer: "\
"https://docs.gitlab.com/ee/development/feature_categorization/#batched-background-migrations"
INVALID_FEATURE_CATEGORY_MSG = "'feature_category' is invalid. "\
"List of valid ones can be found in #{FEATURE_CATEGORIES_FILE_PATH}"
RESTRICT_ON_SEND = [:feature_category].freeze
class << self
attr_accessor :available_feature_categories
end
def_node_search :feature_category?, <<~PATTERN
(:send nil? :feature_category ...)
PATTERN
def on_new_investigation
super
# Defined only once per rubocop whole run instead of each file.
fetch_available_feature_categories unless self.class.available_feature_categories.present?
end
def on_class(node)
return unless in_background_migration?(node) && node.parent_class&.short_name == :BatchedMigrationJob
add_offense(node) unless feature_category?(node)
end
def on_send(node)
return unless in_background_migration?(node)
add_offense(node, message: INVALID_FEATURE_CATEGORY_MSG) unless valid_feature_category?(node)
end
private
def valid_feature_category?(node)
feature_category = node.descendants.first.value
self.class.available_feature_categories.include?(feature_category.to_s)
end
def fetch_available_feature_categories
self.class.available_feature_categories = YAML.load_file(FEATURE_CATEGORIES_FILE_PATH).to_set
end
end
end
end
end
|