-
Notifications
You must be signed in to change notification settings - Fork 2
204 lines (180 loc) · 8.18 KB
/
Copy pathai-iterate.yml
File metadata and controls
204 lines (180 loc) · 8.18 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: AI Iteration
on:
schedule:
- cron: '0 16 * * *'
workflow_dispatch:
inputs:
issue_number:
description: 'Issue number to iterate'
required: false
pr_number:
description: 'PR number to iterate'
required: false
pull_request:
types: [opened, synchronize]
issue_comment:
types: [created]
permissions:
contents: write
pull-requests: write
issues: read
jobs:
ai-iterate:
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && startsWith(github.head_ref, 'ai/issue-')) || (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@ai'))
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install OpenCode and oh-my-opencode
run: |
bun add -g opencode-ai
bunx oh-my-opencode install --no-tui --claude=no --gemini=no --copilot=no
- name: Copy oh-my-opencode config
env:
MINIMAX_API_KEY: ${{ secrets.MINIMAX_API_KEY }}
run: |
mkdir %USERPROFILE%\.config\opencode
# 复制并替换 opencode.json
powershell -Command "$content = Get-Content '.github/workflows/opencode.json' -Raw; $content = $content -replace 'MINIMAX_API_KEY', $env:MINIMAX_API_KEY; Set-Content -Path $env:USERPROFILE\\.config\\opencode\\opencode.json -Value $content"
# 复制并替换 oh-my-opencode.json
powershell -Command "$content = Get-Content '.github/workflows/oh-my-opencode.json' -Raw; $content = $content -replace 'MINIMAX_API_KEY', $env:MINIMAX_API_KEY; Set-Content -Path $env:USERPROFILE\\.config\\opencode\\oh-my-opencode.json -Value $content"
- name: Determine trigger type and get PR info
id: determine
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const isSchedule = context.eventName === 'schedule';
const isDispatch = context.eventName === 'workflow_dispatch';
const isPR = context.eventName === 'pull_request';
const isComment = context.eventName === 'issue_comment';
let issueNumber = null;
let prNumber = null;
let branchName = null;
if (isDispatch && context.payload.inputs && context.payload.inputs.issue_number) {
issueNumber = parseInt(context.payload.inputs.issue_number);
prNumber = issueNumber;
branchName = 'ai/issue-' + issueNumber;
} else if (isPR) {
const match = context.payload.pull_request.head.ref.match(/ai\/issue-(\d+)/);
if (match) {
issueNumber = parseInt(match[1]);
prNumber = context.payload.pull_request.number;
branchName = context.payload.pull_request.head.ref;
}
} else if (isComment) {
const match = context.payload.issue.body.match(/#(\d+)/);
if (context.payload.issue.pull_request) {
prNumber = context.payload.issue.number;
const prMatch = context.payload.issue.head.ref.match(/ai\/issue-(\d+)/);
if (prMatch) {
issueNumber = parseInt(prMatch[1]);
branchName = context.payload.issue.head.ref;
}
}
} else if (isSchedule) {
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: 'ai/issue-'
});
if (prs.length > 0) {
const pr = prs[0];
const match = pr.head.ref.match(/ai\/issue-(\d+)/);
issueNumber = parseInt(match[1]);
prNumber = pr.number;
branchName = pr.head.ref;
}
}
return JSON.stringify({ issueNumber, prNumber, branchName, isSchedule });
- name: Set issue/PR number from dispatch/schedule
id: set_issue_pr
if: steps.determine.outputs.result != '{}'
run: |
node -e "const d=JSON.parse(process.env.RESULT); console.log('issueNumber='+(d.issueNumber||'')); console.log('prNumber='+(d.prNumber||'')); console.log('branchName='+(d.branchName||''))" >> %GITHUB_OUTPUT%
env:
RESULT: ${{ steps.determine.outputs.result }}
- name: Skip if no PRs found
if: steps.determine.outputs.result == '{}' || steps.set_issue_pr.outputs.branchName == ''
run: |
echo "SKIP=true" >> %GITHUB_OUTPUT%
echo "No PRs found to iterate"
- name: Checkout branch for schedule trigger
if: steps.set_issue_pr.outputs.branchName != ''
env:
BRANCH: ${{ steps.set_issue_pr.outputs.branchName }}
run: |
git fetch origin %BRANCH%
git checkout %BRANCH%
- name: Set environment variables
if: steps.set_issue_pr.outputs.branchName != ''
run: |
echo "ISSUE_NUM=${{ steps.set_issue_pr.outputs.issueNumber }}" >> %GITHUB_ENV%
echo "PR_NUMBER=${{ steps.set_issue_pr.outputs.prNumber }}" >> %GITHUB_ENV%
- name: Get PR comments
if: steps.set_issue_pr.outputs.branchName != ''
id: comments
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const prNum = '${{ env.PR_NUMBER }}';
const { data: comments } = await github.rest.issues.listComments({
issue_number: parseInt(prNum),
owner: context.repo.owner,
repo: context.repo.repo
});
const newComments = comments.filter(c =>
c.user.type !== 'Bot' &&
new Date(c.created_at) > new Date('${{ github.event.pull_request.updated_at || github.event.comment.created_at }}')
);
return JSON.stringify(newComments.map(c => c.body));
- name: Run OpenCode for changes
if: steps.comments.outputs.result != '[]'
id: opencode
env:
MINIMAX_API_KEY: ${{ secrets.MINIMAX_API_KEY }}
COMMENTS_DATA: ${{ steps.comments.outputs.result }}
run: |
# 创建临时 prompt 文件在 TEMP 目录
node -e "const fs=require('fs');const c=JSON.parse(process.env.COMMENTS_DATA);let p=fs.readFileSync('.github/workflows/prompts/ai-iterate-prompt.txt','utf8');p=p.replace(/{{issue_number}}/g,'$env:ISSUE_NUM').replace(/{{pr_number}}/g,'$env:PR_NUMBER').replace(/{{comments}}/g,c.join('\\n\\n'));fs.writeFileSync(process.env.TEMP + '\\\\prompt.txt',p);"
# 运行 opencode
opencode run "$(type %TEMP%\prompt.txt)"
- name: Check changes
id: changes
if: steps.opencode.outputs.result != ''
run: |
for /f %%a in ('git status --porcelain ^| find /c /v ""') do echo changed=%%a >> %GITHUB_OUTPUT%
- name: Commit and push
if: steps.changes.outputs.changed > 0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "AI Agent"
git config user.email "ai@github.local"
git add -A
git commit -m "AI: Iterate based on feedback" 2>nul
git push origin "ai/issue-$env:ISSUE_NUM" 2>nul
- name: Post iteration report
if: steps.changes.outputs.changed > 0
uses: actions/github-script@v7
with:
script: |
const body = "## AI Iteration Report\n\nChanges made:\n- Applied requested changes from PR comments\n- Fixed identified issues\n\nFiles modified: See commit history\nTest results: See workflow logs";
await github.rest.issues.createComment({
issue_number: parseInt('${{ env.PR_NUMBER }}'),
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
- name: Cleanup temp files
if: always()
run: |
del /q "%TEMP%\prompt.txt" 2>nul || exit 0