Skip to content

fix(diff): write diff to file #7

fix(diff): write diff to file

fix(diff): write diff to file #7

Workflow file for this run

name: Comment on PR
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
pull-requests: write
contents: read
jobs:
generate_summary:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Generate AI Summary
id: generate_summary
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
API_KEY_GEMENI: ${{ secrets.API_KEY_GEMENI }}
run: |
if [ -z "$API_KEY_GEMENI" ]; then
echo "Error: API_KEY_GEMENI secret is not set!"
exit 1
fi
echo "Your API_KEY_GEMENI is set, all good!"
# Save diff to a file first, preserving the +/- format
gh pr diff ${{ github.event.pull_request.number }} > pr_diff.txt
# Create a properly escaped JSON payload
prompt="Summarize the following PR changes:"
DIFF_CONTENT=$(cat pr_diff.txt | jq -sR .)
# Use jq to create a properly formatted JSON payload
JSON_PAYLOAD=$(jq -n \
--arg prompt "$prompt" \
--arg diff "$DIFF_CONTENT" \
'{
contents: [{
parts: [{
text: ($prompt + " " + ($diff | fromjson))
}]
}]
}')
# Make the API call to the Google API
RESPONSE=$(curl -s \
-H "Content-Type: application/json" \
-X POST \
-d "$JSON_PAYLOAD" \
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY_GEMENI}")
# Check if the response contains an error
if echo "$RESPONSE" | jq -e '.error' > /dev/null; then
echo "Error from Gemini API:"
echo "$RESPONSE" | jq '.error'
exit 1
fi
# Extract summary from candidates (Gemini API response format)
SUMMARY=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text')
if [ -z "$SUMMARY" ]; then
echo "Error: Failed to get summary from Gemini response"
exit 1
fi
echo "Summary generated successfully: $SUMMARY"
# Store summary in a file
echo "$SUMMARY" > summary.txt
- name: Post Summary as Comment
run: |
echo "Commenting on the PR..."
gh pr comment ${{ github.event.pull_request.number }} --body "$(cat summary.txt)" || {
echo "Failed to post comment"
exit 1
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ github.token }}