diff options
Diffstat (limited to 'tooling/danger/model_validations.rb')
-rw-r--r-- | tooling/danger/model_validations.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tooling/danger/model_validations.rb b/tooling/danger/model_validations.rb new file mode 100644 index 00000000000..410c9dd434e --- /dev/null +++ b/tooling/danger/model_validations.rb @@ -0,0 +1,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 |