🔥 - Test Issue #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Manage Branch on Label Change | |
on: | |
issues: | |
types: [labeled, unlabeled] | |
jobs: | |
create-or-delete-branch: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Get Issue Details | |
id: issue_details | |
run: | | |
echo "ISSUE_TITLE=${{ github.event.issue.title }}" >> $GITHUB_ENV | |
echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV | |
echo "LABEL=${{ github.event.label.name }}" >> $GITHUB_ENV | |
echo "ACTION=${{ github.event.action }}" >> $GITHUB_ENV | |
- name: Format Branch Name | |
id: format_branch | |
run: | | |
ISSUE_TITLE="${{ env.ISSUE_TITLE }}" | |
ISSUE_NUMBER="${{ env.ISSUE_NUMBER }}" | |
LABEL="${{ env.LABEL }}" | |
# Sanitize the title: remove non-alphanumeric characters and spaces | |
SANITIZED_TITLE=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]+/-/g' | sed 's/^-//' | sed 's/-$//') | |
# Ensure the issue number is not duplicated in the title | |
SANITIZED_TITLE=$(echo "$SANITIZED_TITLE" | sed "s/^${ISSUE_NUMBER}-//") | |
# Replace spaces with hyphens (important for Git branch names) | |
SANITIZED_TITLE=$(echo "$SANITIZED_TITLE" | sed 's/ /-/g') | |
# Determine branch types | |
if [[ "$LABEL" == "bugfix" ]]; then | |
BRANCH_TYPE="bugfix" | |
OPPOSITE_BRANCH_TYPE="feature" | |
else | |
BRANCH_TYPE="feature" | |
OPPOSITE_BRANCH_TYPE="bugfix" | |
fi | |
# Construct branch names | |
NEW_BRANCH_NAME="$BRANCH_TYPE/${ISSUE_NUMBER}-${SANITIZED_TITLE}" | |
OLD_BRANCH_PREFIX="$OPPOSITE_BRANCH_TYPE/${ISSUE_NUMBER}-" | |
echo "NEW_BRANCH_NAME=$NEW_BRANCH_NAME" >> $GITHUB_ENV | |
echo "OLD_BRANCH_PREFIX=$OLD_BRANCH_PREFIX" >> $GITHUB_ENV | |
- name: Delete Opposite Branch (if exists) | |
if: ${{ env.ACTION == 'labeled' }} | |
run: | | |
OLD_BRANCH_PREFIX="${{ env.OLD_BRANCH_PREFIX }}" | |
REMOTE_BRANCH=$(git ls-remote --heads origin | grep "^refs/heads/$OLD_BRANCH_PREFIX" | awk '{print $2}' | sed 's|refs/heads/||') | |
if [ -n "$REMOTE_BRANCH" ]; then | |
echo "Deleting branch: $REMOTE_BRANCH" | |
git push origin --delete "$REMOTE_BRANCH" | |
else | |
echo "No branch found for prefix: $OLD_BRANCH_PREFIX" | |
fi | |
- name: Check if New Branch Exists | |
id: check_branch | |
if: ${{ env.ACTION == 'labeled' }} | |
run: | | |
NEW_BRANCH_NAME="${{ env.NEW_BRANCH_NAME }}" | |
if git ls-remote --heads origin "$NEW_BRANCH_NAME" | grep -q "$NEW_BRANCH_NAME"; then | |
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV | |
else | |
echo "BRANCH_EXISTS=false" >> $GITHUB_ENV | |
fi | |
- name: Create and Push New Branch | |
if: env.BRANCH_EXISTS == 'false' && env.ACTION == 'labeled' | |
run: | | |
git switch -c "${{ env.NEW_BRANCH_NAME }}" | |
git push origin "${{ env.NEW_BRANCH_NAME }}" | |
delete-branch-on-unlabeled: | |
if: github.event.action == 'unlabeled' && (github.event.label.name == 'in progress' || github.event.label.name == 'bugfix') | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Get Issue Details | |
id: issue_details | |
run: | | |
echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV | |
echo "LABEL=${{ github.event.label.name }}" >> $GITHUB_ENV | |
- name: Delete Branch | |
run: | | |
ISSUE_NUMBER="${{ env.ISSUE_NUMBER }}" | |
LABEL="${{ env.LABEL }}" | |
BRANCH_TYPE="${LABEL}" | |
BRANCH_PREFIX="$BRANCH_TYPE/${ISSUE_NUMBER}-" | |
REMOTE_BRANCH=$(git ls-remote --heads origin | grep "^refs/heads/$BRANCH_PREFIX" | awk '{print $2}' | sed 's|refs/heads/||') | |
if [ -n "$REMOTE_BRANCH" ]; then | |
PROTECTED_BRANCHES=("master" "main" "develop") # Lista de ramas protegidas | |
for branch in "${PROTECTED_BRANCHES[@]}"; do | |
if [[ "$REMOTE_BRANCH" == "$branch" ]]; then | |
echo "Error: Attempted to delete a protected branch: $REMOTE_BRANCH" | |
exit 1 | |
fi | |
done | |
echo "Deleting branch: $REMOTE_BRANCH" | |
git push origin --delete "$REMOTE_BRANCH" | |
else | |
echo "No branch found for prefix: $BRANCH_PREFIX" | |
fi |