Skip to content

Commit 588f3e5

Browse files
fix: improve mergebot /update command with better fork handling and checkout
This commit fixes several issues with the `/update` command in the mergebot workflow: **Problems fixed:** 1. Checkout was trying to fetch branch from base repo instead of head repo 2. Branch comparison used incorrect reference format for same-repo PRs 3. Fork validation happened too late, causing unnecessary operations **Changes:** - Add early exit for fork PRs in "Get PR info" step with clear error message - Specify repository parameter in checkout action to fetch from correct repo - Simplify branch comparison to use simple branch reference for same-repo PRs - Update validation conditions to skip unnecessary steps for fork PRs - Remove redundant "Handle fork PR" step (now handled earlier) - Add better logging for debugging **Benefits:** - Fork PRs now get immediate, clear feedback that update is not supported - Same-repo PRs can now be updated correctly without checkout errors - Better performance by skipping unnecessary operations for forks - Cleaner code flow and easier maintenance 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 66aacfc commit 588f3e5

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

.github/workflows/merge-bot.yml

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,40 @@ jobs:
403403
404404
// Check if it's from the same repository
405405
const isSameRepo = pr.head.repo.full_name === pr.base.repo.full_name;
406-
406+
407+
// Early exit for fork PRs
408+
if (!isSameRepo) {
409+
core.info('❌ PR is from fork - update not supported');
410+
core.setOutput('is_fork', 'true');
411+
412+
await github.rest.issues.createComment({
413+
owner: context.repo.owner,
414+
repo: context.repo.repo,
415+
issue_number: context.payload.issue.number,
416+
body: `ℹ️ Update commands are not supported for PRs from forks.
417+
418+
To update your branch, please run these commands locally:
419+
420+
\`\`\`bash
421+
git fetch upstream ${pr.base.ref}
422+
git merge upstream/${pr.base.ref}
423+
git push
424+
\`\`\`
425+
426+
Or create a new PR from the same repository.`
427+
});
428+
429+
return;
430+
}
431+
407432
core.setOutput('base_branch', pr.base.ref);
408433
core.setOutput('head_branch', pr.head.ref);
409434
core.setOutput('head_repo', pr.head.repo.full_name);
410435
core.setOutput('base_repo', pr.base.repo.full_name);
411436
core.setOutput('is_same_repo', isSameRepo.toString());
412437

413438
- name: Validate branch exists
414-
if: steps.check_perms.outputs.has_permission == 'true'
439+
if: steps.check_perms.outputs.has_permission == 'true' && steps.pr_info.outputs.is_fork != 'true'
415440
id: validate_branch
416441
uses: actions/github-script@v8
417442
with:
@@ -471,14 +496,6 @@ jobs:
471496
script: |
472497
const baseBranch = '${{ steps.pr_info.outputs.base_branch }}';
473498
const headBranch = '${{ steps.pr_info.outputs.head_branch }}';
474-
const isSameRepo = '${{ steps.pr_info.outputs.is_same_repo }}' === 'true';
475-
476-
if (!isSameRepo) {
477-
core.info('PR is from fork - update not supported');
478-
core.setOutput('update_needed', 'false');
479-
core.setOutput('is_fork', 'true');
480-
return;
481-
}
482499
483500
try {
484501
// Get latest commit from base branch
@@ -490,12 +507,12 @@ jobs:
490507
491508
const baseSha = baseBranchData.commit.sha;
492509
493-
// Check if head branch is behind base
510+
// Check if head branch is behind base (same-repo PRs only at this point)
494511
const { data: comparison } = await github.rest.repos.compareCommits({
495512
owner: context.repo.owner,
496513
repo: context.repo.repo,
497514
base: baseSha,
498-
head: `${context.repo.owner}/${context.repo.repo}:${headBranch}`
515+
head: headBranch
499516
});
500517
501518
const isBehind = comparison.behind_by > 0;
@@ -530,32 +547,11 @@ jobs:
530547
body: '✅ Branch is already up to date with the latest changes!'
531548
});
532549
533-
- name: Handle fork PR
534-
if: steps.check_perms.outputs.has_permission == 'true' && steps.check_update.outputs.is_fork == 'true'
535-
uses: actions/github-script@v8
536-
with:
537-
script: |
538-
await github.rest.issues.createComment({
539-
owner: context.repo.owner,
540-
repo: context.repo.repo,
541-
issue_number: context.payload.issue.number,
542-
body: 'ℹ️ Update commands are not supported for PRs from forks.
543-
544-
To update your branch, please run these commands locally:
545-
546-
```bash
547-
git fetch upstream main
548-
git merge upstream/main
549-
git push
550-
```
551-
552-
Or create a new PR from the same repository.'
553-
});
554-
555550
- name: Checkout PR branch
556551
if: steps.check_perms.outputs.has_permission == 'true' && steps.check_update.outputs.update_needed == 'true' && steps.validate_branch.outputs.branch_exists == 'true'
557552
uses: actions/checkout@v4
558553
with:
554+
repository: ${{ steps.pr_info.outputs.head_repo }}
559555
ref: ${{ steps.pr_info.outputs.head_branch }}
560556
fetch-depth: 0
561557
token: ${{ secrets.GITHUB_TOKEN }}
@@ -589,7 +585,9 @@ Or create a new PR from the same repository.'
589585
if: steps.update.outputs.merge_success == 'true'
590586
run: |
591587
HEAD_BRANCH="${{ steps.pr_info.outputs.head_branch }}"
592-
echo "Pushing changes to $HEAD_BRANCH..."
588+
HEAD_REPO="${{ steps.pr_info.outputs.head_repo }}"
589+
590+
echo "Pushing changes to $HEAD_BRANCH in $HEAD_REPO..."
593591
git push origin "$HEAD_BRANCH"
594592
595593
- name: Comment success

0 commit comments

Comments
 (0)