Skip to content

Pull request trigger #12

Pull request trigger

Pull request trigger #12

Workflow file for this run

name: PR Code Review with LLM
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
code-review:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v34
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Extract changed files content
run: |
echo "" > code_changes.txt
IFS=',' read -r -a files <<< "${{ steps.changed-files.outputs.all_changed_files }}"
for file in "${files[@]}"; do
echo "--- FILE: $file ---" >> code_changes.txt
cat "$file" >> code_changes.txt || echo "[File not found: $file]" >> code_changes.txt
done
- name: Debug input to API
run: |
echo "Input to API:"
cat code_changes.txt
- name: Escape code changes
run: |
escaped_content=$(cat code_changes.txt | jq -Rsa '.')
echo "escaped_content=$escaped_content" >> $GITHUB_ENV
- name: Send to LLM API
id: call-llm
run: |
# Properly escape the content for JSON payload
escaped_question=$(jq -n --arg content "$escaped_content" \
'{"question": "You are an expert pull request reviewer. Search for similar code and analyze the following code changes for inefficiencies, missing documentation, and insecurities. Here is the code: \($content)"}')
# Send the API request
curl -X POST https://solana-chat-60707405365.us-central1.run.app \
-H "Content-Type: application/json" \
-d "$escaped_question" > llm_response.json
- name: Debug API response
run: |
echo "Raw API response:"
cat llm_response.json
- name: Parse API response
id: parse-response
run: |
# Extract the first element of the 'parts' array from the response JSON
response=$(jq -r '.answer.parts[0].text' llm_response.json)
if [ -z "$response" ]; then
echo "Error: Response is empty or jq failed to parse the JSON."
exit 1
fi
echo "response<<EOF" >> $GITHUB_ENV
echo "$response" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Create PR comment
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: "AI Code Review"
message: |
### AI Analysis
```text
${{ env.response }}
```