feat: AI Code Reviewer v2 - Severity-based Actions & Project Context#7
feat: AI Code Reviewer v2 - Severity-based Actions & Project Context#7hapo-nghialuu wants to merge 4 commits intomainfrom
Conversation
| @@ -0,0 +1,461 @@ | |||
| """ | |||
There was a problem hiding this comment.
🟡 MEDIUM
Thiếu docstring cho module, nên thêm mô tả chi tiết về chức năng và cách sử dụng.
| # Configure Logging | ||
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
|
||
| # Constants |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm should_review nên sử dụng os.path để xử lý đường dẫn file một cách chính xác hơn.
| BLOCKING_SEVERITIES = ['critical', 'high'] | ||
|
|
||
|
|
||
| def should_review(filename): |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_project_context nên giới hạn kích thước tổng cộng của context để tránh vượt quá giới hạn token của AI.
| context.append(f"### {filepath}\n{text}") | ||
| except Exception: | ||
| # File doesn't exist, skip silently | ||
| pass |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_file_list nên sử dụng đệ quy hoặc os.walk để duyệt thư mục một cách hiệu quả hơn.
| return files | ||
| except Exception as e: | ||
| logging.warning(f"Could not fetch file list: {e}") | ||
| return [] |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_pr_diff nên xử lý trường hợp file quá lớn để tránh vượt quá giới hạn bộ nhớ.
| files_data.append(file_info) | ||
|
|
||
| return pr, files_data | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm resolve_thread nên kiểm tra response từ GraphQL để đảm bảo thread được resolve thành công.
| """Resolve all unresolved bot review threads on the PR.""" | ||
| query = """ | ||
| query($owner: String!, $repo: String!, $prNumber: Int!) { | ||
| repository(owner: $owner, name: $repo) { |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm resolve_existing_comments nên giới hạn số lượng threads được resolve để tránh vượt quá giới hạn API.
| pr_data = data.get('data', {}).get('repository', {}).get('pullRequest') | ||
| if not pr_data: | ||
| return | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm analyze_code_with_openrouter nên kiểm tra kích thước của files_data trước khi gửi đến AI để tránh vượt quá giới hạn token.
| ) | ||
| except Exception as e: | ||
| logging.error(f"Failed to init OpenAI client: {e}") | ||
| return [] |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm submit_review nên kiểm tra độ dài của review_comments để tránh vượt quá giới hạn API của GitHub.
| "filename": "path/to/file.py", | ||
| "line_number": 42, | ||
| "severity": "critical|high|medium|low", | ||
| "comment": "Mô tả vấn đề ngắn gọn bằng tiếng Việt" |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm main nên thêm xử lý ngoại lệ cho các trường hợp không mong muốn khi đọc file event.
| @@ -0,0 +1,475 @@ | |||
| """ | |||
There was a problem hiding this comment.
🟡 MEDIUM
Thiếu docstring cho module, nên thêm mô tả chi tiết về chức năng và cách sử dụng.
|
|
||
| import os | ||
| import json | ||
| import requests |
There was a problem hiding this comment.
🟢 LOW
Biến IGNORED_EXTENSIONS và IGNORED_DIRS nên được định nghĩa là hằng số với tên viết hoa hoàn toàn.
| GITHUB_GRAPHQL_URL = "https://api.github.com/graphql" | ||
|
|
||
| # Severity levels that trigger Request Changes | ||
| BLOCKING_SEVERITIES = ['critical', 'high'] |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm should_review có thể được tối ưu bằng cách sử dụng os.path để xử lý đường dẫn.
| if any(part in filename.split('/') for part in IGNORED_DIRS): | ||
| return False | ||
| return True | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_project_context nên xử lý ngoại lệ cụ thể hơn thay vì sử dụng Exception chung chung.
| files = [] | ||
| while contents: | ||
| item = contents.pop(0) | ||
| if item.type == "dir" and item.name not in IGNORED_DIRS: |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_file_list có thể gây ra lỗi nếu repository quá lớn, nên thêm giới hạn số lượng file.
| if content and hasattr(content, 'decoded_content'): | ||
| full_content = content.decoded_content.decode('utf-8') | ||
| # Limit full content size | ||
| if len(full_content) <= 5000: |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_pr_diff nên kiểm tra file.status cụ thể hơn để tránh bỏ sót các trường hợp cần review.
| query($owner: String!, $repo: String!, $prNumber: Int!) { | ||
| repository(owner: $owner, name: $repo) { | ||
| pullRequest(number: $prNumber) { | ||
| reviewThreads(last: 100) { |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm resolve_thread nên kiểm tra phản hồi từ GraphQL để đảm bảo thread được resolve thành công.
| author = comments[0].get('author') | ||
| if author and author.get('login') == 'github-actions[bot]': | ||
| bot_threads.append(thread.get('id')) | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm analyze_code_with_openrouter nên xử lý ngoại lệ cụ thể hơn và ghi log chi tiết hơn.
| ## PROJECT FILES | ||
| {', '.join(file_list[:50])} | ||
|
|
||
| ## SEVERITY LEVELS (QUAN TRỌNG - PHẢI TRẢ VỀ) |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm submit_review nên kiểm tra review_comments trước khi gọi pr.create_review để tránh lỗi.
| from github import Github, GithubException | ||
| from openai import OpenAI, AuthenticationError, RateLimitError | ||
|
|
||
| # Configure Logging |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm should_review nên sử dụng os.path để xử lý đường dẫn file một cách nhất quán và tránh lỗi trên các hệ điều hành khác nhau.
| if any(part in filename.split('/') for part in IGNORED_DIRS): | ||
| return False | ||
| return True | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_project_context nên giới hạn độ dài của nội dung file một cách nhất quán và tránh cắt cụt nội dung một cách tùy tiện.
|
|
||
| return "\n\n".join(context) if context else "No project documentation found." | ||
|
|
||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_file_list có thể gây ra lỗi nếu số lượng file vượt quá giới hạn cho phép của API GitHub.
| for file in pr.get_files(): | ||
| if not should_review(file.filename): | ||
| continue | ||
| if file.status == 'removed': |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_pr_diff nên xử lý trường hợp file bị xóa một cách rõ ràng hơn để tránh lỗi không mong muốn.
| """ | ||
| headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"} | ||
| payload = {"query": query, "variables": {"owner": repo_owner, "repo": repo_name, "prNumber": pr_number}} | ||
|
|
There was a problem hiding this comment.
🟡 MEDIUM
Hàm get_existing_bot_threads nên kiểm tra và xử lý lỗi từ GraphQL một cách chi tiết hơn.
| mutation ResolveThread($threadId: ID!) { | ||
| resolveReviewThread(input: {threadId: $threadId}) { | ||
| thread { isResolved } | ||
| } |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm manage_review_threads nên cải thiện logic so khớp dòng để tránh trường hợp không khớp do thay đổi dòng.
|
|
||
| if new_file == old_file and abs(new_line - old_line) <= 3: | ||
| # MATCH FOUND! | ||
| matched_thread_ids.add(old_th['id']) |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm analyze_code_with_openrouter nên kiểm tra và xử lý lỗi từ API OpenRouter một cách chi tiết hơn.
| - **medium**: Performance issues, bad practices, potential bugs | ||
| - **low**: Minor suggestions, style (chỉ khi rất cần thiết) | ||
|
|
||
| ## RULES |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm submit_review nên kiểm tra và xử lý lỗi từ GitHub API một cách chi tiết hơn.
| # Check for blocking issues | ||
| has_blocking = any(c.get('severity', '').lower() in BLOCKING_SEVERITIES for c in comments) | ||
| summary = "🚨 **Phát hiện vấn đề nghiêm trọng.**" if has_blocking else "📝 **Góp ý cải thiện code.**" | ||
| event = "REQUEST_CHANGES" if has_blocking else "COMMENT" |
There was a problem hiding this comment.
🟡 MEDIUM
Hàm main nên kiểm tra và xử lý lỗi từ các hàm con một cách chi tiết hơn.
|
|
||
| if not files_data: | ||
| logging.info("No files to review.") | ||
| return |
There was a problem hiding this comment.
🟡 MEDIUM
Script nên có cơ chế logging chi tiết hơn để dễ dàng debug khi có lỗi xảy ra.
AI Code Reviewer v2
New Features
critical,high,medium,lowcritical/high→ Request Changes,medium/low→ Comment onlyTesting
This PR will trigger the AI reviewer on itself. Expect to see the new severity badges in any comments.