Skip to content

[AUTO] Branch Deletion Candidates - 20251101 #1

[AUTO] Branch Deletion Candidates - 20251101

[AUTO] Branch Deletion Candidates - 20251101 #1

name: Branch Deletion Phase Two (PR Processing)
on:
pull_request:
types:
- closed
branches:
- 'development'
paths:
- '**.branchreport'
permissions:
contents: write
jobs:
delete-branches-and-cleanup:
if: |
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.title, '[AUTO] Branch Deletion Candidates')
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.repository.default_branch }}
- name: Delete branch report file
run: |
# Check if file exists and delete it
if [ -f "branch-report.branchreport" ]; then
# Configure Git with your username
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git rm branch-report.branchreport
git commit -m "Remove temporary branch report file"
git push
echo "Removed branch-report.branchreport file"
else
echo "branch-report.branchreport file not found"
fi
- name: Extract branches and delete
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
echo "PR Body Content:"
echo "$PR_BODY"
echo "-----------------------------------"
echo "Extracting branch names for deletion..."
# Extract lines between the markers using awk
DELETION_LIST=$(echo "$PR_BODY" | awk '
BEGIN { print_lines = 0; }
/Branches for deletion/ { print_lines = 1; next; }
/Branches not eligible for deletion/ { print_lines = 0; }
print_lines == 1 && !/^```/ && NF > 0 {
print $1;
}
')
echo "Branches identified for deletion:"
echo "$DELETION_LIST"
echo "-----------------------------------"
if [ -z "$DELETION_LIST" ]; then
echo "No branches found for deletion"
exit 0
fi
# Configure Git with your username
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Process each branch
echo "$DELETION_LIST" | while read -r BRANCH; do
# Skip empty lines
[ -z "$BRANCH" ] && continue
echo "Processing branch: '$BRANCH'"
# Final protection check
branch_lower=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]')
if [[ $branch_lower =~ (backup|development|main|master|production) ]]; then
echo "Skipping protected branch: $BRANCH"
continue
fi
echo "Attempting to delete branch: $BRANCH"
# Check if branch exists before trying to delete
if git ls-remote --heads origin "$BRANCH" | grep -q "$BRANCH"; then
echo "Branch exists, proceeding with deletion"
git push origin --delete "$BRANCH" || {
echo "Failed to delete branch: $BRANCH"
echo "Error code: $?"
}
else
echo "Branch $BRANCH does not exist or is not accessible"
fi
done