Skip to content

Auto Merge Approved PRs #243

Auto Merge Approved PRs

Auto Merge Approved PRs #243

Workflow file for this run

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,
});
return { prResult: JSON.stringify(pr) }
- name: 'Check Approvals and Labels'
id: check
uses: actions/github-script@v6
with:
prResult: ${{ steps.pr.outputs.prResult }}
script: |
const pr = JSON.parse(inputs.prResult);
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
const uniqueApprovers = new Set();
reviews.data.forEach(review => {
if (review.state === 'APPROVED') {
uniqueApprovers.add(review.user.login);
}
});
const hasLabel = pr.labels.some(label => label.name === 'ํ™•์ธ ์š”์ฒญ');
// ๋ฉ”์ธ ๋ธŒ๋žœ์น˜๋กœ ํ–ฅํ•˜๋Š” PR์ธ ๊ฒฝ์šฐ ๋ฆฌ๋ทฐ์–ด 3๋ช…, ๋‹ค๋ฅธ ๋ธŒ๋žœ์น˜์ธ ๊ฒฝ์šฐ 2๋ช… ํ•„์š”
const requiredApprovals = (pr.base.ref === 'main' || pr.base.ref === 'development') ? 3 : 2;
// Check ์กฐ๊ฑด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ์— ์„ค์ •
return { result : uniqueApprovers.size >= 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}`
});