forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch-deletion-pr-creation.yml
More file actions
142 lines (121 loc) · 5.68 KB
/
Copy pathbranch-deletion-pr-creation.yml
File metadata and controls
142 lines (121 loc) · 5.68 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Branch Deletion Phase One (PR Creation)
permissions:
contents: write
pull-requests: write
on:
schedule:
- cron: '00 22 1 * *' # 10PM on 1st of every month
workflow_dispatch:
inputs:
min_age_days:
description: "Minimum age in days since merge"
required: true
default: 27
type: number
jobs:
identify-branches:
if: github.repository_owner == 'mendix'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
persist-credentials: true
- name: Setup jq
run: sudo apt-get install -y jq
- name: Create timestamp file
run: |
echo "Last scan for stale merged branches: $(TZ='Europe/Amsterdam' date +'%Y-%m-%d %H:%M:%S %Z (UTC%:z)')" > branch-cleanup-timestamp.txt
- name: Fetch all branches
run: |
git fetch origin '+refs/heads/*:refs/remotes/origin/*' --prune
- name: Process branches
id: branch-data
env:
MIN_AGE_DAYS: ${{ github.event.inputs.min_age_days || '27' }}
run: |
set -e
ALL_BRANCHES=$(git branch -r | grep -v "origin/HEAD" | sed 's/origin\///')
MIN_AGE_DAYS=${MIN_AGE_DAYS:-27}
echo "MIN_AGE_DAYS=${MIN_AGE_DAYS}"
PROTECTED_COUNT=0
MERGED_COUNT=0
UNMERGED_COUNT=0
PROTECTED_BRANCHES=()
MERGED_BRANCHES_TO_PROCESS=()
BRANCHES_TO_DELETE=()
BRANCHES_TOO_RECENT=()
UNMERGED_BRANCHES=()
CURRENT_DATE=$(date +%Y%m%d)
echo "CURRENT_DATE=$CURRENT_DATE" >> $GITHUB_ENV
for BRANCH in $ALL_BRANCHES; do
branch_lower=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]')
if [[ $branch_lower == *backup* || $branch_lower =~ ^(development|main|master|production)(-[0-9]+)?$ ]]; then
PROTECTED_BRANCHES+=("$BRANCH")
PROTECTED_COUNT=$((PROTECTED_COUNT+1))
elif git branch -r --merged origin/development | grep -q "origin/$BRANCH"; then
MERGED_BRANCHES_TO_PROCESS+=("$BRANCH")
MERGED_COUNT=$((MERGED_COUNT+1))
else
UNMERGED_BRANCHES+=("$BRANCH")
UNMERGED_COUNT=$((UNMERGED_COUNT+1))
fi
done
echo "branch totals: protected=${PROTECTED_COUNT} merged=${MERGED_COUNT} unmerged=${UNMERGED_COUNT}"
for BRANCH in "${MERGED_BRANCHES_TO_PROCESS[@]}"; do
MERGE_HASH=$(git log --grep="Merge branch.*$BRANCH" origin/development -n 1 --pretty=format:"%H" || true)
if [ -z "$MERGE_HASH" ]; then
MERGE_HASH=$(git log -n 1 origin/$BRANCH --pretty=format:"%H" || true)
fi
if [ -z "$MERGE_HASH" ]; then
echo "WARN: no merge commit found for $BRANCH — skipping"
UNMERGED_BRANCHES+=("$BRANCH (no-merge-hash)")
UNMERGED_COUNT=$((UNMERGED_COUNT+1))
continue
fi
MERGE_DATE_EPOCH=$(git show -s --format=%ct $MERGE_HASH 2>/dev/null || true)
if [ -z "$MERGE_DATE_EPOCH" ]; then
echo "WARN: could not read commit date for $BRANCH (hash=$MERGE_HASH) — skipping"
UNMERGED_BRANCHES+=("$BRANCH (no-merge-date)")
UNMERGED_COUNT=$((UNMERGED_COUNT+1))
continue
fi
DAYS_AGO=$(( ($(date +%s) - MERGE_DATE_EPOCH) / 86400 ))
if [[ $DAYS_AGO -ge $MIN_AGE_DAYS ]]; then
BRANCHES_TO_DELETE+=("$BRANCH (${DAYS_AGO}d since merge)")
else
BRANCHES_TOO_RECENT+=("$BRANCH (${DAYS_AGO}d since merge)")
fi
echo "checked $BRANCH: hash=$MERGE_HASH date=$MERGE_DATE_EPOCH days=${DAYS_AGO}"
done
echo "HAS_BRANCHES=$([ ${#BRANCHES_TO_DELETE[@]} -gt 0 ] && echo true || echo false)" >> $GITHUB_ENV
echo "BRANCHES_TO_DELETE=$(printf -- '- %s\n' "${BRANCHES_TO_DELETE[@]}" | jq -Rs .)" >> $GITHUB_ENV
echo "PROTECTED_BRANCHES=$(printf -- '- %s\n' "${PROTECTED_BRANCHES[@]}" | jq -Rs .)" >> $GITHUB_ENV
echo "BRANCHES_TOO_RECENT=$(printf -- '- %s\n' "${BRANCHES_TOO_RECENT[@]}" | jq -Rs .)" >> $GITHUB_ENV
echo "UNMERGED_BRANCHES=$(printf -- '- %s\n' "${UNMERGED_BRANCHES[@]}" | jq -Rs .)" >> $GITHUB_ENV
- name: Create Deletion PR
if: env.HAS_BRANCHES == 'true'
uses: peter-evans/create-pull-request@v8 # NOTE: If you upgrade the version of this action, you must also update the GitHub Actions allowlist in mendix/docs > Settings > Actions > General to permit the new version tag, otherwise the workflow will fail.
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "[Auto] Branch Deletion Candidates - ${{ env.CURRENT_DATE }}"
body: |
### Branches for Deletion
${{ fromJSON(env.BRANCHES_TO_DELETE) }}
### Protected Branches
${{ fromJSON(env.PROTECTED_BRANCHES) }}
### Recent Merges (too recent to delete)
${{ fromJSON(env.BRANCHES_TOO_RECENT) }}
### Unmerged Branches
${{ fromJSON(env.UNMERGED_BRANCHES) }}
base: development
labels: Internal WIP
assignees: MarkvanMents,OlufunkeMoronfolu
reviewers: MarkvanMents,OlufunkeMoronfolu
commit-message: "Add branch cleanup candidates for ${{ env.CURRENT_DATE }}"
add-paths: |
branch-cleanup-timestamp.txt
author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
committer: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"