forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch-deletion-pr-processing.yml
More file actions
96 lines (88 loc) · 3.39 KB
/
Copy pathbranch-deletion-pr-processing.yml
File metadata and controls
96 lines (88 loc) · 3.39 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
name: Branch Deletion Phase Two (PR Processing)
on:
pull_request:
types:
- closed
branches:
- 'development'
paths:
- '**.branchreport'
permissions:
contents: write
jobs:
delete-branches-and-cleanup:
if: |
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.title, '[AUTO] Branch Deletion Candidates')
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.repository.default_branch }}
- name: Delete branch report file
run: |
# Check if file exists and delete it
if [ -f "branch-report.branchreport" ]; then
# Configure Git with your username
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git rm branch-report.branchreport
git commit -m "Remove temporary branch report file"
git push
echo "Removed branch-report.branchreport file"
else
echo "branch-report.branchreport file not found"
fi
- name: Extract branches and delete
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
echo "PR Body Content:"
echo "$PR_BODY"
echo "-----------------------------------"
echo "Extracting branch names for deletion..."
# Extract lines between the markers using awk
DELETION_LIST=$(echo "$PR_BODY" | awk '
BEGIN { print_lines = 0; }
/Branches for deletion/ { print_lines = 1; next; }
/Branches not eligible for deletion/ { print_lines = 0; }
print_lines == 1 && !/^```/ && NF > 0 {
print $1;
}
')
echo "Branches identified for deletion:"
echo "$DELETION_LIST"
echo "-----------------------------------"
if [ -z "$DELETION_LIST" ]; then
echo "No branches found for deletion"
exit 0
fi
# Configure Git with your username
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Process each branch
echo "$DELETION_LIST" | while read -r BRANCH; do
# Skip empty lines
[ -z "$BRANCH" ] && continue
echo "Processing branch: '$BRANCH'"
# Final protection check
branch_lower=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]')
if [[ $branch_lower =~ (backup|development|main|master|production) ]]; then
echo "Skipping protected branch: $BRANCH"
continue
fi
echo "Attempting to delete branch: $BRANCH"
# Check if branch exists before trying to delete
if git ls-remote --heads origin "$BRANCH" | grep -q "$BRANCH"; then
echo "Branch exists, proceeding with deletion"
git push origin --delete "$BRANCH" || {
echo "Failed to delete branch: $BRANCH"
echo "Error code: $?"
}
else
echo "Branch $BRANCH does not exist or is not accessible"
fi
done