Skip to content

Commit c81344d

Browse files
committed
Merge branch 'main_upstream'
# Conflicts: # .github/CODEOWNERS # Dockerfile # biz/queue/worker.py # biz/utils/code_reviewer.py # doc/faq.md # docker-compose.prod.yml # docker-compose.yml
2 parents 4a41cd6 + 5ac1c74 commit c81344d

File tree

8 files changed

+125
-60
lines changed

8 files changed

+125
-60
lines changed

README.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
## 原理
2525

26-
当用户在 GitLab 上提交代码(如 Merge Request 或 Push 操作)时,GitLab 将自动触发 webhook 事件,调用本系统的接口。系统随后通过第三方大模型对代码进行审查,并将审查结果直接反馈到对应的 Merge Request 或 Commit 的 Note 中,便于团队查看和处理。
26+
当用户在 GitLab 上提交代码(如 Merge Request 或 Push 操作)时,GitLab 将自动触发 webhook
27+
事件,调用本系统的接口。系统随后通过第三方大模型对代码进行审查,并将审查结果直接反馈到对应的 Merge Request 或 Commit 的
28+
Note 中,便于团队查看和处理。
2729

2830
![流程图](./doc/img/process.png)
2931

@@ -137,34 +139,15 @@ streamlit run ui.py --server.port=5002 --server.address=0.0.0.0
137139
DINGTALK_WEBHOOK_URL=https://oapi.dingtalk.com/robot/send?access_token=xxx #替换为你的Webhook URL
138140
```
139141

140-
#### 2.配置企业微信推送
141-
142-
- 在企业微信群中添加一个自定义机器人,获取 Webhook URL。
143-
144-
- 更新 .env 中的配置:
145-
```
146-
#企业微信配置
147-
WECOM_ENABLED=1 #0不发送企业微信消息,1发送企业微信消息
148-
WECOM_WEBHOOK_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx #替换为你的Webhook URL
149-
```
150-
151-
#### 3.配置飞书推送
152-
153-
- 在飞书群中添加一个自定义机器人,获取 Webhook URL。
154-
- 更新 .env 中的配置:
155-
```
156-
#飞书配置
157-
FEISHU_ENABLED=1
158-
FEISHU_WEBHOOK_URL=https://open.feishu.cn/open-apis/bot/v2/hook/xxx #替换为你的Webhook URL
159-
```
142+
企业微信和飞书推送配置类似,具体参见 [常见问题](doc/faq.md)
160143

161144
## 其它
162145

163146
**1.如何review代码结构?**
164147

165148
可通过命令工具对代码结构进行检查,命令如下:
166149

167-
```aiignore
150+
```bash
168151
python -m biz.cmd.review
169152
```
170153

api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from datetime import datetime
66
from urllib.parse import urlparse
77

8-
from biz.utils.token_util import count_tokens, truncate_text_by_tokens
98
from apscheduler.schedulers.background import BackgroundScheduler
109
from apscheduler.triggers.cron import CronTrigger
1110
from dotenv import load_dotenv

biz/utils/code_reviewer.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import abc
12
import os
23
import re
3-
import yaml
4-
import abc
54
from typing import Dict, Any, List
65

7-
from biz.utils.log import logger
6+
import yaml
7+
88
from biz.llm.factory import Factory
99
from biz.utils.i18n import get_translator
10+
from biz.utils.log import logger
1011
from biz.utils.token_util import count_tokens, truncate_text_by_tokens
1112

1213
_ = get_translator()
@@ -24,7 +25,8 @@ def _load_prompts(self, prompt_key: str) -> Dict[str, Any]:
2425
lang = os.environ.get('LANGUAGE', 'zh_CN')
2526
prompt_templates_file = os.path.join("locales", lang, "prompt_templates.yml")
2627
try:
27-
with open(prompt_templates_file, "r") as file:
28+
# 在打开 YAML 文件时显式指定编码为 UTF-8,避免使用系统默认的 GBK 编码。
29+
with open(prompt_templates_file, "r", encoding="utf-8") as file:
2830
prompts = yaml.safe_load(file).get(prompt_key, {})
2931
system_prompt = prompts.get("system_prompt")
3032
user_prompt = prompts.get("user_prompt")
@@ -60,6 +62,13 @@ def __init__(self):
6062
super().__init__("code_review_prompt")
6163

