diff --git a/.github/workflows/pr-merge-cleanup.yml b/.github/workflows/pr-merge-cleanup.yml new file mode 100644 index 0000000..9fb7e44 --- /dev/null +++ b/.github/workflows/pr-merge-cleanup.yml @@ -0,0 +1,67 @@ +name: PR Merge Cleanup + +on: + pull_request: + types: + - closed + +jobs: + cleanup-after-merge: + if: > + github.event.pull_request.merged == true && + ( + github.event.pull_request.base.ref == 'dev' || + github.event.pull_request.base.ref == 'main' + ) + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + contents: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Close linked issues via gh CLI + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_BODY: ${{ github.event.pull_request.body }} + shell: bash + run: | + echo "๐Ÿ” PR ๋‚ด์šฉ์—์„œ ์ด์Šˆ ๋‹ซ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ฐพ๋Š” ์ค‘..." + issue_count=0 + failed_count=0 + + # ์ด์Šˆ ๋ฒˆํ˜ธ ์ถ”์ถœ ๋ฐ ์ค‘๋ณต ์ œ๊ฑฐ + issue_numbers=$(printf '%s' "$PR_BODY" | tr -d '\r' | grep -Eoi '(close[sd]?|fix(e[sd])?|resolve[sd]?) #[0-9]+' | grep -oE '#[0-9]+' | tr -d '#' | sort -u) + + if [ -z "$issue_numbers" ]; then + echo "โ„น๏ธ PR ๋ณธ๋ฌธ์—์„œ ๋‹ซ์„ ์ด์Šˆ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค." + exit 0 + fi + + # ๊ฐ ์ด์Šˆ ์ฒ˜๋ฆฌ + while IFS= read -r issue_number; do + if [ -n "$issue_number" ]; then + echo "โžก๏ธ ๋‹ซ๋Š” ์ค‘: #$issue_number" + if gh issue close "$issue_number" --reason completed --repo "$GITHUB_REPOSITORY"; then + echo "โœ… ์ด์Šˆ #$issue_number ๋‹ซ๊ธฐ ์™„๋ฃŒ" + issue_count=$((issue_count + 1)) + else + echo "โš ๏ธ ์ด์Šˆ #$issue_number ๋‹ซ๊ธฐ ์‹คํŒจ (์ด์Šˆ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๊ถŒํ•œ์ด ์—†์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค)" + failed_count=$((failed_count + 1)) + fi + fi + done <<< "$issue_numbers" + + echo "๐Ÿ“Š ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ: ์„ฑ๊ณต $issue_count๊ฐœ, ์‹คํŒจ $failed_count๊ฐœ" + + - name: Delete source branch + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BRANCH_NAME: ${{ github.event.pull_request.head.ref }} + shell: bash + run: | + echo "๐Ÿ—‘๏ธ ์†Œ์Šค ๋ธŒ๋žœ์น˜ ์‚ญ์ œ ์ค‘: $BRANCH_NAME" + gh api repos/${{ github.repository }}/git/refs/heads/$BRANCH_NAME -X DELETE || echo "โš ๏ธ ๋ธŒ๋žœ์น˜ ์‚ญ์ œ ์‹คํŒจ ๋˜๋Š” ์ด๋ฏธ ์‚ญ์ œ๋จ: $BRANCH_NAME"