-
Notifications
You must be signed in to change notification settings - Fork 3.8k
122 lines (102 loc) · 5.02 KB
/
claude-copy-to-main.yml
File metadata and controls
122 lines (102 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
name: Claude Copy PR to Main
on:
issue_comment:
types: [created]
jobs:
copy-to-main:
name: Copy PR to Main
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/claude copy')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
issues: write
id-token: write
env:
GH_TOKEN: ${{ secrets.PAT }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.issue.number }}
steps:
- name: Check commenter has write access
env:
COMMENTER: ${{ github.event.comment.user.login }}
run: |
PERMISSION=$(gh api repos/$REPO/collaborators/$COMMENTER/permission --jq .permission)
if [[ "$PERMISSION" != "admin" && "$PERMISSION" != "write" ]]; then
gh pr comment $PR_NUMBER --repo $REPO --body "❌ You do not have write access to use \`/claude copy\`."
exit 1
fi
- name: Check PR is merged and targets non-main
run: |
PR_JSON=$(gh pr view $PR_NUMBER --repo $REPO --json baseRefName,mergedAt)
PR_BASE=$(echo "$PR_JSON" | jq -r .baseRefName)
PR_MERGED=$(echo "$PR_JSON" | jq -r .mergedAt)
if [ "$PR_BASE" = "main" ]; then
gh pr comment $PR_NUMBER --repo $REPO --body "❌ This PR already targets \`main\`. \`/claude copy\` only works on PRs targeting non-main branches."
exit 1
fi
if [ "$PR_MERGED" = "null" ] || [ -z "$PR_MERGED" ]; then
gh pr comment $PR_NUMBER --repo $REPO --body "❌ This PR has not been merged yet. \`/claude copy\` only works on merged PRs."
exit 1
fi
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.PAT }}
- name: Fetch PR head ref from fork
run: |
git fetch origin pull/$PR_NUMBER/head:pr-$PR_NUMBER-head
- name: Run Claude Copy to Main
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
trigger_phrase: "/claude copy"
github_token: ${{ secrets.PAT }}
prompt: |
REPO: ${{ env.REPO }}
PR NUMBER: ${{ env.PR_NUMBER }}
You are a PR copy assistant. Your job is to apply the final changes from a merged PR onto a new branch based on `main` and create a new PR targeting `main`.
The PR's commits originated from a fork and have been fetched locally as the branch: pr-${PR_NUMBER}-head
STEPS:
1. Get the PR details (title, body, and base branch):
gh pr view $PR_NUMBER --repo $REPO --json title,body,baseRefName
2. Configure git for committing (use the svcnvidia-nemo-ci service account since secrets.PAT belongs to it):
git config user.name "svcnvidia-nemo-ci"
git config user.email "svcnvidia-nemo-ci@nvidia.com"
3. Create a new branch from `main`:
git checkout main
git pull origin main
git checkout -b copy-pr-${PR_NUMBER}-to-main
4. Generate a patch of the PR's final changes and apply it:
MERGE_BASE=$(git merge-base origin/<baseRefName> pr-${PR_NUMBER}-head)
git diff $MERGE_BASE pr-${PR_NUMBER}-head | git apply --3way
(Replace <baseRefName> with the actual base branch name from step 1.)
If the apply fails due to merge conflicts:
a. Identify conflicted files: git diff --name-only --diff-filter=U
b. For each conflicted file, read its contents to see the conflict markers
c. Resolve the conflicts by favoring the `main` branch side when there is a genuine
conflict between the two sides. The goal is to bring the PR's changes into main
without overriding what is already on main.
d. Stage the resolved files: git add <file>
5. Commit the changes:
git add -A
git commit -m "Copy PR #${PR_NUMBER} to main"
6. Push the new branch:
git push origin copy-pr-${PR_NUMBER}-to-main
7. Create a new PR targeting `main`:
gh pr create --repo $REPO \
--base main \
--head copy-pr-${PR_NUMBER}-to-main \
--title "[Copy to main] <original PR title>" \
--body "🤖 **This PR was auto-generated by Claude** via the \`/claude copy\` command.\n\nCherry-picked from #${PR_NUMBER}.\n\n---\n\n<original PR body>"
8. Comment on the original PR with a link to the newly created PR.
IMPORTANT:
- When resolving merge conflicts, favor `main` over the non-main branch. Do not override changes already on main.
- Do NOT force push.
claude_args: |
--allowedTools "Bash(git:*),Bash(gh:*),Read,Edit"
--model "claude-opus-4-6"