Skip to content

Commit

Permalink
Merge pull request #202 from boostcampwm-2024/bug-shared-#168
Browse files Browse the repository at this point in the history
자동 머지 조건 및 세부동작 수정
  • Loading branch information
ezcolin2 authored Nov 19, 2024
2 parents 7630c9e + 4867af7 commit 905ad8d
Showing 1 changed file with 99 additions and 57 deletions.
156 changes: 99 additions & 57 deletions .github/workflows/auto-assign-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ jobs:
uses: actions/github-script@v6
with:
script: |
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
assignees: [context.actor]
});
const prNumber = context.payload.pull_request.number;
const currentAssignees = context.payload.pull_request.assignees.map(a => a.login);
// PR 작성자가 이미 담당자로 지정되어 있지 않은 경우에만 할당
if (!currentAssignees.includes(context.actor)) {
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
assignees: [context.actor]
});
}
auto-reviewers:
if: github.event_name == 'pull_request'
Expand All @@ -57,95 +63,131 @@ jobs:
const prAuthor = context.payload.pull_request.user.login;
const assignees = context.payload.pull_request.assignees.map(a => a.login);
// Define reviewers for each label
// 현재 리뷰어 목록 가져오기
const currentReviewers = context.payload.pull_request.requested_reviewers
? context.payload.pull_request.requested_reviewers.map(r => r.login)
: [];
const BE_reviewers = ['summersummerwhy', 'ezcolin2', 'Tolerblanc'];
const FE_reviewers = ['yewonJin', 'djk01281'];
const doc_reviewers = ['summersummerwhy', 'ezcolin2', 'Tolerblanc', 'yewonJin', 'djk01281'];
// Function to filter out assignees and PR author
// Function to filter out assignees, PR author, and current reviewers
const filterReviewers = (reviewers) => {
return reviewers.filter(r => !assignees.includes(r) && r !== prAuthor);
return reviewers.filter(r =>
!assignees.includes(r) &&
r !== prAuthor &&
!currentReviewers.includes(r)
);
};
// Check the labels on the PR and assign appropriate reviewers
const labels = context.payload.pull_request.labels.map(label => label.name);
let reviewersToAdd = [];
if (labels.includes('🐧🚀😶‍🌫️ BE')) {
reviewersToAdd.push(...filterReviewers(BE_reviewers));
reviewersToAdd.push(...filterReviewers(BE_reviewers));
}
if (labels.includes('🐳🐣 FE')) {
reviewersToAdd.push(...filterReviewers(FE_reviewers));
reviewersToAdd.push(...filterReviewers(FE_reviewers));
}
if (labels.includes('📚 Documentation')) {
reviewersToAdd.push(...filterReviewers(doc_reviewers));
reviewersToAdd.push(...filterReviewers(doc_reviewers));
}
// Remove duplicates if any
// Remove duplicates and limit the number of reviewers
reviewersToAdd = [...new Set(reviewersToAdd)];
const maxReviewers = 4; // 최대 리뷰어 수 제한
reviewersToAdd = reviewersToAdd.slice(0, maxReviewers);
// Request reviewers
// Request reviewers only if there are new reviewers to add
if (reviewersToAdd.length > 0) {
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
reviewers: reviewersToAdd
});
try {
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
reviewers: reviewersToAdd
});
console.log(`Added reviewers: ${reviewersToAdd.join(', ')}`);
} catch (error) {
console.error('Failed to add reviewers:', error);
// 실패해도 워크플로우는 계속 진행
}
} else {
console.log('No new reviewers to add');
}
auto-merge:
if: github.event_name == 'pull_request_review' && github.event.action == 'submitted' && github.event.review.state == 'approved'
if: github.event_name == 'pull_request_review' && github.event.review.state == 'approved'
runs-on: ubuntu-latest
needs: [auto-assign, auto-reviewers]
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "16"

- name: Merge and Close PR if Approved
uses: actions/github-script@v6
with:
script: |
const prNumber = context.payload.pull_request.number;
const prNumber = context.payload.pull_request.number || context.payload.review.pull_request_number;
// Check if PR is already merged
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
if (pr.data.merged) {
return;
}
try {
// PR 정보 가져오기
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// Check if PR is approved
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
if (pr.data.merged) {
console.log('PR이 이미 머지되었습니다.');
return;
}
const approved = reviews.data.some(review => review.state === 'APPROVED');
// 머지 가능 상태 확인
if (!pr.data.mergeable) {
console.log('PR에 충돌이 있습니다. 수동 확인이 필요합니다.');
core.setFailed('PR에 충돌이 있어 자동 머지를 진행할 수 없습니다.');
return;
}
// If approved, merge the PR
if (approved) {
await github.rest.pulls.merge({
// 모든 리뷰 확인
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// Delete the branch after merge
const branchName = context.payload.pull_request.head.ref;
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branchName}`
// 각 리뷰어의 최신 리뷰 상태만 확인
const latestReviews = new Map();
reviews.data.forEach(review => {
latestReviews.set(review.user.login, review.state);
});
const hasRejection = Array.from(latestReviews.values()).includes('CHANGES_REQUESTED');
const approvalCount = Array.from(latestReviews.values()).filter(state => state === 'APPROVED').length;
if (!hasRejection && approvalCount >= 1) {
console.log('PR 머지를 시도합니다...');
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// 브랜치 삭제
const branchName = pr.data.head.ref;
if (branchName !== 'main' && branchName !== 'master') {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branchName}`
});
}
} else {
console.log('머지 조건이 충족되지 않았습니다.');
}
} catch (error) {
console.error('Error:', error);
core.setFailed(error.message);
}

0 comments on commit 905ad8d

Please sign in to comment.