6264
def review_and_strip_code(self, changes_text: str, commits_text: str = '') -> str:
65+
"""
66+
Review判断changes_text超出取前REVIEW_MAX_TOKENS个token,超出则截断changes_text,
67+
调用review_code方法,返回review_result,如果review_result是markdown格式,则去掉头尾的```
68+
:param changes_text:
69+
:param commits_text:
70+
:return:
71+
"""
6372
# 如果超长,取前REVIEW_MAX_TOKENS个token
6473
review_max_tokens = int(os.getenv('REVIEW_MAX_TOKENS', 10000))
6574
# 如果changes为空,打印日志
@@ -72,7 +81,8 @@ def review_and_strip_code(self, changes_text: str, commits_text: str = '') -> st
7281
if tokens_count > review_max_tokens:
7382
changes_text = truncate_text_by_tokens(changes_text, review_max_tokens)
7483

75-
review_result = CodeReviewer().review_code(changes_text, commits_text).strip()
84+
review_result = self.review_code(changes_text, commits_text).strip()
85+
7686
if review_result.startswith("```markdown") and review_result.endswith("```"):
7787
return review_result[11:-3].strip()
7888
return review_result
@@ -84,8 +94,7 @@ def review_code(self, diffs_text: str, commits_text: str = "") -> str:
8494
{
8595
"role": "user",
8696
"content": self.prompts["user_message"]["content"].format(
87-
diffs_text=diffs_text,
88-
commits_text=commits_text
97+
diffs_text=diffs_text, commits_text=commits_text
8998
),
9099
},
91100
]

biz/utils/queue.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
from redis import Redis
66
from rq import Queue
77

8+
from biz.utils.log import logger
9+
810
load_dotenv()
911
queue_driver = os.getenv('QUEUE_DRIVER', 'async')
1012

1113
if queue_driver == 'rq':
1214
queues = {}
1315

16+
1417
def handle_queue(function: callable, data: any, gitlab_token: str, gitlab_url: str, gitlab_url_slug: str):
1518
if queue_driver == 'rq':
1619
if gitlab_url_slug not in queues:
17-
queues[gitlab_url_slug] = Queue(gitlab_url_slug, connection=Redis(os.getenv('REDIS_HOST', '127.0.0.1'), os.getenv('REDIS_PORT', 6379)))
20+
logger.info(f'REDIS_HOST: {os.getenv("REDIS_HOST", "127.0.0.1")},REDIS_PORT: {os.getenv("REDIS_PORT", 6379)}')
21+
queues[gitlab_url_slug] = Queue(gitlab_url_slug, connection=Redis(os.getenv('REDIS_HOST', '127.0.0.1'),
22+
os.getenv('REDIS_PORT', 6379)))
1823

1924
queues[gitlab_url_slug].enqueue(function, data, gitlab_token, gitlab_url, gitlab_url_slug)
2025
else:

