Skip to content

🔥 - Test Issue

🔥 - Test Issue #50

Workflow file for this run

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 }}"
# Sanitizar el título: eliminar caracteres no alfanuméricos y reemplazar los espacios con guiones
SANITIZED_TITLE=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9 ]//g' | sed 's/ /-/g')
# Asegurarse de que el número de la issue no se repita en el título
SANITIZED_TITLE=$(echo "$SANITIZED_TITLE" | sed "s/^${ISSUE_NUMBER}-//")
# Reemplazar múltiples guiones consecutivos por uno solo
SANITIZED_TITLE=$(echo "$SANITIZED_TITLE" | sed 's/-\+/-/g')
# Quitar guiones al principio o al final
SANITIZED_TITLE=$(echo "$SANITIZED_TITLE" | sed 's/^-//' | sed 's/-$//')
# Determinar los tipos de rama
if [[ "$LABEL" == "bug" ]]; then
BRANCH_TYPE="bugfix"
OPPOSITE_BRANCH_TYPE="feature"
else
BRANCH_TYPE="feature"
OPPOSITE_BRANCH_TYPE="bugfix"
fi
# Construir los nombres de las ramas
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 == 'bug')
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