Refactor branch rule squash option validation to support proper internationalization
Summary
Improve error message handling for branch rules with squash options to support proper internationalization. Currently, validation errors are duplicated and split across translation keys, making them difficult to translate correctly.
Problem
When validating that squash options cannot be used with wildcard branch rules:
-
Duplication: Both
ProtectedBranchandSquashOptionmodels validate the same condition, causing duplicate error messages in tests and potentially in the UI -
Translation splitting: Using
errors.add(:squash_option, message)splits the error into separate translation keys ("squash option"+"cannot be used..."), which breaks proper translation in some languages per i18n guidelines -
Nested attributes complexity: Rails'
accepts_nested_attributes_forautomatically prepends attribute names and copies errors from child to parent, making it difficult to keep complete sentences together for translators
Proposed Solution
Add errors directly to the parent object from the child validation to avoid nested attribute prefixing. As suggested in this comment:
def validate_protected_branch_not_wildcard return unless protected_branch&.wildcard?
protected_branch.errors.add(:base, \_('Squash option cannot be used with wildcard branch rules. Use an exact branch name.')) end
Note: This approach may have unintended side effects and requires thorough testing.
Acceptance Criteria
-
Error messages are added to :basewith complete sentences for proper translation -
No duplicate error messages appear in tests or UI -
Error messages comply with GitLab i18n externalization guidelines -
Existing test coverage passes without showing duplicate errors -
No regression in validation behaviour for direct SquashOptioncreation (console, factories, future code paths)
Related
- MR with translation attempt: !211128 (merged)
- Related discussion: #517472 (comment 2870302247)
Edited by 🤖 GitLab Bot 🤖