Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_category.rb « background_migration « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6b68e037368093b893ff35beddf8e3b3b6c27e7 (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 = File.expand_path("../../../config/feature_categories.yml", __dir__)

        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}".freeze

        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