Skip to content

Branch cleanup automation: fix critical safety issues #1069

Description

@ashleyshaw

Describe the bug

Code review of the branch cleanup automation feature (PR #1067) identified four critical safety issues in scripts/cleanup-branches.js:

  1. daysSince(): Returns Infinity for invalid/missing dates instead of 0. Since Infinity < 30 is false, branches with missing metadata get deleted instead of preserved.
  2. isMerged(): Uses String.includes() causing false positives (e.g., origin/feat/foo matches origin/feat/foo-bar).
  3. deleteLocalBranch(): Force-deletes with -D when normal delete fails, risking loss of unmerged work.
  4. buildExcludeRegex(): No error handling for invalid regex patterns, causing script to crash.

To Reproduce

  1. Run cleanup-branches with a branch that has invalid/missing commit date
  2. Expected: branch preserved (missing data = unsafe to delete)
  3. Actual: branch deleted due to Infinity age logic

Expected behavior

All edge cases handled safely with sensible defaults:

  • Invalid dates treated as recent
  • Exact branch name matching (no substrings)
  • Safe-fail on errors (no force operations)
  • Graceful regex error handling

Additional Context

These issues were identified during Copilot code review. A fix commit (a55754e) addresses all four issues with enhanced test coverage (6 tests, all passing).

Related: #1067


Definition of Ready (DoR)

  • Bug is reproducible and clearly described
  • Steps to reproduce written
  • Environment details provided
  • Screenshots/logs included (if possible)
  • Linked to existing issues/PRs (if any)
  • Estimate added (if relevant)
  • Ready for triage

Definition of Done (DoD)

  • Bug confirmed and reproducible
  • Fix implemented and tested (all supported browsers/devices)
  • Follows WordPress coding standards
  • Documentation updated (if needed)
  • Changelog entry prepared for PR (CHANGELOG.md)
  • QA performed
  • Accessibility: no WCAG 2.2 AA regressions (semantic HTML, keyboard support, colour contrast)
  • Security: no XSS, SQL injection, or other OWASP Top 10 vulnerabilities introduced
  • Performance: no adverse impact introduced
  • Linked issue closed

Activity

  1. linear-code commented on Jul 22, 2026

    @linear-code
  2. github-actions commented on Jul 22, 2026

    @github-actions
    Contributor

    ✅ Template check passed after update. Thanks for fixing the issue body.

  3. jl3panadero-source commented on Jul 22, 2026

    @jl3panadero-source

    Solucion Leonidas\n\n```\nBranch Cleanup Automation: Fixing Critical Safety Issues

    ===========================================================

    Introduction

    Branch cleanup automation is a crucial process in software development to maintain code quality, reduce technical debt, and improve overall system safety. In this response, we will outline a step-by-step approach to automate branch cleanup and address critical safety issues.

    Prerequisites

    • Familiarity with Git and version control systems
    • Understanding of automated testing and continuous integration (CI) pipelines
    • Knowledge of scripting languages (e.g., Python, Bash)

    Step 1: Identify Critical Safety Issues

    1. Conduct a code review: Perform a thorough code review to identify critical safety issues, such as:
      • Unstable or deprecated code
      • Security vulnerabilities
      • Performance bottlenecks
    2. Analyze testing coverage: Review automated test coverage to ensure that critical components are adequately tested
    3. Identify merge conflicts: Detect potential merge conflicts that may introduce safety issues

    Step 2: Automate Branch Cleanup

    1. Create a cleanup script: Develop a script (e.g., Python, Bash) to automate branch cleanup tasks, such as:
      • Removing unused or redundant code
      • Updating dependencies
      • Resolving merge conflicts
    2. Integrate with CI/CD pipeline: Integrate the cleanup script with your CI/CD pipeline to ensure automated execution
    3. Schedule regular cleanup: Schedule regular branch cleanup tasks to maintain code quality and prevent technical debt accumulation

    Step 3: Implement Safety Fixes

    1. Develop safety-focused tests: Create automated tests to verify fixes for critical safety issues
    2. Implement code changes: Implement code changes to address identified safety issues
    3. Validate fixes: Validate fixes through automated testing and manual review

    Example Script (Python)

    import os
    import git
    
    # Define cleanup tasks
    def remove_unused_code():
        # Remove unused code
        os.system("git rm -rf unused_code")
    
    def update_dependencies():
        # Update dependencies
        os.system("pip install -r requirements.txt")
    
    def resolve_merge_conflicts():
        # Resolve merge conflicts
        os.system("git merge --no-commit --no-ff origin/main")
    
    # Create a Git repository object
    repo = git.Repo()
    
    # Define the cleanup function
    def cleanup_branch():
        remove_unused_code()
        update_dependencies()
        resolve_merge_conflicts()
        repo.index.commit("Automated branch cleanup")
    
    # Schedule regular cleanup
    schedule.every(1).day.do(cleanup_branch)

    Commit Message Guidelines

    • Use descriptive commit messages that follow the standard format:
      • fix: [ brief description ]
      • feat: [ brief description ]
    • Include relevant keywords (e.g., safety, security, performance) to facilitate search and filtering

    Best Practices

    • Regularly review and update the cleanup script to ensure it remains effective
    • Monitor automated testing and CI/CD pipeline performance to detect potential issues
    • Continuously refactor and improve code quality to prevent technical debt accumulation

    By following these steps and guidelines, you can automate branch cleanup and fix critical safety issues, ensuring a more maintainable and secure codebase.\n```

  4. added
    migrate:type:bug
    migrate:type:security
    and removed on Jul 22, 2026
  5. 1 remaining item

  6. added theissue type on Jul 22, 2026
  7. added this to the v1.0 milestone on Jul 22, 2026
  8. modified the milestones: v1.0, v1.1 on Aug 29, 2026
  9. github-actions commented on Sep 8, 2026

    @github-actions
    Contributor

    ⚠️ Bare Labels Detected

    This issue has labels that are not in the canonical taxonomy:

    • migrate:priority:normal
    • migrate:type:bug
    • migrate:type:security

    Valid Canonical Labels

    Please use labels with a prefix from the canonical taxonomy:

    • Type: type:bug, type:feature, type:task, type:documentation, etc.
    • Status: status:needs-triage, status:in-progress, status:done, etc.
    • Priority: priority:critical, priority:high, priority:normal, priority:low
    • Area: area:ci, area:docs, area:labels, area:security, etc.
    • Meta: meta:needs-changelog, meta:duplicate, meta:blocked, etc.

    For the complete list, see .github/labels.yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Fields

Priority

None yet

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions