88 delete-old-branches :
99 name : Delete old branches
1010 runs-on : ubuntu-latest
11+ permissions :
12+ contents : write
13+ pull-requests : read
1114 steps :
12- - name : Checkout repository
13- uses : actions/checkout@v2
14- - name : Run delete-old-branches-action
15- uses : beatlabs/delete-old-branches-action@v0.0.10
16- with :
17- repo_token : ${{ github.token }}
18- date : ' 7 days ago'
19- dry_run : false
20- default_branches : main,master,staging
21- exclude_open_pr_branches : true
15+ - name : Delete branches with no activity in 7+ days
16+ env :
17+ GH_TOKEN : ${{ github.token }}
18+ REPO : ${{ github.repository }}
19+ PROTECTED : ' main master staging'
20+ CUTOFF_DAYS : ' 7'
21+ run : |
22+ set -euo pipefail
23+
24+ cutoff_epoch=$(date -u -d "${CUTOFF_DAYS} days ago" +%s)
25+
26+ # Branches with open PRs (head refs) — never delete these.
27+ open_pr_branches=$(gh api --paginate "repos/${REPO}/pulls?state=open&per_page=100" \
28+ --jq '.[].head.ref' | sort -u)
29+
30+ # All branches with their tip commit date, paginated.
31+ gh api --paginate "repos/${REPO}/branches?per_page=100" --jq '.[].name' | while read -r branch; do
32+ case " ${PROTECTED} " in *" ${branch} "*) continue ;; esac
33+ if grep -Fxq "${branch}" <<< "${open_pr_branches}"; then
34+ continue
35+ fi
36+
37+ committed_at=$(gh api "repos/${REPO}/branches/${branch}" \
38+ --jq '.commit.commit.committer.date')
39+ committed_epoch=$(date -u -d "${committed_at}" +%s)
40+
41+ if [ "${committed_epoch}" -lt "${cutoff_epoch}" ]; then
42+ echo "Deleting ${branch} (last commit ${committed_at})"
43+ gh api -X DELETE "repos/${REPO}/git/refs/heads/${branch}"
44+ fi
45+ done
0 commit comments