Add or update delete-merged-branches workflow

This commit is contained in:
GitHub Actions Bot 2025-07-10 20:49:27 +03:00
parent 647c851e34
commit 3aff44a806
1 changed files with 12 additions and 19 deletions

View File

@ -22,7 +22,6 @@ jobs:
echo "🔍 Base branch: $BASE_REF"
echo "🔍 Head branch: $HEAD_REF"
# List of protected branches
PROTECTED_BRANCHES=(
master
dev_odex-event
@ -90,19 +89,19 @@ jobs:
master_odex25_donation
)
# Rule 1: Do not delete dev_* merged into preprod_*
# Rule 1
if [[ "$HEAD_REF" == dev_* && "$BASE_REF" == preprod_* ]]; then
echo "🚫 Rule: Do not delete dev_* merged into preprod_*"
exit 0
fi
# Rule 2: Do not delete preprod_* merged into master_*
# Rule 2
if [[ "$HEAD_REF" == preprod_* && "$BASE_REF" == master_* ]]; then
echo "🚫 Rule: Do not delete preprod_* merged into master_*"
exit 0
fi
# Rule 3: Skip if it's a protected branch
# Rule 3: Protected branches
for protected in "${PROTECTED_BRANCHES[@]}"; do
if [[ "$HEAD_REF" == "$protected" ]]; then
echo "🛡️ '$HEAD_REF' is a protected branch. Skipping deletion."
@ -110,31 +109,25 @@ jobs:
fi
done
echo "✅ '$HEAD_REF' is eligible for deletion. Checking protection status..."
echo "✅ '$HEAD_REF' is eligible for deletion. Checking protection..."
PROTECTION_URL="https://api.github.com/repos/$REPO/branches/$HEAD_REF/protection"
PROTECTION_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token $GH_TOKEN" "$PROTECTION_URL")
PROTECTION_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" "$PROTECTION_URL")
if [ "$PROTECTION_STATUS" -eq 200 ]; then
echo "🔓 Branch is protected — removing protection..."
curl -s -X DELETE \
-H "Authorization: token $GH_TOKEN" \
"$PROTECTION_URL"
echo "🔓 Removing protection on '$HEAD_REF'..."
curl -s -X DELETE -H "Authorization: token $GH_TOKEN" "$PROTECTION_URL"
else
echo " No protection detected on '$HEAD_REF'"
echo " No protection found for '$HEAD_REF' (HTTP $PROTECTION_STATUS)"
fi
echo "🧹 Deleting branch '$HEAD_REF'..."
DELETE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
-H "Authorization: token $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$REPO/git/refs/heads/$HEAD_REF)
echo "🧹 Attempting to delete branch '$HEAD_REF'..."
DELETE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$REPO/git/refs/heads/$HEAD_REF)
if [ "$DELETE_STATUS" -eq 204 ]; then
echo "✅ Successfully deleted branch '$HEAD_REF'"
echo "✅ Branch '$HEAD_REF' successfully deleted"
else
echo "❌ Failed to delete branch '$HEAD_REF' — HTTP $DELETE_STATUS"
exit 1
fi
fi