blob: 410c9dd434ea2c233c3a21b79982ba9df5ae77d1 (
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
|
# frozen_string_literal: true
require_relative 'suggestor'
module Tooling
module Danger
module ModelValidations
include ::Tooling::Danger::Suggestor
MODEL_FILES_PATH = 'app/models'
MODEL_CONCERN_FILES_PATH = 'app/models/concerns'
EE_PREFIX = 'ee/'
MODEL_FILES_REGEX = %r{\A(#{EE_PREFIX})?#{MODEL_FILES_PATH}}
MODEL_CONCERN_FILES_REGEX = %r{\A(#{EE_PREFIX})?#{MODEL_CONCERN_FILES_PATH}}
VALIDATION_METHODS = %w[validates validate validates_each validates_with validates_associated].freeze
VALIDATIONS_REGEX = /^\+\s*(.*\.)?(#{VALIDATION_METHODS.join('|')})[( ]/
CODE_QUALITY_URL = "https://docs.gitlab.com/ee/development/code_review.html#quality"
SUGGEST_MR_COMMENT = <<~SUGGEST_COMMENT.freeze
Did you consider new validations can break existing records?
Please follow the [code quality guidelines about new model validations](#{CODE_QUALITY_URL}) when adding a new
model validation.
If you're adding the validations to a model with no records you can ignore this warning.
SUGGEST_COMMENT
def add_comment_for_added_validations
changed_model_files.each do |filename|
add_suggestion(
filename: filename,
regex: VALIDATIONS_REGEX,
comment_text: SUGGEST_MR_COMMENT
)
end
end
def changed_model_files
added_files = helper.added_files
modified_files = helper.modified_files
modified_files.grep(MODEL_FILES_REGEX) + added_files.grep(MODEL_CONCERN_FILES_REGEX)
end
end
end
end
|