Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(*): auto-merge workflow 추가 #2

Merged
merged 10 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @Doeunnkimm @semnil5202 @LeeJeongHooo @Andrevile
* @Doeunnkimm @semnil5202 @LeeJeongHooo @Andrevile @sambad-adventure
134 changes: 134 additions & 0 deletions .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Loading