chore(*): auto-merge workflow ์ถ๊ฐ #19
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 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 }} |