Skip to content

Commit 980cd34

Browse files
authored
Merge pull request #27 from mimo-x/optimize_info
fix some issue
2 parents 6ee2b0a + 55476be commit 980cd34

File tree

12 files changed

+69
-49
lines changed

12 files changed

+69
-49
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ app/mrmax.py
44
db/*
55
!db/.gitkeep
66
/venv/
7+
.config.py
8+
__pycache__/
9+
.DS_Store
710

811
/config/config_local.py
912

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,12 @@
176176

177177
```bash
178178
git clone git@github.com:mimo-x/Code-Review-GPT-Gitlab.git
179+
179180
cd Code-Review-GPT-Gitlab
181+
180182
vim ./config/config.py
181-
docker run -d -v ./config:/workspace/config -p 8080:80 --name codereview xuxin1/llmcodereview:latest
183+
184+
docker run -d --network bridge --add-host=host.docker.internal:host-gateway -v ./config:/workspace/config -p 8080:80 --name codereview xuxin1/llmcodereview:latest
182185
```
183186

184187
#### 源代码运行 💻

config/config.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
# api 接口封装类
2+
23
llm_api_impl = "large_model.api.default_api.DefaultApi"
34

45
# DeepSeek配置示例
56
# api 配置方式参考 docs/config.md
67
# 默认使用认UnionLLM,参考:https://github.com/EvalsOne/UnionLLM/tree/main/docs
78
# UnionLLM兼容LiteLLM,参考LiteLLM文档:https://docs.litellm.ai/docs
9+
810
api_config = {
911
"api_key": "your deepseek key",
1012
"model": 'deepseek-chat',
1113
"provider": "deepseek",
1214
}
13-
# # GPT API 配置示例
14-
# # model list
15-
# model_gpt_35_turbo = "gpt-3.5-turbo"
16-
# model_gpt_4o = "gpt-4o"
17-
#
15+
16+
# demo-proxy-gpt
1817
# api_config = {
1918
# "api_key": "your openai key",
2019
# "api_base": "https://api.openai.com/v1",
21-
# "model": model_gpt_4o,
20+
# "model": "gpt_4o",
2221
# "provider": "openai",
2322
# }
2423

25-
# ollama 配置示例
24+
# demo-ollama
2625
# api_config = {
2726
# "api_base": "http://localhost:11434",
2827
# "model": "llama3.2",
2928
# "provider": "ollama",
3029
# }
3130

31+
# demo-azure
32+
# api_config = {
33+
# "AZURE_API_KEY": "*",
34+
# "AZURE_API_BASE": "https://*.openai.azure.com",
35+
# "AZURE_API_VERSION": "2024-10-21",
36+
# "model": "azure/o1-mini",
37+
# }
38+
3239
# Prompt
33-
gpt_message = """
40+
GPT_MESSAGE = """
3441
你是一位资深编程专家,gitlab的分支代码变更将以git diff 字符串的形式提供,请你帮忙review本段代码。然后你review内容的返回内容必须严格遵守下面的格式,包括标题内容。模板中的变量内容解释:
3542
变量5为: 代码中的优点。变量1:给review打分,分数区间为0~100分。变量2:code review发现的问题点。变量3:具体的修改建议。变量4:是你给出的修改后的代码。
3643
必须要求:1. 以精炼的语言、严厉的语气指出存在的问题。2. 你的反馈内容必须使用严谨的markdown格式 3. 不要携带变量内容解释信息。4. 有清晰的标题结构。有清晰的标题结构。有清晰的标题结构。
@@ -57,16 +64,24 @@
5764

5865
# ------------------Gitlab info--------------------------
5966
# Gitlab url
60-
gitlab_server_url = "https://gitlab.com"
67+
GITLAB_SERVER_URL = "https://gitlab.com"
6168

6269
# Gitlab private token
63-
gitlab_private_token = "gitlab private token"
70+
GITLAB_PRIVATE_TOKEN = "gitlab private token"
6471

6572
# Gitlab modifies the maximum number of files
66-
maximum_files = 50
73+
MAX_FILES = 50
6774

6875

6976
# ------------- Message notification --------------------
7077
# dingding notification (un necessary)
71-
dingding_bot_webhook = "https://oapi.dingtalk.com/robot/send?access_token=*****************************************"
72-
dingding_secret = "S********************************950f"
78+
DINGDING_BOT_WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=*****************************************"
79+
DINGDING_SECRET = "S********************************950f"
80+
81+
82+
# ------------- code review settings --------------------
83+
# expect file types
84+
EXCLUDE_FILE_TYPES = ['.py', '.java', '.class', '.vue', ".go",".c",".cpp"]
85+
86+
# ignore file types
87+
IGNORE_FILE_TYPES = ["mod.go"]

doc/config.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ api_config = {
7575

7676

7777
## Gitlab配置
78-
- `gitlab_server_url`: Gitlab服务器地址
79-
- `gitlab_private_token`: Gitlab私有令牌
78+
- `GITLAB_SERVER_URL`: Gitlab服务器地址
79+
- `GITLAB_PRIVATE_TOKEN`: Gitlab私有令牌
8080
- `maximum_files`: Gitlab Merge Request最大文件数
8181

8282
## 消息通知配置

doc/review.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class CustomReviewHandle(ReviewHandle):
349349
content = filter_diff_content(change['diff'])
350350
messages = [
351351
{"role": "system",
352-
"content": gpt_message
352+
"content": GPT_MESSAGE
353353
},
354354
{"role": "user",
355355
"content": f"请review这部分代码变更{content}",

gitlab_integration/gitlab_fetcher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def get_changes(self, force=False):
2828
if self._changes_cache and not force:
2929
return self._changes_cache
3030
# URL for the GitLab API endpoint
31-
url = f"{gitlab_server_url}/api/v4/projects/{self.project_id}/merge_requests/{self.iid}/changes"
31+
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{self.project_id}/merge_requests/{self.iid}/changes"
3232

3333
# Headers for the request
3434
headers = {
35-
"PRIVATE-TOKEN": gitlab_private_token
35+
"PRIVATE-TOKEN": GITLAB_PRIVATE_TOKEN
3636
}
3737

3838
# Make the GET request
@@ -58,11 +58,11 @@ def get_file_content(self, file_path, branch_name='main', force=False):
5858
if file_path in self._file_content_cache and not force:
5959
return self._file_content_cache[file_path]
6060
# URL for the GitLab API endpoint
61-
url = f"{gitlab_server_url}/api/v4/projects/{self.project_id}/repository/files/{file_path}/raw?ref={branch_name}"
61+
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{self.project_id}/repository/files/{file_path}/raw?ref={branch_name}"
6262

6363
# Headers for the request
6464
headers = {
65-
"PRIVATE-TOKEN": gitlab_private_token
65+
"PRIVATE-TOKEN": GITLAB_PRIVATE_TOKEN
6666
}
6767

6868
# Make the GET request
@@ -84,11 +84,11 @@ def get_info(self, force=False):
8484
if self._info_cache and not force:
8585
return self._info_cache
8686
# URL for the GitLab API endpoint
87-
url = f"{gitlab_server_url}/api/v4/projects/{self.project_id}/merge_requests/{self.iid}"
87+
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{self.project_id}/merge_requests/{self.iid}"
8888

8989
# Headers for the request
9090
headers = {
91-
"PRIVATE-TOKEN": gitlab_private_token
91+
"PRIVATE-TOKEN": GITLAB_PRIVATE_TOKEN
9292
}
9393

9494
# Make the GET request
@@ -115,11 +115,11 @@ def get_info(self):
115115
:return: Project information
116116
"""
117117
# URL for the GitLab API endpoint
118-
url = f"{gitlab_server_url}/api/v4/projects/{self.project_id}"
118+
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{self.project_id}"
119119

120120
# Headers for the request
121121
headers = {
122-
"PRIVATE-TOKEN": gitlab_private_token
122+
"PRIVATE-TOKEN": GITLAB_PRIVATE_TOKEN
123123
}
124124

125125
# Make the GET request
@@ -195,7 +195,7 @@ def find_files_by_keyword(self, keyword, branch_name="main"):
195195
# 构建带有身份验证信息的 URL
196196
def _build_authenticated_url(self, repo_url):
197197
# 如果 URL 使用 https
198-
token = gitlab_private_token
198+
token = GITLAB_PRIVATE_TOKEN
199199
if repo_url.startswith("https://"):
200200
return f"https://oauth2:{token}@{repo_url[8:]}"
201201
# 如果 URL 使用 http

large_model/api/default_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ def set_config(self, api_config: dict) -> bool:
1616
if api_config is None:
1717
raise ValueError("api_config is None")
1818
for key in api_config:
19-
self.params[key] = api_config[key]
2019
# 如果为大写,则写入环境变量
2120
if key.isupper():
2221
os.environ[key] = api_config[key]
22+
continue
23+
self.params[key] = api_config[key]
24+
2325
return True
2426

2527
def generate_text(self, messages: list) -> bool:

response_module/response_target/msg_response/dingtalk_response.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def send_dingtalk_message_by_sign(self, message_text):
3636
"""
3737
timestamp = str(round(time.time() * 1000))
3838
sign = self.__get_sign(timestamp)
39-
webhookurl = f"{dingding_bot_webhook}&timestamp={timestamp}&sign={sign}"
39+
webhookurl = f"{DINGDING_BOT_WEBHOOK}&timestamp={timestamp}&sign={sign}"
4040
# 构建请求头
4141
headers = {
4242
"Content-Type": "application/json",
@@ -75,7 +75,7 @@ def send_dingtalk_message_by_key_word(self, project_url):
7575
7676
"""
7777
# 设置钉钉机器人的 Webhook URL
78-
webhook_url = dingding_bot_webhook
78+
webhook_url = DINGDING_BOT_WEBHOOK
7979

8080
# 要发送的消息内容
8181
message = f"新工程接入\nurl:{project_url}"
@@ -97,7 +97,7 @@ def __get_sign(self, timestamp):
9797
:return: 签名
9898
'''
9999

100-
secret = dingding_secret
100+
secret = DINGDING_SECRET
101101
secret_enc = secret.encode('utf-8')
102102
string_to_sign = '{}\n{}'.format(timestamp, secret)
103103
string_to_sign_enc = string_to_sign.encode('utf-8')

response_module/response_target/msg_response/gitlab_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def send(self, message):
2222
@retry(stop_max_attempt_number=3, wait_fixed=2000)
2323
def send_merge(self, message):
2424
headers = {
25-
"Private-Token": gitlab_private_token,
25+
"Private-Token": GITLAB_PRIVATE_TOKEN,
2626
"Content-Type": "application/json"
2727
}
2828
project_id = self.project_id
2929
merge_request_id = self.merge_request_id
30-
url = f"{gitlab_server_url}/api/v4/projects/{project_id}/merge_requests/{merge_request_id}/notes"
30+
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{project_id}/merge_requests/{merge_request_id}/notes"
3131
data = {
3232
"body": message
3333
}

review_engine/handler/default_handler.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import threading
33

44
from retrying import retry
5-
6-
from config.config import gpt_message
5+
from config.config import GPT_MESSAGE, EXCLUDE_FILE_TYPES, IGNORE_FILE_TYPES, MAX_FILES
76
from review_engine.abstract_handler import ReviewHandle
87
from utils.gitlab_parser import filter_diff_content
98
from utils.logger import log
@@ -22,8 +21,8 @@ def process_change(change):
2221

2322
futures = []
2423
for change in changes:
25-
if any(change["new_path"].endswith(ext) for ext in ['.py', '.java', '.class', '.vue', ".go"]) and not any(
26-
change["new_path"].endswith(ext) for ext in ["mod.go"]):
24+
if any(change["new_path"].endswith(ext) for ext in EXCLUDE_FILE_TYPES) and not any(
25+
change["new_path"].endswith(ext) for ext in IGNORE_FILE_TYPES):
2726
futures.append(executor.submit(process_change, change))
2827
else:
2928
log.info(f"{change['new_path']} 非目标检测文件!")
@@ -38,7 +37,7 @@ def generate_review_note(change, model):
3837
content = filter_diff_content(change['diff'])
3938
messages = [
4039
{"role": "system",
41-
"content": gpt_message
40+
"content": GPT_MESSAGE
4241
},
4342
{"role": "user",
4443
"content": f"请review这部分代码变更{content}",
@@ -66,8 +65,7 @@ def merge_handle(self, gitlabMergeRequestFetcher, gitlabRepoManager, hook_info,
6665
self.default_handle(changes, merge_info, hook_info, reply, model)
6766

6867
def default_handle(self, changes, merge_info, hook_info, reply, model):
69-
maximum_files = 50
70-
if changes and len(changes) <= maximum_files:
68+
if changes and len(changes) <= MAX_FILES:
7169
# Code Review 信息
7270
review_info = chat_review(changes, generate_review_note, model)
7371
if review_info:
@@ -110,7 +108,7 @@ def default_handle(self, changes, merge_info, hook_info, reply, model):
110108
})
111109

112110

113-
elif changes and len(changes) > maximum_files:
111+
elif changes and len(changes) > MAX_FILES:
114112
reply.add_reply({
115113
'title': '__MAIN_REVIEW__',
116114
'content': (

0 commit comments

Comments
 (0)