fix(diff): write diff to file #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# Properly escape the diff content for JSON | |
DIFF_CONTENT=$(jq -Rs '.' < pr_diff.txt) | |
# Create the JSON payload | |
JSON_PAYLOAD=$(cat << EOF | |
{ | |
"contents": [{ | |
"parts": [{"text": "Summarize the following PR changes: ${DIFF_CONTENT}"}] | |
}] | |
} | |
EOF | |
) | |
# Make the API call to the Google API with the properly formatted JSON | |
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: OpenAI API returned an error" | |
echo "Error Code: $(echo "$RESPONSE" | jq -r '.error.code')" | |
echo "Full Response: $RESPONSE" | |
exit 1 | |
fi | |
# Extract the summary from the response | |
SUMMARY=$(echo "$RESPONSE" | jq -r '.choices[0].message.content') | |
if [ -z "$SUMMARY" ]; then | |
echo "Error: Failed to get summary from OpenAI response" | |
exit 1 | |
fi | |
echo "Summary generated successfully: $SUMMARY" | |
# Store summary in a file | |
echo "$SUMMARY" > summary.txt |