Skip to content

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:

  1. Duplication: Both ProtectedBranch and SquashOption models validate the same condition, causing duplicate error messages in tests and potentially in the UI
  2. 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
  3. Nested attributes complexity: Rails' accepts_nested_attributes_for automatically 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 :base with 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 SquashOption creation (console, factories, future code paths)

Related

Edited by 🤖 GitLab Bot 🤖