1
- from analyzers .ast_parser import ASTAnalyzer
2
- from rules import style , security
1
+ # from analyzers.ast_parser import ASTAnalyzer
2
+ # from rules import style, security
3
3
4
- def code_review (file_path ):
5
- # AST解析
6
- with open (file_path ) as f :
7
- tree = ast .parse (f .read ())
4
+ # def code_review(file_path):
5
+ # # AST解析
6
+ # with open(file_path) as f:
7
+ # tree = ast.parse(f.read())
8
+
9
+ # # 执行静态分析
10
+ # analyzer = ASTAnalyzer()
11
+ # analyzer.visit(tree)
12
+
13
+ # # 应用规则检查
14
+ # style_violations = StyleRules.check_naming_convention(tree)
15
+ # security_issues = SecurityRules.check_injection(tree)
8
16
9
- # 执行静态分析
10
- analyzer = ASTAnalyzer ()
11
- analyzer .visit (tree )
17
+ # # AI增强审查
18
+ # if config['ai']['enable']:
19
+ # ai_reviewer = AICodeReviewer(config['ai']['api_key'])
20
+ # ai_suggestions = ai_reviewer.get_optimization_suggestion(code_snippet)
21
+
22
+ # # 生成报告
23
+ # generate_report({
24
+ # 'static_analysis': analyzer.findings,
25
+ # 'style_violations': style_violations,
26
+ # 'ai_suggestions': ai_suggestions
27
+ # })
28
+ import click
29
+ from ai_review .core .analyzer import CodeAnalyzer
30
+ from ai_review .core .model_adapter import AIModelHandler
31
+
32
+ @click .command ()
33
+ @click .argument ('file_path' )
34
+ @click .option ('--ai' , is_flag = True , help = '启用AI增强审查' )
35
+ def review (file_path : str , ai : bool ):
36
+ """执行代码审查流水线"""
37
+ with open (file_path ) as f :
38
+ code = f .read ()
12
39
13
- # 应用规则检查
14
- style_violations = StyleRules . check_naming_convention ( tree )
15
- security_issues = SecurityRules . check_injection ( tree )
40
+ # 静态规则审查
41
+ analyzer = CodeAnalyzer ([ 'ai_review.rules.security_rules' ] )
42
+ findings = analyzer . analyze ( code )
16
43
17
- # AI增强审查
18
- if config ['ai' ]['enable' ]:
19
- ai_reviewer = AICodeReviewer (config ['ai' ]['api_key' ])
20
- ai_suggestions = ai_reviewer .get_optimization_suggestion (code_snippet )
44
+ # AI增强分析
45
+ if ai :
46
+ ai_handler = AIModelHandler ()
47
+ ai_comment = ai_handler .get_code_review (code , {'findings' : findings })
48
+ click .echo (f"\n AI审查建议:\n { ai_comment } " )
21
49
22
- # 生成报告
23
- generate_report ({
24
- 'static_analysis' : analyzer .findings ,
25
- 'style_violations' : style_violations ,
26
- 'ai_suggestions' : ai_suggestions
27
- })
50
+ # 输出格式化结果
51
+ click .echo ("\n 静态审查结果:" )
52
+ for issue in findings :
53
+ click .secho (f"[{ issue ['severity' ].upper ()} ] Line { issue ['line' ]} : { issue ['message' ]} " ,
54
+ fg = 'red' if issue ['severity' ] == 'critical' else 'yellow' )
0 commit comments