Skip to content

🔥 - Test Issue

🔥 - Test Issue #42

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 "LABELG=${{ github.event.label.name }}" >> $GITHUB_ENV
echo "ACTION=${{ github.event.action }}" >> $GITHUB_ENV
ISSUE_TITLE="${{ env.ISSUE_TITLE }}"
ISSUE_NUMBER="${{ env.ISSUE_NUMBER }}"
LABELG="${{ env.LABELG }}"
# 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/-$//')
echo "branch title $SANITIZED_TITLE"
echo "label $LABELG"
echo "SANITIZED_TITLE=$SANITIZED_TITLE" >> $GITHUB_ENV
- name: Get Labels and Determine Branch Type
id: determine_branch_type
run: |
ISSUE_NUMBER="${{ github.event.issue.number }}"
REPO_OWNER="${{ github.repository_owner }}"
REPO_NAME="${{ github.event.repository.name }}"
GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}"
# Fetch the labels for the issue
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$ISSUE_NUMBER/labels")
# Extract label names and store them in an array
LABELS=($(echo "$RESPONSE" | jq -r '.[].name'))
# Display the labels
echo "Labels: ${LABELS[@]}"
# Store labels as an environment variable for use in later steps
echo "labels=${LABELS[@]}" >> $GITHUB_ENV
# Determinar el tipo de rama según las etiquetas
BRANCH_TYPE="feature" # Por defecto, tipo de rama "feature"
for LABEL in "${LABELS[@]}"; do
if [[ "$LABEL" == "bug" ]]; then
BRANCH_TYPE="bugfix"
break
elif [[ "$LABEL" == "hotfix" ]]; then
BRANCH_TYPE="hotfix"
break
fi
done
echo "BRANCH_TYPE=$BRANCH_TYPE" >> $GITHUB_ENV
- name: Create and Push New Branch
if: ${{ env.ACTION == 'labeled' && env.BRANCH_TYPE != 'feature' }}
run: |
SANITIZED_TITLE="${{ env.SANITIZED_TITLE }}"
BRANCH_TYPE="${{ env.BRANCH_TYPE }}"
ISSUE_NUMBER="${{ env.ISSUE_NUMBER }}"
# Construct new branch name
NEW_BRANCH_NAME="$BRANCH_TYPE/${ISSUE_NUMBER}-${SANITIZED_TITLE}"
echo "Creating branch: $NEW_BRANCH_NAME"
# Check if branch exists
if git ls-remote --heads origin "$NEW_BRANCH_NAME"; then
echo "Branch already exists: $NEW_BRANCH_NAME"
else
git switch -c "$NEW_BRANCH_NAME"
git push origin "$NEW_BRANCH_NAME"
fi
- name: Delete Unnecessary Branches (if exists)
if: ${{ env.ACTION == 'labeled' }}
run: |
ISSUE_NUMBER="${{ env.ISSUE_NUMBER }}"
BRANCH_TYPE="${{ env.BRANCH_TYPE }}"
# Definir todas las posibilidades de ramas
BRANCH_TYPES=("feature" "bugfix" "hotfix")
# Recorrer las posibles ramas
for TYPE in "${BRANCH_TYPES[@]}"; do
BRANCH_NAME="${TYPE}/${ISSUE_NUMBER}-${SANITIZED_TITLE}"
# Si la rama no es del tipo actual (por ejemplo, si la issue está en "bugfix", no eliminamos "bugfix")
if [[ "$TYPE" != "$BRANCH_TYPE" ]]; then
# Comprobar si la rama existe en el repositorio remoto
REMOTE_BRANCH=$(git ls-remote --heads origin | grep "^refs/heads/$BRANCH_NAME" | awk '{print $2}' | sed 's|refs/heads/||')
if [ -n "$REMOTE_BRANCH" ]; then
echo "Deleting unnecessary branch: $BRANCH_NAME"
git push origin --delete "$BRANCH_NAME"
else
echo "No branch found to delete: $BRANCH_NAME"
fi
fi
done