add web
This commit is contained in:
@@ -94,10 +94,8 @@ class DiffParser:
|
||||
|
||||
def merge_issues_with_code(scan_results: Dict[str, Any], diff: str) -> Dict[str, Any]:
|
||||
"""将扫描问题与代码片段关联"""
|
||||
if not diff:
|
||||
return scan_results
|
||||
parser = DiffParser(diff) if diff else None
|
||||
|
||||
parser = DiffParser(diff)
|
||||
enriched_results = {
|
||||
'scanners': [],
|
||||
'summary': scan_results.get('summary', {}),
|
||||
@@ -118,17 +116,93 @@ def merge_issues_with_code(scan_results: Dict[str, Any], diff: str) -> Dict[str,
|
||||
|
||||
issues = scanner_data.get('issues', [])
|
||||
for issue in issues:
|
||||
enriched_issue = enrich_issue_with_code(issue, parser)
|
||||
enriched_issue = enrich_issue_with_code(issue, parser) if parser else issue
|
||||
enriched_scanner['issues'].append(enriched_issue)
|
||||
|
||||
enriched_results['scanners'].append(enriched_scanner)
|
||||
|
||||
# 处理 AI 审查结果,转换为问题格式
|
||||
if 'ai' in scan_results:
|
||||
enriched_results['ai'] = scan_results['ai']
|
||||
ai_issues = convert_ai_reviews_to_issues(scan_results['ai'], parser)
|
||||
enriched_results['ai'] = {
|
||||
'name': 'ai',
|
||||
'issues': ai_issues,
|
||||
'summary': scan_results['ai'].get('summary', ''),
|
||||
'files_reviewed': scan_results['ai'].get('files_reviewed', 0)
|
||||
}
|
||||
|
||||
return enriched_results
|
||||
|
||||
|
||||
def convert_ai_reviews_to_issues(ai_result: Dict[str, Any], parser: Optional[DiffParser] = None) -> List[Dict[str, Any]]:
|
||||
"""将 AI 审查结果转换为问题格式"""
|
||||
issues = []
|
||||
|
||||
reviews = ai_result.get('reviews', [])
|
||||
for review in reviews:
|
||||
file_path = review.get('file', '')
|
||||
review_data = review.get('review', {})
|
||||
|
||||
if not review_data:
|
||||
continue
|
||||
|
||||
# 获取文件内容作为代码上下文
|
||||
code_context = None
|
||||
if parser:
|
||||
matched_path = None
|
||||
for path in parser.files.keys():
|
||||
if file_path.endswith(path) or path.endswith(file_path) or file_path in path:
|
||||
matched_path = path
|
||||
break
|
||||
|
||||
if matched_path:
|
||||
chunk = parser.get_file_content(matched_path)
|
||||
if chunk and chunk.new_content:
|
||||
lines = chunk.new_content.split('\n')[:10]
|
||||
code_context = {
|
||||
'file': matched_path,
|
||||
'line': 1,
|
||||
'preview': '\n'.join(lines),
|
||||
'has_more': len(chunk.new_content.split('\n')) > 10
|
||||
}
|
||||
|
||||
# 处理优点(不作为问题显示)
|
||||
advantages = review_data.get('优点', [])
|
||||
# 处理问题
|
||||
problems = review_data.get('问题', [])
|
||||
for idx, problem in enumerate(problems):
|
||||
issues.append({
|
||||
'file': file_path,
|
||||
'line': 1, # AI 审查不返回具体行号
|
||||
'severity': 'warning',
|
||||
'message': f'[AI 建议] {problem}',
|
||||
'category': 'ai',
|
||||
'code_context': code_context,
|
||||
'review_data': {
|
||||
'type': '问题',
|
||||
'content': problem
|
||||
}
|
||||
})
|
||||
|
||||
# 处理优化建议
|
||||
optimizations = review_data.get('优化', [])
|
||||
for optimization in optimizations:
|
||||
issues.append({
|
||||
'file': file_path,
|
||||
'line': 1,
|
||||
'severity': 'info',
|
||||
'message': f'[AI 优化] {optimization}',
|
||||
'category': 'ai',
|
||||
'code_context': code_context,
|
||||
'review_data': {
|
||||
'type': '优化',
|
||||
'content': optimization
|
||||
}
|
||||
})
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
def enrich_issue_with_code(issue: Dict[str, Any], parser: DiffParser) -> Dict[str, Any]:
|
||||
"""为单个问题添加代码片段"""
|
||||
enriched = issue.copy()
|
||||
|
||||
Reference in New Issue
Block a user