diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 647ab703..d4c45876 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @Doeunnkimm @semnil5202 @LeeJeongHooo @Andrevile \ No newline at end of file +* @Doeunnkimm @semnil5202 @LeeJeongHooo @Andrevile @sambad-adventure \ No newline at end of file diff --git a/.github/workflows/auto-merge.yml b/.github/workflows/auto-merge.yml new file mode 100644 index 00000000..2ce84e61 --- /dev/null +++ b/.github/workflows/auto-merge.yml @@ -0,0 +1,134 @@ +name: Auto Merge PR + +on: + pull_request_review: + types: [submitted] + + pull_request: + types: [labeled, unlabeled] + +jobs: + auto-merge: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Check Labels + id: check-labels + uses: actions/github-script@v6 + with: + script: | + const labels = context.payload.pull_request.labels.map(label => label.name); + core.setOutput("no_auto_merge", labels.includes("no auto merge")); + core.setOutput("auto_merge", labels.includes("auto approve")); + core.setOutput("main_merge", labels.includes("main merge")); + + - name: Set Environment Variables + run: | + echo "NO_AUTO_MERGE=${{ steps.check-labels.outputs.no_auto_merge }}" >> $GITHUB_ENV + echo "AUTO_MERGE=${{ steps.check-labels.outputs.auto_merge }}" >> $GITHUB_ENV + echo "MAIN_MERGE=${{ steps.check-labels.outputs.main_merge }}" >> $GITHUB_ENV + + - name: Auto Approve if Auto Merge Label Exists + if: env.AUTO_MERGE == 'true' && env.NO_AUTO_MERGE == 'false' + uses: actions/github-script@v6 + with: + script: | + await github.rest.pulls.createReview({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + event: 'APPROVE', + body: '고생했다. 👍🏽' + }); + env: + GITHUB_TOKEN: ${{ secrets.PUBLIC_ACCOUNT_TOKEN }} + + - name: Merge Pull Request on Review + if: | + github.event_name == 'pull_request_review' && + github.event.review.state == 'approved' && + env.NO_AUTO_MERGE == 'false' + uses: actions/github-script@v6 + with: + script: | + const targetBranch = process.env.MAIN_MERGE === 'true' ? 'main' : 'dev'; + await github.rest.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + merge_method: 'squash', + commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`, + commit_message: 'Auto-merging PR', + sha: context.payload.pull_request.head.sha, + base: targetBranch + }); + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Merge Pull Request on Label + if: | + github.event_name == 'pull_request' && + github.event.action == 'labeled' && + env.NO_AUTO_MERGE == 'false' + uses: actions/github-script@v6 + with: + script: | + const { data: reviews } = await github.rest.pulls.listReviews({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + }); + + const approved = reviews.some(review => review.state === 'APPROVED'); + const targetBranch = process.env.MAIN_MERGE === 'true' ? 'main' : 'dev'; + + if (approved) { + await github.rest.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + merge_method: 'squash', + commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`, + commit_message: 'Auto-merging PR', + sha: context.payload.pull_request.head.sha, + base: targetBranch + }); + } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Merge Pull Request on Unlabel + if: | + github.event_name == 'pull_request' && + github.event.action == 'unlabeled' && + github.event.label.name == 'no auto merge' + uses: actions/github-script@v6 + with: + script: | + const { data: reviews } = await github.rest.pulls.listReviews({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + }); + + const approved = reviews.some(review => review.state === 'APPROVED'); + const labels = context.payload.pull_request.labels.map(label => label.name); + const targetBranch = labels.includes("main merge") ? 'main' : 'dev'; + + if (approved) { + await github.rest.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + merge_method: 'squash', + commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`, + commit_message: 'Auto-merging PR', + sha: context.payload.pull_request.head.sha, + base: targetBranch + }); + } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}