-
Notifications
You must be signed in to change notification settings - Fork 1
56 lines (45 loc) · 1.63 KB
/
clean_branches.yml
File metadata and controls
56 lines (45 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
name: Cleanup old nightly branches
on:
schedule:
- cron: "0 3 * * *" # runs daily at 3am UTC
workflow_dispatch:
permissions:
contents: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Install GitHub CLI
run: |
sudo apt-get update
sudo apt-get install gh -y
- name: Delete nightly branches older than 7 days
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
CUTOFF_DATE=$(date -d "7 days ago" +%s)
echo "Cutoff timestamp: $CUTOFF_DATE"
# paginate through all branches
gh api repos/${{ github.repository }}/branches --paginate -q '.[].name' | while read branch; do
if [[ "$branch" == nightly/* ]]; then
echo "Checking $branch..."
# skip if PR is open for this branch
PR_COUNT=$(gh pr list --head "$branch" --state open --json number -q 'length')
if [ "$PR_COUNT" -gt 0 ]; then
echo "Skipping $branch (open PR exists)"
continue
fi
# get last commit date
COMMIT_DATE=$(gh api repos/${{ github.repository }}/commits/$branch -q '.commit.committer.date')
COMMIT_TS=$(date -d "$COMMIT_DATE" +%s)
if [ "$COMMIT_TS" -lt "$CUTOFF_DATE" ]; then
echo "Deleting $branch (last commit: $COMMIT_DATE)"
git push origin --delete "$branch"
else
echo "Keeping $branch (recent)"
fi
fi
done