From b0b7496232f37d0a40d63ad2b8975d781a07c59c Mon Sep 17 00:00:00 2001
From: Dylan Griffith <dyl.griffith@gmail.com>
Date: Mon, 12 Feb 2024 15:52:19 +1100
Subject: [PATCH] GitLab Housekeeper handle more special characters in
 identifiers

These special characters break the branch name. We might want to use
file paths and namespaced class names in `identifiers` in future and as
such we should strip these out when generating the branch name.
---
 gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb       | 1 +
 gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
index 62d6eb00c7e6..4fdf73477215 100644
--- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
+++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
@@ -54,6 +54,7 @@ def create_commit(branch_name, change)
       def branch_name(identifiers)
         # Hyphen-case each identifier then join together with hyphens.
         branch_name = identifiers
+          .map { |i| i.gsub(/[^\w]+/, '-') }
           .map { |i| i.gsub(/[[:upper:]]/) { |w| "-#{w.downcase}" } }
           .join('-')
           .delete_prefix("-")
diff --git a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb
index 81c4342b95fe..05d34b049037 100644
--- a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb
+++ b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb
@@ -9,7 +9,7 @@
   let(:logger) { instance_double(Logger, info: nil) }
   let(:git) { described_class.new(logger: logger) }
   let(:repository_path) { Pathname(Dir.mktmpdir) }
-  let(:test_branch_name) { 'gitlab-housekeeper--test-branch' }
+  let(:test_branch_name) { 'gitlab-housekeeper--some-class--test--branch_123' }
   let(:file_in_master) { 'file_in_master.txt' }
   let(:file_in_another_branch) { 'file_in_another_branch.txt' }
 
@@ -57,7 +57,7 @@ def setup_and_checkout_another_branch
       split over multiple lines!
       COMMIT
 
-      change.identifiers = %w[GitlabHousekeeper TestBranch]
+      change.identifiers = %w[GitlabHousekeeper::SomeClass Test/Branch_123]
 
       Dir.mkdir('files')
       File.write(test_file1, "Content in file 1!")
-- 
GitLab