Skip to content

Commit 3907622

Browse files
author
siquanlv
committed
ai code review
1 parent 6a4fad8 commit 3907622

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

.github/scripts/ai_code_reviewer.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,42 @@ def get_llm_response(prompt, api_key, model_name="gpt-4o"): # 或 "gemini-pro"
116116
llm_api_key = os.environ.get('LLM_API_KEY') # 统一使用 LLM_API_KEY 环境变量
117117
llm_model_name = os.environ.get('LLM_MODEL_NAME', 'gpt-4o') # 默认为 gpt-4o
118118

119+
# <-- 新增的调试输出 -->
120+
print(f"--- Python Script Init Debug ---")
121+
print(f"Read GITHUB_BASE_REF: {base_ref}")
122+
print(f"Read GITHUB_HEAD_REF: {head_ref}")
123+
print(f"Read GITHUB_PR_NUMBER: {pull_request_number}")
124+
print(f"Read GITHUB_REPOSITORY: {repo_name}")
125+
print(f"LLM_API_KEY is {'present' if llm_api_key else 'MISSING'}")
126+
print(f"--- End Python Script Init Debug ---")
127+
# <-- 结束新增的调试输出 -->
128+
119129
if not all([base_ref, head_ref, pull_request_number, repo_name, llm_api_key]):
120130
print("Missing required environment variables.")
131+
# 更详细的缺失信息
132+
missing_vars = []
133+
if not base_ref: missing_vars.append("GITHUB_BASE_REF")
134+
if not head_ref: missing_vars.append("GITHUB_HEAD_REF")
135+
if not pull_request_number: missing_vars.append("GITHUB_PR_NUMBER")
136+
if not repo_name: missing_vars.append("GITHUB_REPOSITORY")
137+
if not llm_api_key: missing_vars.append("LLM_API_KEY")
138+
print(f"Specifically missing: {', '.join(missing_vars)}")
121139
sys.exit(1)
122140

123141
print(f"Reviewing PR #{pull_request_number} for {repo_name}...")
124142
print(f"Base Ref: {base_ref}, Head Ref: {head_ref}")
125143

126144
# 切换到 base_ref 以便正确获取 diff
127-
subprocess.run(f"git fetch origin {base_ref}", shell=True, check=True)
128-
subprocess.run(f"git checkout {base_ref}", shell=True, check=True)
145+
# subprocess.run(f"git fetch origin {base_ref}", shell=True, check=True)
146+
# subprocess.run(f"git checkout {base_ref}", shell=True, check=True)
129147

130148
# 获取所有变更的 .java 文件路径
131149
changed_java_files_command = f"git diff --name-only {base_ref} {head_ref} | grep '\\.java$'"
150+
print(f"Executing git diff command: {changed_java_files_command}") # <-- 新增调试:打印实际执行的命令
132151
changed_java_files = subprocess.check_output(changed_java_files_command, shell=True, text=True, encoding='utf-8').strip()
133152
changed_files_list = changed_java_files.split('\n') if changed_java_files else []
134153

154+
135155
if not changed_java_files:
136156
print("No Java files changed in this PR. Skipping AI review.")
137157
# 在 GitHub Actions 中,如果不想让这一步失败,可以 exit 0 或不输出任何内容

.github/workflows/ai_code_review.yml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,55 @@ name: AI Powered Code Review
33

44
on:
55
pull_request:
6-
types: [opened, reopened, synchronize] # 当 PR 打开、重开或有新提交时触发
6+
types: [opened, reopened, synchronize]
77

88
jobs:
99
ai_review:
10-
runs-on: ubuntu-latest # 使用 Ubuntu 运行器
10+
runs-on: ubuntu-latest
1111
permissions:
12-
contents: read # 读取代码的权限
13-
pull-requests: write # 在 PR 上发表评论的权限
12+
contents: read
13+
pull-requests: write
1414

1515
steps:
1616
- name: Checkout Code
1717
uses: actions/checkout@v4
1818
with:
19-
fetch-depth: 0 # 必须获取完整的历史,以便 git diff 正确工作
19+
fetch-depth: 0
2020

2121
- name: Set up Python
2222
uses: actions/setup-python@v5
2323
with:
24-
python-version: '3.9' # 或者你喜欢的 Python 版本
24+
python-version: '3.9'
2525

2626
- name: Install Python Dependencies
27-
run: pip install openai # 或者 google-generativeai,取决于你选择的 LLM
27+
run: pip install openai
2828

29-
- name: Get Pull Request Details
29+
- name: Get Pull Request Details and Set Env # 保持这一步,它将 SHA 设置为环境变量
3030
id: pr_details
3131
run: |
3232
echo "GITHUB_BASE_REF=${{ github.event.pull_request.base.sha }}" >> $GITHUB_ENV
3333
echo "GITHUB_HEAD_REF=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
3434
echo "GITHUB_PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
3535
echo "GITHUB_REPOSITORY=${{ github.repository }}" >> $GITHUB_ENV
3636
37+
- name: **Debug Env Vars Before Python Script** # <-- 新增的调试步骤
38+
run: |
39+
echo "Current GITHUB_BASE_REF: $GITHUB_BASE_REF"
40+
echo "Current GITHUB_HEAD_REF: $GITHUB_HEAD_REF"
41+
echo "Current GITHUB_PR_NUMBER: $GITHUB_PR_NUMBER"
42+
echo "Current GITHUB_REPOSITORY: $GITHUB_REPOSITORY"
43+
echo "LLM_API_KEY is present: ${LLM_API_KEY:+true}" # 检查API key是否存在,不打印值
44+
env:
45+
LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }} # 需要在这里也传递,以便在此步骤中访问
46+
3747
- name: Run AI Code Review
3848
run: python .github/scripts/ai_code_reviewer.py
3949
env:
40-
LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }} # 或者 ${{ secrets.GEMINI_API_KEY }}
41-
LLM_MODEL_NAME: gpt-4o # 或者 gemini-pro
42-
# GITHUB_TOKEN 是 Actions 默认提供的,用于 API 交互,这里会被 gh cli 自动使用
43-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }}
51+
LLM_MODEL_NAME: gpt-4o
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
# 明确传递这些环境变量,确保Python脚本能获取到正确的值
54+
GITHUB_BASE_REF: ${{ env.GITHUB_BASE_REF }}
55+
GITHUB_HEAD_REF: ${{ env.GITHUB_HEAD_REF }}
56+
GITHUB_PR_NUMBER: ${{ env.GITHUB_PR_NUMBER }}
57+
GITHUB_REPOSITORY: ${{ env.GITHUB_REPOSITORY }}

0 commit comments

Comments
 (0)