|
29 | 29 | ISSUE_BODY_TEMPLATE = "issue_body.md.j2" |
30 | 30 | REMOVAL_PR_BODY_TEMPLATE = "removal_pr_body.md.j2" |
31 | 31 |
|
| 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 | + |
32 | 53 | # Maximum issues to check when looking for duplicates (GitHub API max is 100) |
33 | 54 | MAX_ISSUES_PER_PAGE = 100 |
34 | 55 | # 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 |
180 | 201 | name = component["name"] |
181 | 202 | path = component["path"] |
182 | 203 | title = get_removal_pr_title(name) |
183 | | - branch_name = f"remove-stale-{name}" |
| 204 | + branch_name = f"remove-stale-{sanitize_branch_name(name)}" |
184 | 205 | owners = get_owners(repo_path / path) |
185 | 206 |
|
186 | 207 | if dry_run: |
|
0 commit comments