🔥 - Test Issue #44
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 | |
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/-$//') | |
echo "branch $SANITIZED_TITLE" | |
echo "label $LABEL" | |
# 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 }}" |