doc/faq.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,53 @@ OLLAMA_API_BASE_URL=http://127.0.0.1:11434 # 错误
8686
OLLAMA_API_BASE_URL=http://{宿主机/外网IP地址}:11434 # 正确
8787
```
8888

89-
#### 6. 翻译
89+
#### 6.如何使用Redis Queue队列?
90+
91+
**操作步骤**
92+
93+
1.开发调试模式下,启动容器:
94+
95+
```
96+
docker compose -f docker-compose.rq.yml up -d
97+
```
98+
99+
2.生产模式下,启动容器:
100+
```
101+
docker compose -f docker-compose.prod.yml up -d
102+
```
103+
104+
**特别说明:**
105+
106+
在 .env 文件中配置 WORKER_QUEUE,其值为 GitLab 域名,并将域名中的点(.)替换为下划线(_)。如果域名为 gitlab.test.cn,则配置为:
107+
108+
```
109+
WORKER_QUEUE=gitlab_test_cn
110+
```
111+
112+
#### 7. 如何配置企业微信和飞书消息推送?
113+
114+
**1.配置企业微信推送**
115+
116+
- 在企业微信群中添加一个自定义机器人,获取 Webhook URL。
117+
118+
- 更新 .env 中的配置:
119+
```
120+
#企业微信配置
121+
WECOM_ENABLED=1 #0不发送企业微信消息,1发送企业微信消息
122+
WECOM_WEBHOOK_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx #替换为你的Webhook URL
123+
```
124+
125+
**2.配置飞书推送**
126+
127+
- 在飞书群中添加一个自定义机器人,获取 Webhook URL。
128+
- 更新 .env 中的配置:
129+
```
130+
#飞书配置
131+
FEISHU_ENABLED=1
132+
FEISHU_WEBHOOK_URL=https://open.feishu.cn/open-apis/bot/v2/hook/xxx #替换为你的Webhook URL
133+
```
134+
135+
#### 8. 翻译
90136

91137
更改翻译后无法正确应用,我该怎么办?
92138
该应用程序使用 python 原生 gettext 实现来加载各种语言的翻译。如果您更改了翻译文件,但是应用程序没有正确应用新的翻译,您可以尝试以下步骤:

docker-compose.prod.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
context: .
2424
dockerfile: Dockerfile
2525
target: worker
26-
# image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
26+
image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
2727
volumes_from:
2828
- app
2929
env_file:
@@ -41,7 +41,7 @@ services:
4141
# context: .
4242
# dockerfile: Dockerfile
4343
# target: worker
44-
# # image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
44+
# image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
4545
# volumes_from:
4646
# - app
4747
# env_file:

docker-compose.rq.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
services:
2+
app:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
target: dev
7+
image: ghcr.io/mashb1t/ai-codereview-gitlab:1
8+
ports:
9+
- "5001:5001"
10+
- "5002:5002"
11+
volumes:
12+
- ./biz:/app/biz
13+
- ./data:/app/data
14+
- ./locales:/app/locales
15+
- ./log:/app/log
16+
- ./api.py:/app/api.py
17+
- ./ui.py:/app/ui.py
18+
env_file:
19+
- ./conf/.env
20+
depends_on:
21+
redis:
22+
condition: service_started
23+
restart: unless-stopped
24+
25+
worker:
26+
build:
27+
context: .
28+
dockerfile: Dockerfile
29+
target: worker
30+
image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
31+
volumes_from:
32+
- app
33+
env_file:
34+
- ./conf/.env
35+
depends_on:
36+
redis:
37+
condition: service_started
38+
restart: unless-stopped
39+
40+
redis:
41+
image: redis:alpine
42+
ports:
43+
- "6379:6379"
44+
volumes:
45+
- redis_data:/data
46+
restart: unless-stopped
47+
48+
volumes:
49+
redis_data:

docker-compose.yml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ services:
1414
- ./locales:/app/locales
1515
- ./log:/app/log
1616
- ./api.py:/app/api.py
17-
- ./requirements.txt:/app/requirements.txt
1817
- ./ui.py:/app/ui.py
1918
env_file:
2019
- ./conf/.env
@@ -23,30 +22,5 @@ services:
2322
condition: service_started
2423
restart: unless-stopped
2524

26-
worker:
27-
build:
28-
context: .
29-
dockerfile: Dockerfile
30-
target: worker
31-
image: ghcr.io/mashb1t/ai-codereview-gitlab:1-worker
32-
volumes_from:
33-
- app
34-
env_file:
35-
- ./conf/.env
36-
depends_on:
37-
redis:
38-
condition: service_started
39-
restart: unless-stopped
40-
41-
redis:
42-
image: redis:alpine
43-
env_file:
44-
- ./conf/.env
45-
ports:
46-
- "6379:6379"
47-
volumes:
48-
- redis_data:/data
49-
restart: unless-stopped
50-
5125
volumes:
5226
redis_data:

0 commit comments

Comments
 (0)