Skip to content

feat: AI Code Reviewer v2 - Severity-based Actions & Project Context#7

Closed
hapo-nghialuu wants to merge 4 commits intomainfrom
feat/ai-reviewer-v2
Closed

feat: AI Code Reviewer v2 - Severity-based Actions & Project Context#7
hapo-nghialuu wants to merge 4 commits intomainfrom
feat/ai-reviewer-v2

Conversation

@hapo-nghialuu
Copy link
Copy Markdown
Member

AI Code Reviewer v2

New Features

  • Severity Classification: Issues are now classified as critical, high, medium, low
  • Smart Actions: critical/high → Request Changes, medium/low → Comment only
  • Project Context: Injects README.md and ARCHITECTURE.md into AI prompt
  • Improved Prompt: Much stricter to reduce false positives

Testing

This PR will trigger the AI reviewer on itself. Expect to see the new severity badges in any comments.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Một số góp ý để cải thiện code.

Đây là AI review tự động. Vui lòng verify trước khi áp dụng.

Comment thread scripts/ai_reviewer.py
@@ -0,0 +1,461 @@
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
# Configure Logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Constants
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
BLOCKING_SEVERITIES = ['critical', 'high']


def should_review(filename):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
context.append(f"### {filepath}\n{text}")
except Exception:
# File doesn't exist, skip silently
pass
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
return files
except Exception as e:
logging.warning(f"Could not fetch file list: {e}")
return []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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ớ.

Comment thread scripts/ai_reviewer.py
files_data.append(file_info)

return pr, files_data

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM

Hàm resolve_thread nên kiểm tra response từ GraphQL để đảm bảo thread được resolve thành công.

Comment thread scripts/ai_reviewer.py
"""Resolve all unresolved bot review threads on the PR."""
query = """
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
pr_data = data.get('data', {}).get('repository', {}).get('pullRequest')
if not pr_data:
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
)
except Exception as e:
logging.error(f"Failed to init OpenAI client: {e}")
return []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
"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"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Một số góp ý để cải thiện code.

Đây là AI review tự động. Vui lòng verify trước khi áp dụng.

Comment thread scripts/ai_reviewer.py
@@ -0,0 +1,475 @@
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py

import os
import json
import requests
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 LOW

Biến IGNORED_EXTENSIONSIGNORED_DIRS nên được định nghĩa là hằng số với tên viết hoa hoàn toàn.

Comment thread scripts/ai_reviewer.py
GITHUB_GRAPHQL_URL = "https://api.github.com/graphql"

# Severity levels that trigger Request Changes
BLOCKING_SEVERITIES = ['critical', 'high']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
if any(part in filename.split('/') for part in IGNORED_DIRS):
return False
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
files = []
while contents:
item = contents.pop(0)
if item.type == "dir" and item.name not in IGNORED_DIRS:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
if content and hasattr(content, 'decoded_content'):
full_content = content.decoded_content.decode('utf-8')
# Limit full content size
if len(full_content) <= 5000:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
reviewThreads(last: 100) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
author = comments[0].get('author')
if author and author.get('login') == 'github-actions[bot]':
bot_threads.append(thread.get('id'))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
## PROJECT FILES
{', '.join(file_list[:50])}

## SEVERITY LEVELS (QUAN TRỌNG - PHẢI TRẢ VỀ)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM

Hàm submit_review nên kiểm tra review_comments trước khi gọi pr.create_review để tránh lỗi.

Comment thread scripts/ai_reviewer.py Outdated
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Góp ý cải thiện code.

Comment thread scripts/ai_reviewer.py
from github import Github, GithubException
from openai import OpenAI, AuthenticationError, RateLimitError

# Configure Logging
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
if any(part in filename.split('/') for part in IGNORED_DIRS):
return False
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py

return "\n\n".join(context) if context else "No project documentation found."


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
for file in pr.get_files():
if not should_review(file.filename):
continue
if file.status == 'removed':
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
"""
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
payload = {"query": query, "variables": {"owner": repo_owner, "repo": repo_name, "prNumber": pr_number}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
mutation ResolveThread($threadId: ID!) {
resolveReviewThread(input: {threadId: $threadId}) {
thread { isResolved }
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py

if new_file == old_file and abs(new_line - old_line) <= 3:
# MATCH FOUND!
matched_thread_ids.add(old_th['id'])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
- **medium**: Performance issues, bad practices, potential bugs
- **low**: Minor suggestions, style (chỉ khi rất cần thiết)

## RULES
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py
# 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"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread scripts/ai_reviewer.py

if not files_data:
logging.info("No files to review.")
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM

Script nên có cơ chế logging chi tiết hơn để dễ dàng debug khi có lỗi xảy ra.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant