Update kimi-pr-review.yml #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Kimi PR Review | ||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize] | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| jobs: | ||
| review: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Get PR Diff | ||
| id: diff | ||
| run: | | ||
| git fetch origin ${{ github.event.pull_request.base.ref }} | ||
| git diff origin/${{ github.event.pull_request.base.ref }}..HEAD > pr_diff.txt | ||
| head -c 12000 pr_diff.txt > pr_diff_small.txt | ||
| - name: Kimi Review with Python | ||
| env: | ||
| KIMI_API_KEY: ${{ secrets.KIMI_API_KEY }} | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| REPO: ${{ github.repository }} | ||
| run: | | ||
| python3 << 'PYTHON_SCRIPT' | ||
| import os | ||
| import json | ||
| import urllib.request | ||
| import urllib.error | ||
| # 读取diff | ||
| with open('pr_diff_small.txt', 'r') as f: | ||
| diff = f.read() | ||
| # 构造请求 | ||
| payload = { | ||
| "model": "kimi-k2", | ||
| "messages": [ | ||
| { | ||
| "role": "system", | ||
| "content": """You are a professional code reviewer. Analyze the code changes and provide a bilingual report in this exact format: | ||
| ## 中文审查报告 | ||
| **变更总结**: (中文简述做了什么改动) | ||
| **问题与建议**: (中文列出潜在问题或改进建议) | ||
| **代码评分**: X/5 | ||
| --- | ||
| ## English Review Report | ||
| **Summary**: (English description of changes) | ||
| **Issues & Suggestions**: (English list of issues/improvements) | ||
| **Rating**: X/5 stars | ||
| Keep both sections professional and concise.""" | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": f"Please review this code diff:\n\n{diff}" | ||
| } | ||
| ] | ||
| } | ||
| # 调用Kimi API | ||
| req = urllib.request.Request( | ||
| 'https://api.moonshot.cn/v1/chat/completions', | ||
| data=json.dumps(payload).encode('utf-8'), | ||
| headers={ | ||
| 'Authorization': f'Bearer {os.environ["KIMI_API_KEY"]}', | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| method='POST' | ||
| ) | ||
| try: | ||
| with urllib.request.urlopen(req) as response: | ||
| result = json.loads(response.read().decode('utf-8')) | ||
| review = result['choices'][0]['message']['content'] | ||
| except Exception as e: | ||
| review = f"API调用失败: {str(e)}" | ||
| # 提交评论 | ||
| comment_payload = json.dumps({ | ||
| "body": f"## 🤖 Kimi Code Review Report\n\n{review}\n\n---\n*Generated by Kimi AI*" | ||
| }).encode('utf-8') | ||
| comment_req = urllib.request.Request( | ||
| f'https://api.github.com/repos/{os.environ["REPO"]}/issues/{os.environ["PR_NUMBER"]}/comments', | ||
| data=comment_payload, | ||
| headers={ | ||
| 'Authorization': f'token {os.environ["GITHUB_TOKEN"]}', | ||
| 'Accept': 'application/vnd.github.v3+json' | ||
| }, | ||
| method='POST' | ||
| ) | ||
| try: | ||
| urllib.request.urlopen(comment_req) | ||
| print("评论提交成功") | ||
| except Exception as e: | ||
| print(f"评论提交失败: {e}") | ||
| PYTHON_SCRIPT | ||