Skip to content

PR Review - Kimi

PR Review - Kimi #11

name: PR Review - Kimi
on:
pull_request:
types: [opened, synchronize, reopened]
pull_request_review_comment:
types: [created]
issue_comment:
types: [created]
permissions:
contents: read
pull-requests: write
issues: write
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
kimi-review:
name: Kimi Code Review
if: |
github.event_name == 'pull_request' ||
(github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '@kimi')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@kimi'))
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Read review prompt
id: prompt
run: |
PROMPT=$(cat .github/prompts/pr_review.md)
echo "content<<EOF" >> $GITHUB_OUTPUT
echo "$PROMPT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Get PR diff
id: diff
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
run: |
gh pr diff $PR_NUMBER > pr_diff.txt
# Truncate if too large (Kimi has context limits)
head -c 100000 pr_diff.txt > pr_diff_truncated.txt
- name: Get PR title
id: pr_info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
run: |
TITLE=$(gh pr view $PR_NUMBER --json title -q '.title')
echo "title=$TITLE" >> $GITHUB_OUTPUT
- name: Kimi Code Review
id: kimi_review
env:
KIMI_API_KEY: ${{ secrets.KIMI_API_KEY }}
PR_TITLE: ${{ steps.pr_info.outputs.title }}
REVIEW_PROMPT: ${{ steps.prompt.outputs.content }}
run: |
if [ -z "$KIMI_API_KEY" ]; then
echo "Error: KIMI_API_KEY secret is not set" > kimi_review.txt
exit 0
fi
DIFF_CONTENT=$(cat pr_diff_truncated.txt | jq -Rs .)
# Build the request body
REQUEST_BODY=$(jq -n \
--arg diff "$DIFF_CONTENT" \
--arg title "$PR_TITLE" \
--arg prompt "$REVIEW_PROMPT" \
'{
"model": "moonshot-v1-128k",
"messages": [
{
"role": "system",
"content": $prompt
},
{
"role": "user",
"content": ("PR Title: " + $title + "\n\nDiff:\n" + $diff)
}
],
"temperature": 0.3,
"max_tokens": 4096
}')
# Try the API call
HTTP_RESPONSE=$(curl -s -w "\n%{http_code}" https://api.moonshot.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $KIMI_API_KEY" \
-d "$REQUEST_BODY")
HTTP_CODE=$(echo "$HTTP_RESPONSE" | tail -n1)
RESPONSE=$(echo "$HTTP_RESPONSE" | sed '$d')
if [ "$HTTP_CODE" != "200" ]; then
echo "API Error (HTTP $HTTP_CODE): $RESPONSE" > kimi_review.txt
else
# Check for API errors in response
ERROR=$(echo "$RESPONSE" | jq -r '.error.message // empty')
if [ -n "$ERROR" ]; then
echo "API Error: $ERROR" > kimi_review.txt
else
REVIEW=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // "Error: Unexpected API response"')
echo "$REVIEW" > kimi_review.txt
fi
fi
- name: Post review comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
run: |
echo "<details><summary><h2>Kimi AI Code Review</h2></summary>" > body.md
cat kimi_review.txt >> body.md
echo -e "\n---\n*Automated review by Kimi (Moonshot AI)*\n</details>" >> body.md
gh pr comment ${{ github.event.pull_request.number }} --body-file body.md