fix: correct Ollama URL, API endpoint, and JSON construction in pr-review workflow

- Fix OLLAMA_URL to point at actual Ollama server (172.0.1.42:11434)
- Fix API path from /v1/chat to /api/chat (Ollama native endpoint)
- Fix response parsing from OpenAI format to Ollama native (.message.content)
- Use jq to safely construct JSON bodies in both Analyze and Post steps
- Add HTTP status code check and response body logging for diagnostics
This commit is contained in:
Shaun Arman 2026-04-12 15:56:13 -05:00
parent d759486b51
commit 5e61d4f550

View File

@ -32,25 +32,33 @@ jobs:
- name: Analyze with Ollama
if: steps.diff.outputs.diff_size > '0'
env:
OLLAMA_URL: http://172.0.0.29:3000/ollama/v1
API_KEY: ${{ secrets.OLLAMA_API_KEY }}
OLLAMA_URL: http://172.0.1.42:11434
run: |
DIFF_CONTENT=$(cat /tmp/pr_diff.txt)
PROMPT="Analyze the following code changes for completion, completeness, correctness, security issues, and best practices. PR Title: ${{ github.event.pull_request.title }}. Diff: $DIFF_CONTENT. Provide review with: 1) Completion check, 2) Completeness check, 3) Bugs/errors, 4) Security issues, 5) Best practices. Then give specific comments with suggested fixes."
PR_TITLE="${{ github.event.pull_request.title }}"
PROMPT="Analyze the following code changes for correctness, security issues, and best practices. PR Title: ${PR_TITLE}\n\nDiff:\n${DIFF_CONTENT}\n\nProvide a review with: 1) Summary, 2) Bugs/errors, 3) Security issues, 4) Best practices. Give specific comments with suggested fixes."
BODY=$(jq -n \
--arg model "qwen3-coder-next:latest" \
--arg content "$PROMPT" \
'{model: $model, messages: [{role: "user", content: $content}], stream: false}')
echo "Calling Ollama API..."
RESPONSE=$(curl -s -X POST "$OLLAMA_URL/chat" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "{\"model\":\"qwen3-coder-next:latest\",\"messages\":[{\"role\":\"user\",\"content\":\"$PROMPT\"}],\"stream\":false}")
echo "Response: $RESPONSE" > /tmp/ollama_response.txt
echo "$RESPONSE" > /tmp/ollama_response.json
echo "Response saved to /tmp/ollama_response.json"
if [ -z "$RESPONSE" ]; then
echo "ERROR: Empty response from Ollama"
HTTP_CODE=$(curl -s -o /tmp/ollama_response.json -w "%{http_code}" \
-X POST "$OLLAMA_URL/api/chat" \
-H "Content-Type: application/json" \
-d "$BODY")
echo "HTTP status: $HTTP_CODE"
echo "Response body:"
cat /tmp/ollama_response.json
if [ "$HTTP_CODE" != "200" ]; then
echo "ERROR: Ollama returned HTTP $HTTP_CODE"
exit 1
fi
REVIEW=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // empty' 2>/dev/null || echo "Failed to parse jq response")
echo "$REVIEW" > /tmp/pr_review.txt
REVIEW=$(jq -r '.message.content // empty' /tmp/ollama_response.json)
if [ -z "$REVIEW" ]; then
echo "WARNING: No review content extracted"
echo "ERROR: No content in Ollama response"
exit 1
fi
echo "$REVIEW" > /tmp/pr_review.txt
- name: Post review comment
if: success()
@ -59,8 +67,14 @@ jobs:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
if [ -f "/tmp/pr_review.txt" ] && [ -s "/tmp/pr_review.txt" ]; then
REVIEW_BODY=$(cat /tmp/pr_review.txt | head -c 65536)
curl -s -X POST "http://172.0.0.29:3000/api/v1/repos/sarman/tftsr-devops_investigation/pulls/$PR_NUMBER/reviews" -H "Authorization: token $TF_TOKEN" -H "Content-Type: application/json" -d "{\"body\": \"🤖 Automated PR Review:\n\n$REVIEW_BODY\n\n---\n*this is an automated review from Ollama*\", \"event\": \"COMMENT\"}"
REVIEW_BODY=$(head -c 65536 /tmp/pr_review.txt)
BODY=$(jq -n \
--arg body "🤖 Automated PR Review:\n\n${REVIEW_BODY}\n\n---\n*this is an automated review from Ollama*" \
'{body: $body, event: "COMMENT"}')
curl -s -X POST "http://172.0.0.29:3000/api/v1/repos/sarman/tftsr-devops_investigation/pulls/$PR_NUMBER/reviews" \
-H "Authorization: token $TF_TOKEN" \
-H "Content-Type: application/json" \
-d "$BODY"
else
echo "No review to post"
fi