🔥 - Test Issue #4
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="${ISSUE_TITLE}" | |
ISSUE_NUMBER="${ISSUE_NUMBER}" | |
LABEL="${LABEL}" | |
SANITIZED_TITLE=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]+/-/g' | sed 's/^-//' | sed 's/-$//') | |
if [[ "$LABEL" == "bugfix" ]]; then | |
BRANCH_TYPE="bugfix" | |
OPPOSITE_BRANCH_TYPE="feature" | |
else | |
BRANCH_TYPE="feature" | |
OPPOSITE_BRANCH_TYPE="bugfix" | |
fi | |
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="${OLD_BRANCH_PREFIX}" | |
REMOTE_BRANCH=$(git ls-remote --heads origin | grep "$OLD_BRANCH_PREFIX" | awk '{print $2}' | sed 's|refs/heads/||') | |
echo "REMOTE_BRANCH=$REMOTE_BRANCH" >> $GITHUB_ENV | |
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: | | |
if git ls-remote --heads origin "${{ env.NEW_BRANCH_NAME }}" | grep -q "${{ env.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: ${{ steps.check_branch.outputs.BRANCH_EXISTS == 'false' }} | |
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="${{ env.LABEL }}" | |
BRANCH_PREFIX="$BRANCH_TYPE/${ISSUE_NUMBER}-" | |
echo "BRANCH_PREFIX=$BRANCH_PREFIX" >> $GITHUB_ENV | |
BRANCH_PREFIX="${{ env.BRANCH_PREFIX }}" | |
REMOTE_BRANCH=$(git ls-remote --heads origin | grep "$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: $BRANCH_PREFIX" | |
fi |