diff --git a/.github/workflows/ai-code-review.yml b/.github/workflows/ai-code-review.yml index bb7317d..ad9fa25 100644 --- a/.github/workflows/ai-code-review.yml +++ b/.github/workflows/ai-code-review.yml @@ -1,45 +1,60 @@ -# https://github.com/anc95/ChatGPT-CodeReview/tree/main -name: Code Review +name: DeepSeek API Request and PR Comment permissions: contents: read - pull-requests: write + pull-requests: write # 需要写权限才能在 PR 上发表评论 on: - pull_request_target: + pull_request_target: # 触发目标仓库的 PR 操作 types: [opened, reopened, synchronize] jobs: - test: - # if: ${{ contains(github.event.*.labels.*.name, 'gpt review') }} # Optional; to run only when a label is attached + review: runs-on: ubuntu-latest + steps: - - uses: anc95/ChatGPT-CodeReview@main - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - OPENAI_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }} - # Optional - LANGUAGE: Chinese - OPENAI_API_ENDPOINT: https://api.deepseek.com/v1 - MODEL: deepseek-chat - PROMPT: | - 我将提供给你一段github pull request的file diff。请你扮演一位专业的开源社区开发者,帮我进行code review。 - [[代码审核原则]] - [代码质量] - 1. 该修改是否有必要,是否相比旧代码有改进或添加了新功能? - 2. 是否有逻辑错误或潜在的运行时错误? - 3. 是否有与其他代码部分的兼容性问题,会不会破坏与修改无关的现有逻辑? - 4. 是否有可以优化的代码结构或逻辑,是否有更简洁或更高效的方式来实现相同功能? - 5. 是否有潜在的性能问题? - [安全性] - 1. 是否有敏感信息泄露的风险(如硬编码的密钥、密码等)? - 2. 是否有可能运行来路不明的外部代码? + - name: Checkout the target branch + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.base.ref }} - [[结果返回格式]] - 如果发现任何问题或改进建议,请分点详细列出并说明;如果没有问题,请直接输出“LGTM”表示通过。 - top_p: 1 - temperature: 1.0 - max_tokens: 8192 - MAX_PATCH_LENGTH: # if the patch/diff length is large than MAX_PATCH_LENGTH, will be ignored and won't review. By default, with no MAX_PATCH_LENGTH set, there is also no limit for the patch/diff length. - IGNORE_PATTERNS: # glob pattern or regex pattern to ignore files, separated by comma - INCLUDE_PATTERNS: # glob pattern or regex pattern to include files, separated by comma + - name: Checkout the source branch + run: | + git fetch origin ${{ github.event.pull_request.head.ref }} + git checkout ${{ github.event.pull_request.head.ref }} + - name: Get pull request diff + id: pr_diff + run: | + # 获取 PR 的 diff 信息 + PR_DIFF=$(curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files") + echo "$PR_DIFF" > pr_diff.json + echo "diff_file=pr_diff.json" >> $GITHUB_ENV + - name: Send request to DeepSeek API for Code Review + id: deepseek_response + run: | + PR_DIFF_CONTENT=$(cat ${{ env.diff_file }}) + response=$(curl https://api.deepseek.com/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer sk-d5f21dec47a244d5a899a26ba813ed2e" \ + -d '{ + "model": "deepseek-chat", + "messages": [ + {"role": "system", "content": "我将提供给你一段github pull request的file diff。请你扮演一位专业的开源社区开发者,帮我进行code review。\n\n[[代码审核原则]]\n[代码质量]\n1. 该修改是否有必要,是否相比旧代码有改进或添加了新功能?\n2. 是否有逻辑错误或潜在的运行时错误?\n3. 是否有与其他代码部分的兼容性问题,会不会破坏与修改无关的现有逻辑?\n4. 是否有可以优化的代码结构或逻辑,是否有更简洁或更高效的方式来实现相同功能?\n5. 是否有潜在的性能问题?\n[安全性]\n1. 是否有敏感信息泄露的风险(如硬编码的密钥、密码等)?\n2. 是否有可能运行来路不明的外部代码?\n\n[[结果返回格式]]\n如果发现任何问题或改进建议,请分点详细列出并说明;如果没有问题,请直接输出“LGTM”表示通过。${PR_DIFF_CONTENT}"}, + {"role": "user", "content": "${PR_DIFF_CONTENT}"} + ], + "stream": false + }') + echo "$response" > response.json + echo "$response" | jq . + - name: Post comment on PR with review result + run: | + COMMENT=$(jq -r '.choices[0].message.content' response.json) + curl -X POST \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d '{ + "body": "'"$COMMENT"'", + "note": "Code Review by DeepSeek" + }' \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"