From ba3fdf0ba9a7828a3919ff11503fe26fa30180b3 Mon Sep 17 00:00:00 2001 From: Nubenetes Bot Date: Mon, 18 May 2026 10:52:32 +0200 Subject: [PATCH] feat(ops): add stale branch cleanup logic (30 days) to workflow --- .github/workflows/cleanup_merged_branches.yml | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.github/workflows/cleanup_merged_branches.yml b/.github/workflows/cleanup_merged_branches.yml index f50da03c..5fb741f7 100644 --- a/.github/workflows/cleanup_merged_branches.yml +++ b/.github/workflows/cleanup_merged_branches.yml @@ -38,3 +38,31 @@ jobs: echo "Deleting remote branch: $branch" git push origin --delete "$branch" done + + echo "---" + echo "Identifying stale unmerged branches (no activity for 30 days)..." + # Get all remote branches with their last commit date + # Format: authordate:iso8601 refname:short + current_date=$(date +%s) + stale_seconds=$((30 * 24 * 60 * 60)) # 30 days + + git for-each-ref --format='%(authordate:unix) %(refname:short)' refs/remotes/origin | while read last_commit_unix branch_full; do + branch=$(echo "$branch_full" | sed 's/origin\///') + + # Skip protected branches and HEAD + if [[ "$branch" =~ ^(master|develop|gh-pages|HEAD)$ ]]; then + continue + fi + + # Skip branches that were just identified as merged (avoid double attempt) + if [[ " $branches " =~ " $branch " ]]; then + continue + fi + + age=$((current_date - last_commit_unix)) + if [ "$age" -gt "$stale_seconds" ]; then + last_date=$(date -d "@$last_commit_unix" +"%Y-%m-%d") + echo "Deleting stale branch: $branch (Last commit: $last_date)" + git push origin --delete "$branch" || echo "Could not delete $branch (might be protected or already gone)" + fi + done