forked from gitlabhq/gitlabhq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_categories.rb
77 lines (60 loc) · 2.33 KB
/
feature_categories.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
require 'set' # rubocop:disable Lint/RedundantRequireStatement -- Ruby 3.1 and earlier needs this. Drop this line after Ruby 3.2+ is only supported.
require 'yaml'
require 'digest/sha2'
require 'did_you_mean'
module RuboCop
class FeatureCategories
MSG = 'Please use a valid feature category. %{msg_suggestion}' \
'See %{document_link}'
MSG_DID_YOU_MEAN = 'Did you mean `:%{suggestion}`? '
MSG_SYMBOL = 'Please use a symbol as value.'
CONFIG_PATH = File.expand_path("../config/feature_categories.yml", __dir__)
# List of feature categories which are not defined in config/feature_categories.yml
# https://docs.gitlab.com/ee/development/feature_categorization/#tooling-feature-category
# https://docs.gitlab.com/ee/development/feature_categorization/#shared-feature-category
CUSTOM_CATEGORIES = %w[
tooling
shared
test_platform
].to_set.freeze
def self.available
@available ||= YAML.load_file(CONFIG_PATH).to_set
end
def self.available_with_custom
@available_with_custom ||= available.union(CUSTOM_CATEGORIES)
end
# Used by RuboCop to invalidate its cache if the contents of
# config/feature_categories.yml changes.
# Define a method called `external_dependency_checksum` and call
# this method to use it.
def self.config_checksum
@config_checksum ||= Digest::SHA256.file(CONFIG_PATH).hexdigest
end
attr_reader :categories
def initialize(categories)
@categories = categories
end
def check(value_node:, document_link:)
if value_node
if !value_node.sym_type?
yield MSG_SYMBOL
elsif !categories.include?(value_node.value.to_s) # rubocop:disable Rails/NegateInclude
yield format_message(value_node.value, document_link: document_link)
end
else
yield format_message(nil, document_link: document_link)
end
end
private
def format_message(value, document_link:)
format(MSG, msg_suggestion: suggestion_message(value), document_link: document_link)
end
def suggestion_message(value)
spell = DidYouMean::SpellChecker.new(dictionary: categories)
suggestions = spell.correct(value)
return if suggestions.none?
format(MSG_DID_YOU_MEAN, suggestion: suggestions.first)
end
end
end