-
Notifications
You must be signed in to change notification settings - Fork 2
303 lines (264 loc) · 10.8 KB
/
Copy pathai-implement.yml
File metadata and controls
303 lines (264 loc) · 10.8 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
name: AI Implementation
on:
schedule:
- cron: '0 16 * * *'
workflow_dispatch:
push:
branches:
- 'ai/**'
concurrency:
group: ai-impl-${{ github.ref_name }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: write
issues: read
id-token: write
jobs:
ai-implement:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
fetch-depth: 1
- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Get opencode version
id: version
run: |
VERSION=$(curl -sf https://api.github.com/repos/anomalyco/opencode/releases/latest | grep -o '"tag_name": *"[^"]*"' | cut -d'"' -f4)
echo "version=${VERSION:-latest}" >> $GITHUB_OUTPUT
- name: Cache opencode
id: cache
uses: actions/cache@v4
with:
path: ~/.opencode/bin
key: opencode-${{ runner.os }}-${{ runner.arch }}-${{ steps.version.outputs.version }}
- name: Install opencode
if: steps.cache.outputs.cache-hit != 'true'
run: curl -fsSL https://opencode.ai/install | bash
- name: Add opencode to PATH
run: echo "$HOME/.opencode/bin" >> $GITHUB_PATH
- name: Install oh-my-opencode
run: |
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 -p ~/.config/opencode
sed "s/MINIMAX_API_KEY/${MINIMAX_API_KEY}/g" .github/workflows/opencode.json > ~/.config/opencode/opencode.json
sed "s/MINIMAX_API_KEY/${MINIMAX_API_KEY}/g" .github/workflows/oh-my-opencode.json > ~/.config/opencode/oh-my-opencode.json
- name: Determine trigger type and get issue 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 isPush = context.eventName === 'push';
let issueNumber = null;
let branchName = null;
if (isPush && '${{ github.ref_name }}'.startsWith('ai/issue-')) {
const match = '${{ github.ref_name }}'.match(/ai\/issue-(\d+)/);
if (match) {
issueNumber = parseInt(match[1]);
branchName = '${{ github.ref_name }}';
}
} else if (isDispatch) {
const inputs = context.payload.inputs;
if (inputs && inputs.issue_number) {
issueNumber = parseInt(inputs.issue_number);
branchName = 'ai/issue-' + issueNumber;
}
} else if (isSchedule) {
const { data: issues } = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'ai-task',
per_page: 10
});
if (issues.length === 0) {
console.log('No issues with ai-task label found.');
return JSON.stringify({ issueNumber: null, branchName: null });
}
const { data: comments } = await github.rest.issues.listComments({
issue_number: issues[0].number,
owner: context.repo.owner,
repo: context.repo.repo
});
const hasPlan = comments.some(c => c.body.includes('## AI Implementation Plan'));
if (hasPlan) {
issueNumber = issues[0].number;
branchName = 'ai/issue-' + issueNumber;
}
}
return JSON.stringify({ issueNumber, branchName });
- name: Set issue number
id: extract
run: |
node -e "const d=JSON.parse(process.env.RESULT); console.log('issue_number='+(d.issueNumber||'')); console.log('branch_name='+(d.branchName||''))" >> $GITHUB_OUTPUT
env:
RESULT: ${{ steps.determine.outputs.result }}
- name: Skip if no issues to process
if: steps.extract.outputs.issue_number == ''
run: |
echo "SKIP=true" >> $GITHUB_OUTPUT
echo "No issues with plan found"
- name: Checkout branch
if: steps.extract.outputs.branch_name != ''
env:
BRANCH: ${{ steps.extract.outputs.branch_name }}
run: |
git fetch origin $BRANCH || git checkout -b $BRANCH || true
git checkout $BRANCH || true
- name: Set environment variables
if: steps.extract.outputs.branch_name != ''
run: |
echo "ISSUE_NUM=${{ steps.extract.outputs.issue_number }}" >> $GITHUB_ENV
echo "PR_NUMBER=${{ steps.extract.outputs.issue_number }}" >> $GITHUB_ENV
- name: Get issue and plan
id: issue
if: steps.extract.outputs.branch_name != ''
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const num = '${{ steps.extract.outputs.issue_number }}';
const { data: issue } = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parseInt(num)
});
const { data: comments } = await github.rest.issues.listComments({
issue_number: parseInt(num),
owner: context.repo.owner,
repo: context.repo.repo
});
const plan = comments.find(c => c.body.includes('## AI Implementation Plan'));
return JSON.stringify({ number: issue.number, title: issue.title, plan: plan ? plan.body : '' });
- name: Prepare prompt
if: steps.extract.outputs.branch_name != ''
id: prepare_prompt
env:
ISSUE_DATA: ${{ steps.issue.outputs.result }}
run: |
node -e "
const fs = require('fs');
const data = JSON.parse(process.env.ISSUE_DATA || '{}');
let p = fs.readFileSync('.github/workflows/prompts/ai-implement-prompt.txt','utf8');
p = p.replace(/{{issue_number}}/g, data.number || '')
.replace(/{{issue_title}}/g, data.title || '')
.replace(/{{plan}}/g, data.plan || 'No plan found');
console.log('prompt_content=' + p);
" >> $GITHUB_OUTPUT
- name: Run OpenCode
id: opencode
if: steps.extract.outputs.branch_name != ''
run: opencode github run
env:
MINIMAX_API_KEY: ${{ secrets.MINIMAX_API_KEY }}
MODEL: minimax-cn-coding-plan/MiniMax-M2.5
AGENT: sisyphus
PROMPT: ${{ steps.prepare_prompt.outputs.prompt_content }}
- name: Check changes
id: changes
run: |
echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT
- name: Commit changes
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: Implement issue-${{ env.ISSUE_NUM }}" || true
git push origin "ai/issue-${{ env.ISSUE_NUM }}" || true
- name: Build
id: build
run: |
dotnet restore TelegramSearchBot.sln
dotnet build TelegramSearchBot.sln --configuration Release --no-restore
continue-on-error: true
- name: Prepare build fix prompt
if: steps.build.outcome == 'failure'
id: prepare_build_fix
run: |
node -e "
const fs = require('fs');
let p = fs.readFileSync('.github/workflows/prompts/ai-fix-build-prompt.txt','utf8');
p = p.replace(/{{issue_number}}/g, '${{ env.ISSUE_NUM }}');
console.log('prompt_content=' + p);
" >> $GITHUB_OUTPUT
- name: Fix build
if: steps.build.outcome == 'failure'
run: opencode github run
env:
MINIMAX_API_KEY: ${{ secrets.MINIMAX_API_KEY }}
MODEL: minimax-cn-coding-plan/MiniMax-M2.5
AGENT: sisyphus
PROMPT: ${{ steps.prepare_build_fix.outputs.prompt_content }}
- name: Rebuild after fix
if: steps.build.outcome == 'failure'
run: dotnet build TelegramSearchBot.sln --configuration Release
- name: Commit build fixes
if: steps.build.outcome == 'failure'
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: Fix build issue-${{ env.ISSUE_NUM }}" || true
git push origin "ai/issue-${{ env.ISSUE_NUM }}" || true
- name: Run tests
id: tests
run: dotnet test TelegramSearchBot.sln --configuration Release --no-build
continue-on-error: true
- name: Prepare test fix prompt
if: steps.tests.outcome == 'failure'
id: prepare_test_fix
run: |
node -e "
const fs = require('fs');
let p = fs.readFileSync('.github/workflows/prompts/ai-fix-tests-prompt.txt','utf8');
p = p.replace(/{{issue_number}}/g, '${{ env.ISSUE_NUM }}');
console.log('prompt_content=' + p);
" >> $GITHUB_OUTPUT
- name: Fix tests
if: steps.tests.outcome == 'failure'
run: opencode github run
env:
MINIMAX_API_KEY: ${{ secrets.MINIMAX_API_KEY }}
MODEL: minimax-cn-coding-plan/MiniMax-M2.5
AGENT: sisyphus
PROMPT: ${{ steps.prepare_test_fix.outputs.prompt_content }}
- name: Rebuild after test fix
if: steps.tests.outcome == 'failure'
run: |
dotnet build TelegramSearchBot.sln --configuration Release
dotnet test TelegramSearchBot.sln --configuration Release --no-build || true
- name: Commit fixes
if: steps.tests.outcome == 'failure'
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: Fix tests issue-${{ env.ISSUE_NUM }}" || true
git push origin "ai/issue-${{ env.ISSUE_NUM }}" || true
- name: Create PR
if: always()
id: create_pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create --base master --head "ai/issue-${{ env.ISSUE_NUM }}" --title "AI: Issue #${{ env.ISSUE_NUM }}" --body "AI implementation" || echo "PR exists"