|
| 1 | +name: PR Review - Kimi |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + pull_request_review_comment: |
| 7 | + types: [created] |
| 8 | + issue_comment: |
| 9 | + types: [created] |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + kimi-review: |
| 21 | + name: Kimi Code Review |
| 22 | + if: | |
| 23 | + github.event_name == 'pull_request' || |
| 24 | + (github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '@kimi')) || |
| 25 | + (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@kimi')) |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v6 |
| 30 | + |
| 31 | + - name: Read review prompt |
| 32 | + id: prompt |
| 33 | + run: | |
| 34 | + PROMPT=$(cat .github/prompts/pr_review.md) |
| 35 | + echo "content<<EOF" >> $GITHUB_OUTPUT |
| 36 | + echo "$PROMPT" >> $GITHUB_OUTPUT |
| 37 | + echo "EOF" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Get PR diff |
| 40 | + id: diff |
| 41 | + env: |
| 42 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + run: | |
| 44 | + gh pr diff ${{ github.event.pull_request.number }} > pr_diff.txt |
| 45 | + # Truncate if too large (Kimi has context limits) |
| 46 | + head -c 100000 pr_diff.txt > pr_diff_truncated.txt |
| 47 | +
|
| 48 | + - name: Kimi Code Review |
| 49 | + id: kimi_review |
| 50 | + env: |
| 51 | + KIMI_API_KEY: ${{ secrets.KIMI_API_KEY }} |
| 52 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 53 | + REVIEW_PROMPT: ${{ steps.prompt.outputs.content }} |
| 54 | + run: | |
| 55 | + if [ -z "$KIMI_API_KEY" ]; then |
| 56 | + echo "Error: KIMI_API_KEY secret is not set" > kimi_review.txt |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | +
|
| 60 | + DIFF_CONTENT=$(cat pr_diff_truncated.txt) |
| 61 | +
|
| 62 | + # Build the request body |
| 63 | + REQUEST_BODY=$(jq -n \ |
| 64 | + --arg diff "$DIFF_CONTENT" \ |
| 65 | + --arg title "$PR_TITLE" \ |
| 66 | + --arg prompt "$REVIEW_PROMPT" \ |
| 67 | + '{ |
| 68 | + "model": "moonshot-v1-128k", |
| 69 | + "messages": [ |
| 70 | + { |
| 71 | + "role": "system", |
| 72 | + "content": $prompt |
| 73 | + }, |
| 74 | + { |
| 75 | + "role": "user", |
| 76 | + "content": ("PR Title: " + $title + "\n\nDiff:\n" + $diff) |
| 77 | + } |
| 78 | + ], |
| 79 | + "temperature": 0.3, |
| 80 | + "max_tokens": 4096 |
| 81 | + }') |
| 82 | +
|
| 83 | + # Try the API call |
| 84 | + HTTP_RESPONSE=$(curl -s -w "\n%{http_code}" https://api.moonshot.ai/v1/chat/completions \ |
| 85 | + -H "Content-Type: application/json" \ |
| 86 | + -H "Authorization: Bearer $KIMI_API_KEY" \ |
| 87 | + -d "$REQUEST_BODY") |
| 88 | +
|
| 89 | + HTTP_CODE=$(echo "$HTTP_RESPONSE" | tail -n1) |
| 90 | + RESPONSE=$(echo "$HTTP_RESPONSE" | sed '$d') |
| 91 | +
|
| 92 | + if [ "$HTTP_CODE" != "200" ]; then |
| 93 | + echo "API Error (HTTP $HTTP_CODE): $RESPONSE" > kimi_review.txt |
| 94 | + else |
| 95 | + # Check for API errors in response |
| 96 | + ERROR=$(echo "$RESPONSE" | jq -r '.error.message // empty') |
| 97 | + if [ -n "$ERROR" ]; then |
| 98 | + echo "API Error: $ERROR" > kimi_review.txt |
| 99 | + else |
| 100 | + REVIEW=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // "Error: Unexpected API response"') |
| 101 | + echo "$REVIEW" > kimi_review.txt |
| 102 | + fi |
| 103 | + fi |
| 104 | +
|
| 105 | + - name: Post review comment |
| 106 | + env: |
| 107 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 108 | + run: | |
| 109 | + echo "<details><summary><h2>Kimi AI Code Review</h2></summary>" > body.md |
| 110 | + cat kimi_review.txt >> body.md |
| 111 | + echo -e "\n---\n*Automated review by Kimi (Moonshot AI)*\n</details>" >> body.md |
| 112 | +
|
| 113 | + gh pr comment ${{ github.event.pull_request.number }} --body-file body.md |
0 commit comments