Skip to content

Commit 09f9373

Browse files
committed
chore(ci): Sanitize generated stale component removal PRs branch names
Signed-off-by: Giulio Frasca <gfrasca@redhat.com>
1 parent f3bc1db commit 09f9373

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

.github/scripts/stale_component_handler/stale_component_handler.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@
2929
ISSUE_BODY_TEMPLATE = "issue_body.md.j2"
3030
REMOVAL_PR_BODY_TEMPLATE = "removal_pr_body.md.j2"
3131

32+
33+
def sanitize_branch_name(name: str) -> str:
34+
"""Sanitize a string to be a valid git branch name.
35+
36+
Git branch names cannot contain spaces, ~, ^, :, ?, *, [, \\, or
37+
consecutive dots. They also cannot begin/end with dots or slashes.
38+
"""
39+
import re
40+
41+
# Replace spaces and invalid characters with hyphens
42+
sanitized = re.sub(r"[\s~^:?*\[\]\\@{}'\"]+", "-", name)
43+
# Replace consecutive dots with a single dot
44+
sanitized = re.sub(r"\.{2,}", ".", sanitized)
45+
# Remove leading/trailing dots, slashes, and hyphens
46+
sanitized = sanitized.strip(".-/")
47+
# Collapse multiple consecutive hyphens into one
48+
sanitized = re.sub(r"-{2,}", "-", sanitized)
49+
# Convert to lowercase for consistency
50+
sanitized = sanitized.lower()
51+
return sanitized
52+
3253
# Maximum issues to check when looking for duplicates (GitHub API max is 100)
3354
MAX_ISSUES_PER_PAGE = 100
3455
# GitHub API limits assignees to 10 per issue
@@ -180,7 +201,7 @@ def create_removal_pr(repo: str, component: dict, repo_path: Path, dry_run: bool
180201
name = component["name"]
181202
path = component["path"]
182203
title = get_removal_pr_title(name)
183-
branch_name = f"remove-stale-{name}"
204+
branch_name = f"remove-stale-{sanitize_branch_name(name)}"
184205
owners = get_owners(repo_path / path)
185206

186207
if dry_run:

0 commit comments

Comments
 (0)