1+ name : Branch Deletion Phase One (PR Creation)
2+ permissions :
3+ contents : write
4+ pull-requests : write
5+ on :
6+ schedule :
7+ - cron : ' 00 22 1 * *' # 10PM on 1st of every month
8+ workflow_dispatch :
9+ inputs :
10+ min_age_days :
11+ description : " Minimum age in days since merge"
12+ required : true
13+ default : 27
14+ type : number
15+ jobs :
16+ identify-branches :
17+ if : github.repository_owner == 'mendix'
18+ runs-on : ubuntu-latest
19+ steps :
20+ - name : Checkout code
21+ uses : actions/checkout@v4
22+ with :
23+ fetch-depth : 0
24+ token : ${{ secrets.GITHUB_TOKEN }}
25+ persist-credentials : true
26+
27+ - name : Fetch all branches
28+ run : |
29+ echo "Fetching all branches from remote..."
30+ git fetch origin '+refs/heads/*:refs/remotes/origin/*' --prune
31+ echo "Fetched branches:"
32+ git branch -r
33+
34+ - name : Process branches
35+ id : branch-data
36+ run : |
37+ set -e
38+ echo "Finding all branches for processing..."
39+ ALL_BRANCHES=$(git branch -r | grep -v "origin/HEAD" | sed 's/origin\///')
40+
41+ echo "All branches found:"
42+ printf "%s\n" "${ALL_BRANCHES[@]}"
43+
44+ MIN_AGE_DAYS=${{ github.event.inputs.min_age_days || 27 }}
45+
46+ # Arrays to hold branches
47+ PROTECTED_BRANCHES=()
48+ MERGED_BRANCHES_TO_PROCESS=()
49+ BRANCHES_TO_DELETE=()
50+ BRANCHES_TOO_RECENT=()
51+
52+ CURRENT_DATE=$(date +%Y%m%d)
53+ echo "CURRENT_DATE=$CURRENT_DATE" >> $GITHUB_ENV
54+
55+ # Check branches
56+ for BRANCH in $ALL_BRANCHES; do
57+ branch_lower=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]')
58+ # Identify protected branches
59+ if [[ $branch_lower =~ (backup|development|main|master|production) ]]; then
60+ if git branch -r --merged origin/development | grep -q "origin/$BRANCH"; then
61+ PROTECTED_BRANCHES+=("$BRANCH (protected name, merged)")
62+ else
63+ PROTECTED_BRANCHES+=("$BRANCH (protected name, not merged)")
64+ fi
65+ else
66+ # Process non-protected merged branches
67+ if git branch -r --merged origin/development | grep -q "origin/$BRANCH"; then
68+ MERGED_BRANCHES_TO_PROCESS+=("$BRANCH")
69+ else
70+ UNMERGED_BRANCHES+=("$BRANCH (not merged)")
71+ fi
72+ fi
73+ done
74+
75+ # Process potential deletion
76+ for BRANCH in "${MERGED_BRANCHES_TO_PROCESS[@]}"; do
77+ MERGE_HASH=$(git log --grep="Merge branch.*$BRANCH" origin/development -n 1 --pretty=format:"%H" ||
78+ git log --grep="Merge pull request.*$BRANCH" origin/development -n 1 --pretty=format:"%H")
79+ [ -z "$MERGE_HASH" ] && MERGE_HASH=$(git log -n 1 origin/$BRANCH --pretty=format:"%H")
80+
81+ MERGE_DATE=$(git show -s --format=%ct $MERGE_HASH)
82+ DAYS_AGO=$(( ($(date +%s) - MERGE_DATE) / 86400 ))
83+
84+ if [[ $DAYS_AGO -ge $MIN_AGE_DAYS ]]; then
85+ BRANCHES_TO_DELETE+=("$BRANCH ($DAYS_AGO days)")
86+ else
87+ BRANCHES_TOO_RECENT+=("$BRANCH ($DAYS_AGO days)")
88+ fi
89+ done
90+
91+ # Display non-deleted branches for logging
92+ ALL_NON_DELETED_BRANCHES=("${PROTECTED_BRANCHES[@]}" "${BRANCHES_TOO_RECENT[@]}" "${UNMERGED_BRANCHES[@]}")
93+ IFS=$'\n' ALL_NON_DELETED_BRANCHES=($(sort <<<"${ALL_NON_DELETED_BRANCHES[*]}"))
94+ unset IFS
95+
96+ if [ ${#BRANCHES_TO_DELETE[@]} -eq 0 ]; then
97+ echo "No branches found for deletion."
98+ echo "NO_BRANCHES=true" >> $GITHUB_ENV
99+ exit 0
100+ else
101+ echo "NO_BRANCHES=false" >> $GITHUB_ENV
102+ fi
103+
104+ # Create report
105+ echo "# Branch Cleanup Report - $(date +%Y-%m-%d)" > branch-report.branchreport
106+ echo "## Branches for deletion (merged >=${MIN_AGE_DAYS} days ago):" >> branch-report.branchreport
107+ echo '```' >> branch-report.branchreport
108+ printf "%s\n" "${BRANCHES_TO_DELETE[@]}" >> branch-report.branchreport
109+ echo '```' >> branch-report.branchreport
110+ echo "## Branches not eligible for deletion:" >> branch-report.branchreport
111+ echo '```' >> branch-report.branchreport
112+ printf "%s\n" "${ALL_NON_DELETED_BRANCHES[@]}" >> branch-report.branchreport
113+ echo '```' >> branch-report.branchreport
114+
115+ echo "BRANCHES_TO_DELETE<<EOF" >> $GITHUB_ENV
116+ printf "%s\n" "${BRANCHES_TO_DELETE[@]}" >> $GITHUB_ENV
117+ echo "EOF" >> $GITHUB_ENV
118+ echo "ALL_NON_DELETED_BRANCHES<<EOF" >> $GITHUB_ENV
119+ printf "%s\n" "${ALL_NON_DELETED_BRANCHES[@]}" >> $GITHUB_ENV
120+ echo "EOF" >> $GITHUB_ENV
121+
122+ - name : Create Deletion PR
123+ if : env.NO_BRANCHES != 'true'
124+ uses : peter-evans/create-pull-request@v6
125+ with :
126+ commit-message : " Branch cleanup proposal"
127+ title : " [AUTO] Branch Deletion Candidates - ${{ env.CURRENT_DATE }}"
128+ body : |
129+ ## Branches for deletion (merged to development)
130+ ```
131+ ${{ env.BRANCHES_TO_DELETE }}
132+ ```
133+
134+ ## Branches not eligible for deletion
135+ ```
136+ ${{ env.ALL_NON_DELETED_BRANCHES }}
137+ ```
138+ ## ⚠️ Warning
139+ Merging this PR will:
140+ 1. Delete the branches listed in the "Branches for deletion" section.
141+ 2. Remove the temporary branch-report.branchreport file.
142+ branch : branch-cleanup-${{ env.CURRENT_DATE }}
143+ assignees : MarkvanMents,OlufunkeMoronfolu
144+ reviewers : MarkvanMents,OlufunkeMoronfolu
145+ labels : Internal WIP
146+ add-paths : |
147+ branch-report.branchreport
0 commit comments