Skip to content

Commit 8408bbb

Browse files
committed
Refactor code review workflow to improve diff generation and API health check; add job summary step
1 parent 6e7dd15 commit 8408bbb

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

.github/workflows/code-review.yml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@ jobs:
1515
pull-requests: write # Allows the workflow to write comments on pull requests
1616
env:
1717
MODEL_NAME: "llama3.2:latest"
18-
steps:
18+
steps:
1919
- name: Checkout Repository
2020
uses: actions/checkout@v4
2121
with:
2222
fetch-depth: 0 # Fetches all history for the entire branch
2323

2424
- name: Fetch all branches
2525
run: git fetch --all
26-
26+
2727
- name: Generate Diff
2828
id: generate-diff
2929
run: |
3030
echo "Generating diff between origin/${{ github.base_ref }} and origin/${{ github.head_ref }}"
3131
git diff --unified=10 origin/${{ github.base_ref }}...origin/${{ github.head_ref }} > changes.diff
32-
3332
# Check if diff is empty
3433
if [ ! -s changes.diff ]; then
3534
echo "No changes detected"
3635
echo "NO_CHANGES=true" >> $GITHUB_ENV
3736
exit 0
3837
fi
39-
4038
# Limit diff size to avoid token limits (first 100 lines)
4139
head -100 changes.diff > limited_changes.diff
4240
mv limited_changes.diff changes.diff
@@ -47,13 +45,13 @@ jobs:
4745
grep -E '^(\+|-)' changes.diff | sed 's/^+/Added: /; s/^-/Removed: /' > sanitized_diff.txt
4846
echo "Sanitized diff content:"
4947
cat sanitized_diff.txt
50-
48+
5149
- name: Upload Diff as Artifact
5250
uses: actions/upload-artifact@v4
5351
with:
5452
name: sanitized-pr-diff
5553
path: sanitized_diff.txt
56-
54+
5755
- name: Install Ollama
5856
run: |
5957
curl -fsSL https://ollama.com/install.sh | sh
@@ -63,11 +61,17 @@ jobs:
6361
run: |
6462
ollama pull ${{ env.MODEL_NAME }} || { echo "Failed to pull model"; exit 1; }
6563
ollama list
66-
6764
# Verify model is available via API
6865
echo "Verifying model availability via API..."
6966
curl -s http://localhost:11434/api/tags | jq '.'
70-
67+
68+
- name: Check Ollama API Health
69+
run: |
70+
if ! curl -sf http://localhost:11434/api/tags; then
71+
echo "Ollama API is not responding."
72+
exit 1
73+
fi
74+
7175
- name: Wait for Ollama to be ready
7276
run: |
7377
for i in {1..10}; do
@@ -82,33 +86,24 @@ jobs:
8286
- name: Prepare Prompt
8387
run: |
8488
DIFF=$(cat sanitized_diff.txt)
85-
8689
# Check if diff is empty
8790
if [ -z "$DIFF" ]; then
8891
echo "No changes detected in the diff"
8992
DIFF="No code changes detected in this pull request."
9093
fi
91-
92-
PROMPT="Please review the following code changes, summarize the changes, and provide feedback:
93-
94-
$DIFF
95-
96-
Feedback:"
97-
94+
PROMPT="Please review the following code changes, summarize the changes, and provide a short feedback:\n\n$DIFF\n\nFeedback:"
9895
echo "PROMPT:"
9996
echo "$PROMPT"
100-
10197
# Export PROMPT so it's available to later steps
10298
echo "PROMPT<<EOF" >> $GITHUB_ENV
10399
echo "$PROMPT" >> $GITHUB_ENV
104100
echo "EOF" >> $GITHUB_ENV
105-
106101
shell: /usr/bin/bash -e {0}
107102

108103
- name: Code Review
109104
run: |
110105
echo "Starting code review with model: $MODEL_NAME"
111-
106+
echo "PROMPT: $PROMPT"
112107
PAYLOAD=$(jq -n \
113108
--arg model "$MODEL_NAME" \
114109
--arg prompt "$PROMPT" \
@@ -118,25 +113,20 @@ jobs:
118113
temperature: 0.5,
119114
stream: false
120115
}')
121-
116+
echo "PAYLOAD: $PAYLOAD"
122117
echo "Sending request to Ollama API..."
123118
RAW_RESPONSE=$(curl -s -X POST http://localhost:11434/api/generate \
124119
-H "Content-Type: application/json" \
125120
-d "$PAYLOAD" || { echo "API call failed"; exit 1; })
126-
127121
echo "RAW RESPONSE:\n$RAW_RESPONSE"
128-
129122
# Try to extract the response, fallback to a default message if not found
130123
REVIEW=$(echo "$RAW_RESPONSE" | jq -r '.response // empty')
131-
132124
if [ -z "$REVIEW" ] || [ "$REVIEW" = "null" ]; then
133125
echo "Error: .response field is missing or empty in the API response."
134126
REVIEW="Model did not return a valid review. RAW_RESPONSE: $RAW_RESPONSE"
135127
fi
136-
137128
echo "Final review content:"
138129
echo "$REVIEW"
139-
140130
# Export REVIEW for the next step
141131
echo "REVIEW<<EOF" >> $GITHUB_ENV
142132
echo "$REVIEW" >> $GITHUB_ENV
@@ -153,4 +143,12 @@ jobs:
153143
repo: context.repo.repo,
154144
issue_number: context.issue.number,
155145
body: `### 🤖 AI Review\n\n${review}`
156-
});
146+
});
147+
148+
- name: Add Job Summary
149+
run: |
150+
echo "### AI Code Review Completed" >> $GITHUB_STEP_SUMMARY
151+
echo "Model: $MODEL_NAME" >> $GITHUB_STEP_SUMMARY
152+
echo "Prompt used:" >> $GITHUB_STEP_SUMMARY
153+
echo "$PROMPT" >> $GITHUB_STEP_SUMMARY
154+
shell: /usr/bin/bash -e {0}

0 commit comments

Comments
 (0)