Auto Merge Approved PRs #77
Workflow file for this run
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: 'Auto Merge Approved PRs' | |
on: | |
pull_request_review: | |
types: [ submitted ] | |
jobs: | |
auto_merge: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Get Pull Request' | |
id: pr | |
uses: actions/github-script@v6 | |
with: | |
result-encoding: string | |
script: | | |
const { data: pr } = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.payload.pull_request.number, | |
}); | |
core.setOutput('prResult', JSON.stringify(pr)); | |
- name: 'Check Approvals and Labels' | |
id: check | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const pr = JSON.parse(process.env.PR_RESULT); | |
const reviews = await github.rest.pulls.listReviews({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pr.number, | |
}); | |
const approvals = reviews.data.filter(review => review.state === 'APPROVED'); | |
const hasLabel = pr.labels.some(label => label.name === '확인 요청'); | |
// 메인 브랜치로 향하는 PR인 경우 리뷰어 3명, 다른 브랜치인 경우 2명 필요 | |
const requiredApprovals = pr.base.ref === 'main' ? 3 : 2; | |
// Check 조건 결과를 출력에 설정 | |
core.setOutput('result', approvals.length >= requiredApprovals && hasLabel); | |
- name: 'Change Label to 작업 완료' | |
if: steps.check.outputs.result == 'true' | |
uses: actions-ecosystem/action-add-labels@v1 | |
with: | |
labels: '작업 완료' | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
- name: 'Remove Label 확인 요청' | |
if: steps.check.outputs.result == 'true' | |
uses: actions-ecosystem/action-remove-labels@v1 | |
with: | |
labels: '확인 요청' | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
- name: 'Merge PR' | |
if: steps.check.outputs.result == 'true' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
await github.rest.pulls.merge({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.payload.pull_request.number, | |
merge_method: 'merge', // 'squash' 또는 'rebase'로도 설정 가능 | |
commit_title: `자동 머지: PR #${context.payload.pull_request.number}` | |
}); |