-
Notifications
You must be signed in to change notification settings - Fork 36
74 lines (63 loc) · 2.41 KB
/
cleanup-branches.yaml
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
name: cleanup-merged-branches
on:
schedule:
# Every week on Monday morning at 00:00
- cron: '0 0 * * 1'
workflow_dispatch:
inputs:
dryRun:
description: "Dry Run"
required: false
default: true
type: choice
options:
- false
- true
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# With fetch-depth 0 the checkout action will also fetch all tags and branches.
fetch-depth: 0
token: ${{ secrets.GH_PAT }}
- name: Configure git name/email
run: |
git config user.name "IBM/Instana/Team Node.js"
git config user.email [email protected]
- name: Authenticate GitHub CLI
run: |
gh auth login --with-token <<< "${{ secrets.GH_PAT }}"
- name: Delete old merged branches
env:
DRY_RUN: ${{ github.event.inputs.dryRun }}
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
run: |
if [[ "$(uname)" == "Darwin" ]]; then
THRESHOLD_DATE=$(date -v-60d +%s)
else
THRESHOLD_DATE=$(date +%s -d '60 days ago')
fi
git fetch --prune --all
for branch in $(git branch -r | grep -E 'origin/(chore-|docs-|fix-|feat-|test-|refactor-)'); do
branch_name=$(echo "$branch" | sed 's|origin/||')
pr_info=$(gh pr list --state merged --base main --head "$branch_name" --json mergedAt)
pr_merged_at=$(echo "$pr_info" | jq -r '.[0].mergedAt' 2>/dev/null)
if [[ -z "$pr_merged_at" || "$pr_merged_at" == "null" ]]; then
echo "Branch $branch_name has no merged PR. Skipping."
continue
fi
pr_merged_at_cleaned=$(echo "$pr_merged_at" | sed 's/Z$//')
pr_merged_timestamp=$(date -d "$pr_merged_at_cleaned" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%S" "$pr_merged_at_cleaned" +%s)
if [[ $pr_merged_timestamp -lt $THRESHOLD_DATE ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
echo "Dry run: git push origin --delete $branch_name"
else
echo "Deleting branch: $branch_name"
git push origin --delete "$branch_name"
fi
else
echo "Skipping branch: $branch_name (merged less than 60 days ago)."
fi
done