-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (114 loc) · 5.07 KB
/
cleanup-branch.yml
File metadata and controls
125 lines (114 loc) · 5.07 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
name: Suggest Cleaned-up Branch
on:
workflow_call:
inputs:
commit_threshold:
description: 'Number of commits after which to suggest cleanup'
required: false
default: 10
type: number
days_stale:
description: 'Number of days since last commit to consider branch stale'
required: false
default: 30
type: number
base_branch:
description: 'Base branch for cleanup (default: main)'
required: false
default: 'main'
type: string
dry_run:
description: 'If true, only report, do not open PR'
required: false
default: true
type: boolean
concurrency:
group: cleanup-branch-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true
jobs:
check-and-suggest:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Get current branch name
id: branch
run: echo "branch_name=${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
- name: Count commits on branch
id: count
run: |
COMMITS=$(git rev-list --count origin/${{ steps.branch.outputs.branch_name }})
echo "commit_count=$COMMITS" >> $GITHUB_OUTPUT
- name: Get last commit date
id: lastdate
run: |
LAST_DATE=$(git log -1 --format=%ct origin/${{ steps.branch.outputs.branch_name }})
NOW=$(date +%s)
AGE_DAYS=$(( (NOW - LAST_DATE) / 86400 ))
echo "age_days=$AGE_DAYS" >> $GITHUB_OUTPUT
- name: Decide if cleanup needed
id: need_cleanup
run: |
NEED_CLEANUP=false
REASON=""
COMMIT_COUNT="${{ steps.count.outputs.commit_count }}"
DAYS_STALE="${{ steps.lastdate.outputs.age_days }}"
echo "[cleanup-branch] Commit count: $COMMIT_COUNT (threshold: ${{ inputs.commit_threshold }})"
echo "[cleanup-branch] Days since last commit: $DAYS_STALE (threshold: ${{ inputs.days_stale }})"
if [ "$COMMIT_COUNT" -gt "${{ inputs.commit_threshold }}" ]; then
NEED_CLEANUP=true
REASON="Commit count ($COMMIT_COUNT) exceeds threshold (${{ inputs.commit_threshold }})"
fi
if [ "$DAYS_STALE" -gt "${{ inputs.days_stale }}" ]; then
NEED_CLEANUP=true
if [ -n "$REASON" ]; then
REASON="$REASON; "
fi
REASON+="Branch is stale ($DAYS_STALE days > ${{ inputs.days_stale }})"
fi
if [ "$NEED_CLEANUP" = true ]; then
echo "[cleanup-branch] Cleanup needed: true. Reason: $REASON"
else
echo "[cleanup-branch] Cleanup needed: false."
fi
echo "need_cleanup=$NEED_CLEANUP" >> $GITHUB_OUTPUT
echo "cleanup_reason=$REASON" >> $GITHUB_OUTPUT
- name: Squash and push cleaned branch
if: steps.need_cleanup.outputs.need_cleanup == 'true' && inputs.dry_run == false
run: |
set -e
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -b cleaned-${{ steps.branch.outputs.branch_name }} origin/${{ steps.branch.outputs.branch_name }}
git reset --soft origin/${{ inputs.base_branch }}
git commit -m "Squashed commit for branch cleanup" --allow-empty
git push -f origin cleaned-${{ steps.branch.outputs.branch_name }}
- name: Create PR for cleaned branch
if: steps.need_cleanup.outputs.need_cleanup == 'true' && inputs.dry_run == false
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: cleaned-${{ steps.branch.outputs.branch_name }}
base: ${{ steps.branch.outputs.branch_name }}
committer: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
title: "Suggest: Cleaned-up branch (squashed commits)"
body: |
This PR was automatically created to suggest a cleaned-up (squashed) version of `${{ steps.branch.outputs.branch_name }}`.
- Original commit count: ${{ steps.count.outputs.commit_count }}
- Days since last commit: ${{ steps.lastdate.outputs.age_days }}
- Base branch: ${{ inputs.base_branch }}
Please review and merge if you want to clean up the branch history.
- name: Report (dry run or no cleanup needed)
if: steps.need_cleanup.outputs.need_cleanup == 'false' || inputs.dry_run == true
run: |
echo "No cleanup needed or dry run enabled."
echo "Commit count: ${{ steps.count.outputs.commit_count }}"
echo "Days since last commit: ${{ steps.lastdate.outputs.age_days }}"
echo "Cleanup needed: ${{ steps.need_cleanup.outputs.need_cleanup }}"
echo "Reason: ${{ steps.need_cleanup.outputs.cleanup_reason }}"