diff --git a/.github/workflows/auto-pull-request.yml b/.github/workflows/auto-pull-request.yml new file mode 100644 index 000000000..401c94a5f --- /dev/null +++ b/.github/workflows/auto-pull-request.yml @@ -0,0 +1,100 @@ +name: Auto Create Pull Request + +on: + push: + branches: + - 'feat/#*' + - 'refactor/#*' + - 'fix/#*' + +jobs: + auto-pull-request: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Extract Issue Number + id: extract + run: | + branch_name="${GITHUB_REF#refs/heads/}" + echo "BRANCH_NAME=$branch_name" >> $GITHUB_ENV + + if [[ "$branch_name" =~ ^(feat|fix|refactor)/#([0-9]+)$ ]]; then + branch_prefix="${BASH_REMATCH[1]}" + issue_number="${BASH_REMATCH[2]}" + echo "BRANCH_PREFIX=$branch_prefix" >> $GITHUB_ENV + echo "ISSUE_NUMBER=$issue_number" >> $GITHUB_ENV + else + exit 0 + fi + + - name: Check for Already Exist + id: check_pr + run: | + existing_pr=$(gh pr list --search "${{ env.BRANCH_NAME }}" --json number -q '.[] | .number') + if [ -n "$existing_pr" ]; then + exit 0 + fi + env: + GH_TOKEN: ${{ github.token }} + + - name: Fetch Issue Details + run: | + issue_number="${{ env.ISSUE_NUMBER }}" + branch_prefix="${{ env.BRANCH_PREFIX }}" + + response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/${{ github.repository }}/issues/$issue_number") + + assignees=$(echo "$response" | jq -r '.assignees[].login' | tr '\n' ', ' | sed 's/, $//') + title=$(echo "$response" | jq -r '.title') + labels=$(echo "$response" | jq -r '.labels[].name' | tr '\n' ', ' | sed 's/, $//') + pr_title="${branch_prefix}: ${title}(#${issue_number})" + + echo "ASSIGNEES=$assignees" >> $GITHUB_ENV + echo "LABELS=$labels" >> $GITHUB_ENV + echo "TITLE=$title" >> $GITHUB_ENV + echo "PR_TITLE=$pr_title" >> $GITHUB_ENV + + - name: Generate PR Body + id: generate-body + run: | + issue_number="${{ env.ISSUE_NUMBER }}" + + pr_body="" + echo "## 📌 관련 이슈" >> body.md + echo "" >> body.md + echo "- closed : #${issue_number} " >> body.md + echo "" >> body.md + echo "## ✨ PR 세부 내용" >> body.md + echo "" >> body.md + echo "" >> body.md + + summary=$(cat body.md) + echo "PR_BODY<> $GITHUB_ENV + echo "$summary" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + + - name: Create Pull Request + run: | + pr_number=$(gh pr create --title "${{ env.PR_TITLE }}" --body "${{ env.PR_BODY }}" --base "develop" --label "${{ env.LABELS }}" | xargs gh pr view --json number -q .number) + echo "PR_NUMBER=$pr_number" >> $GITHUB_ENV + env: + GH_TOKEN: ${{ github.token }} + + - name: Add Labels & Reviewers to Pull Request + run: | + IFS=',' read -r -a LABEL_ARRAY <<< "${{ env.LABELS }}" + for LABEL in "${LABEL_ARRAY[@]}"; do + if [[ "$LABEL" == "BE" ]]; then + gh pr edit "${{ env.PR_NUMBER }}" --add-reviewer "ashsty,jcoding-play,hjk0761,youngsu5582" + elif [[ "$LABEL" == "FE" ]]; then + gh pr edit "${{ env.PR_NUMBER }}" --add-reviewer "00kang,pp449,chlwlstlf" + fi + gh pr edit "${{ env.PR_NUMBER }}" --add-label "$LABEL" --remove-reviewer "${{ env.ASSIGNEES }}" + done + env: + GH_TOKEN: ${{ github.token }}