Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ai-code-reviewer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

# Temporarily disable errexit to capture errors properly
set +e
AI_RESPONSE=$(cat diff.txt | bash ai-reviewer.sh 2>&1)
AI_RESPONSE=$(cat diff.txt | bash ai-reviewer.sh)
EXIT_CODE=$?
set -e

Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:

# Extract fields from JSON
REVIEW=$(echo "$AI_RESPONSE" | jq -r '.review // "No review provided"')
DECISION=$(echo "$AI_RESPONSE" | jq -r '.fail_pass_workflow // "uncertain"')
DECISION=$(echo "$AI_RESPONSE" | jq -r '.fail_pass_workflow // "uncertain"' | tr -d '\n\r' | xargs)
LABELS=$(echo "$AI_RESPONSE" | jq -r '.labels_added[]? // empty')

# Post the review as PR comment
Expand All @@ -130,7 +130,7 @@ jobs:
echo "AI decision: $DECISION"

# Store the decision for later use
echo "DECISION=$DECISION" >> $GITHUB_ENV
echo "DECISION=$DECISION" >> "$GITHUB_ENV"

# Remove the ai_code_review label to make re-triggering easier
echo "Removing ai_code_review label to allow easy re-triggering"
Expand Down
22 changes: 15 additions & 7 deletions ai-reviewer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ fi
AI_MODEL="${AI_MODEL:-moonshotai/kimi-k2-thinking}"
AI_TEMPERATURE="${AI_TEMPERATURE:-0.1}"
AI_MAX_TOKENS="${AI_MAX_TOKENS:-64000}"
MAX_DIFF_SIZE="${MAX_DIFF_SIZE:-800000}" # 800KB default limit (~200K tokens, matching model context size)
MAX_DIFF_SIZE="${MAX_DIFF_SIZE:-5000000}" # 5MB default limit (allows large PRs while preventing excessive API usage)
EXCLUDE_FILE_PATTERNS="${EXCLUDE_FILE_PATTERNS:-*.lock,*.min.js,*.min.css,package-lock.json,yarn.lock}"

# Context inclusion options (set to 'false' to disable, reduces token usage)
INCLUDE_PREVIOUS_REVIEWS="${INCLUDE_PREVIOUS_REVIEWS:-true}"
INCLUDE_HUMAN_COMMENTS="${INCLUDE_HUMAN_COMMENTS:-true}"
INCLUDE_CHECK_RUNS="${INCLUDE_CHECK_RUNS:-true}"
INCLUDE_LABELS="${INCLUDE_LABELS:-true}"
INCLUDE_PR_DESCRIPTION="${INCLUDE_PR_DESCRIPTION:-true}"
INCLUDE_COMMIT_MESSAGES="${INCLUDE_COMMIT_MESSAGES:-true}"

# Read diff content from stdin
DIFF_CONTENT=$(cat)

Expand Down Expand Up @@ -64,23 +72,23 @@ fi

# Fetch previous AI review (only the most recent one) for context
PREVIOUS_REVIEWS=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_PREVIOUS_REVIEWS" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
# Fetch only the most recent AI review comment
PREVIOUS_REVIEWS=$(gh api "repos/$REPO_FULL_NAME/issues/$PR_NUMBER/comments" \
--jq '[.[] | select(.body | startswith("## AI Code Review"))] | last | if . then "### Previous AI Review (" + .created_at + "):\n" + .body + "\n---\n" else "" end' 2>/dev/null | head -c 10000 || echo "")
fi

# Fetch human comments for context
HUMAN_COMMENTS=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_HUMAN_COMMENTS" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
# Fetch comments from humans (not the bot)
HUMAN_COMMENTS=$(gh api "repos/$REPO_FULL_NAME/issues/$PR_NUMBER/comments" \
--jq '[.[] | select(.body | startswith("## AI Code Review") | not)] | map("**" + .user.login + "** (" + .created_at + "):\n" + .body) | join("\n\n---\n\n")' 2>/dev/null | head -c 20000 || echo "")
fi

# Fetch GitHub Actions check runs status (if PR_NUMBER and REPO_FULL_NAME are set)
CHECK_RUNS_STATUS=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_CHECK_RUNS" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
# Get the head SHA of the PR
HEAD_SHA=$(gh api "repos/$REPO_FULL_NAME/pulls/$PR_NUMBER" --jq '.head.sha' 2>/dev/null || echo "")

Expand All @@ -93,7 +101,7 @@ fi

# Fetch available repository labels (if PR_NUMBER and REPO_FULL_NAME are set)
AVAILABLE_LABELS=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_LABELS" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
# Fetch all labels from the repository
if [ "$DEBUG_MODE" = "true" ]; then
echo "🔍 Fetching available labels from repository..." >&2
Expand All @@ -113,7 +121,7 @@ fi

# Fetch PR title and description
PR_DESCRIPTION=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_PR_DESCRIPTION" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$DEBUG_MODE" = "true" ]; then
echo "🔍 Fetching PR title and description..." >&2
fi
Expand All @@ -127,7 +135,7 @@ fi

# Fetch commit messages (limit to 15 most recent, exclude merges)
COMMIT_MESSAGES=""
if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$INCLUDE_COMMIT_MESSAGES" = "true" ] && [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then
if [ "$DEBUG_MODE" = "true" ]; then
echo "🔍 Fetching commit messages..." >&2
fi
Expand Down