Skip to content

Commit 9664fbb

Browse files
committed
feat: 添加 Deep Code Bot workflow,支持 /deepcode 指令自动答疑和提 PR
1 parent 6f0f4e5 commit 9664fbb

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
name: Deep Code Bot
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
contents: write
9+
pull-requests: write
10+
issues: write
11+
12+
jobs:
13+
deepcode:
14+
# 只有仓库 Owner 自己才能触发
15+
if: |
16+
contains(github.event.comment.body, '/deepcode') &&
17+
github.event.comment.user.login == github.event.repository.owner.login
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
ref: ${{ github.event.issue.pull_request && format('refs/pull/{0}/head', github.event.issue.number) || github.ref }}
24+
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: '20'
28+
29+
- name: Install deep-code
30+
run: npm i -g @liwenkai/deepcode
31+
32+
# 提取 /deepcode 后面的内容作为指令
33+
- name: Extract prompt
34+
id: prompt
35+
uses: actions/github-script@v7
36+
with:
37+
result-encoding: string
38+
script: |
39+
const body = context.payload.comment.body;
40+
return body.replace(/^\/deepcode\s*/, '').trim() || '请分析一下这个项目';
41+
42+
# 启动 deep-code HTTP 服务(不需要 TTY)
43+
- name: Start deep-code server
44+
env:
45+
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
46+
# 预放行所有工具,绕过交互式审批
47+
DEEP_CODE_APPROVAL_AUTO_ALLOW: "read_,list_,grep_,write_,apply_,shell,job,fetch_,web_"
48+
run: |
49+
deepcode serve --http --port 7878 --auth-token bot-token &
50+
echo "等待服务就绪..."
51+
for i in $(seq 1 15); do
52+
if curl -s http://127.0.0.1:7878/health > /dev/null 2>&1; then
53+
echo "服务已就绪"
54+
break
55+
fi
56+
sleep 1
57+
done
58+
59+
# 发送指令给 deep-code,等待完成,保存返回的 SSE 事件
60+
- name: Run deep-code
61+
run: |
62+
PROMPT='${{ steps.prompt.outputs.result }}'
63+
echo "指令: $PROMPT"
64+
65+
# 调用 /v1/prompt API,返回 SSE 流
66+
curl -s -N -X POST http://127.0.0.1:7878/v1/prompt \
67+
-H "Authorization: Bearer bot-token" \
68+
-H "Content-Type: application/json" \
69+
-d "{\"prompt\": \"$PROMPT\"}" \
70+
> deepcode-events.txt 2>&1
71+
72+
# 从 SSE 中提取 provider 事件的文本内容(AI 的回答)
73+
grep '^data: ' deepcode-events.txt | while read -r line; do
74+
kind=$(echo "$line" | sed 's/^data: //' | jq -r '.item.kind // empty' 2>/dev/null)
75+
if [ "$kind" = "provider" ]; then
76+
echo "$line" | sed 's/^data: //' | jq -r '.item.payload.delta // empty' 2>/dev/null
77+
fi
78+
done > deepcode-answer.txt
79+
80+
# 如果 AI 回答为空,把原始事件存下来方便排查
81+
if [ ! -s deepcode-answer.txt ]; then
82+
echo "(deep-code 未输出文本回答,以下为原始事件摘要)" > deepcode-answer.txt
83+
grep '^data: ' deepcode-events.txt | head -20 | sed 's/^data: //' | while read -r line; do
84+
kind=$(echo "$line" | jq -r '.item.kind // empty' 2>/dev/null)
85+
echo "- 事件: $kind"
86+
done >> deepcode-answer.txt
87+
fi
88+
89+
# 检查是否有文件被修改
90+
- name: Check diff
91+
id: diff
92+
run: |
93+
if [[ -n $(git status --porcelain) ]]; then
94+
echo "has_changes=true" >> $GITHUB_OUTPUT
95+
echo "修改的文件:" >> deepcode-summary.txt
96+
git status --short >> deepcode-summary.txt
97+
else
98+
echo "has_changes=false" >> $GITHUB_OUTPUT
99+
fi
100+
101+
# 有文件改动则提交 PR
102+
- name: Create PR
103+
if: steps.diff.outputs.has_changes == 'true'
104+
env:
105+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
106+
run: |
107+
BRANCH="deepcode-bot-$(date +%s)"
108+
git config user.name "deep-code-bot"
109+
git config user.email "bot@deep-code.dev"
110+
git checkout -b "$BRANCH"
111+
git add -A
112+
git commit -m "🤖 deep-code: ${{ steps.prompt.outputs.result }}"
113+
git push origin "$BRANCH"
114+
gh pr create \
115+
--title "🤖 deep-code: ${{ steps.prompt.outputs.result }}" \
116+
--body "由 @${{ github.event.comment.user.login }} 在 Issue #${{ github.event.issue.number }} 中触发" \
117+
--head "$BRANCH" \
118+
--label "🤖 deep-code"
119+
120+
# 把 deep-code 的回答贴回 Issue 评论区
121+
- name: Post reply
122+
uses: actions/github-script@v7
123+
with:
124+
script: |
125+
const fs = require('fs');
126+
const answer = fs.readFileSync('deepcode-answer.txt', 'utf8').slice(0, 60000);
127+
const hasChanges = '${{ steps.diff.outputs.has_changes }}' === 'true';
128+
129+
let body = '## 🤖 Deep Code\n\n';
130+
body += '```\n' + answer + '\n```\n\n';
131+
132+
if (hasChanges) {
133+
body += '✅ **已提交 PR,请查看仓库中的新 PR。**';
134+
} else {
135+
body += '✅ 分析完毕,当前代码无需修改。';
136+
}
137+
138+
github.rest.issues.createComment({
139+
issue_number: context.issue.number,
140+
owner: context.repo.owner,
141+
repo: context.repo.repo,
142+
body: body
143+
});

0 commit comments

Comments
 (0)