Update greeting message in Get method of InfoController for clarity #56
This file contains hidden or 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: PR Code Review with AI | |
# Trigger the workflow when a pull request is opened or updated | |
on: | |
pull_request_target: | |
branches: | |
- main | |
jobs: | |
code-review: | |
name: Review Changes with AI | |
runs-on: ubuntu-latest | |
permissions: | |
issues: write # Allows the workflow to write comments on issues and pull requests | |
pull-requests: write # Allows the workflow to write comments on pull requests | |
env: | |
MODEL_NAME: "llama3.2:latest" | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Fetches all history for the entire branch | |
- name: Fetch all branches | |
run: git fetch --all | |
- name: Generate Diff | |
id: generate-diff | |
run: | | |
echo "Generating diff between origin/${{ github.base_ref }} and origin/${{ github.head_ref }}" | |
git diff --unified=10 origin/${{ github.base_ref }}...origin/${{ github.head_ref }} > changes.diff | |
# Check if diff is empty | |
if [ ! -s changes.diff ]; then | |
echo "No changes detected" | |
echo "NO_CHANGES=true" >> $GITHUB_ENV | |
exit 0 | |
fi | |
# Limit diff size to avoid token limits (first 100 lines) | |
head -100 changes.diff > limited_changes.diff | |
mv limited_changes.diff changes.diff | |
- name: Sanitize Diff | |
run: | | |
# Remove metadata lines and retain only actual code changes (+/-) | |
grep -E '^(\+|-)' changes.diff | sed 's/^+/Added: /; s/^-/Removed: /' > sanitized_diff.txt | |
echo "Sanitized diff content:" | |
cat sanitized_diff.txt | |
- name: Upload Diff as Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: sanitized-pr-diff | |
path: sanitized_diff.txt | |
- name: Install Ollama | |
run: | | |
curl -fsSL https://ollama.com/install.sh | sh | |
ollama --version | |
- name: Pull Model | |
run: | | |
ollama pull ${{ env.MODEL_NAME }} || { echo "Failed to pull model"; exit 1; } | |
ollama list | |
# Verify model is available via API | |
echo "Verifying model availability via API..." | |
curl -s http://localhost:11434/api/tags | jq '.' | |
# - name: Check Ollama API Health | |
# run: | | |
# if ! curl -sf http://localhost:11434/api/tags; then | |
# echo "Ollama API is not responding." | |
# exit 1 | |
# fi | |
- name: Wait for Ollama to be ready | |
run: | | |
for i in {1..10}; do | |
if curl -s http://localhost:11434/api/tags | jq '.'; then | |
echo "Ollama is ready." | |
break | |
fi | |
echo "Waiting for Ollama to be ready..." | |
sleep 3 | |
done | |
- name: Prepare Prompt | |
run: | | |
DIFF=$(cat sanitized_diff.txt) | |
# Check if diff is empty | |
if [ -z "$DIFF" ]; then | |
echo "No changes detected in the diff" | |
DIFF="No code changes detected in this pull request." | |
fi | |
PROMPT="You are an experienced software engineer reviewing a pull request. Carefully review the following code changes for correctness, clarity, maintainability, and potential issues. Summarize what was changed, point out any problems or improvements, and provide constructive, actionable feedback as a code reviewer would in a real PR review. | |
$DIFF | |
PR Review:" | |
echo "PROMPT:" | |
echo "$PROMPT" | |
# Export PROMPT so it's available to later steps | |
echo "PROMPT<<EOF" >> $GITHUB_ENV | |
echo "$PROMPT" >> $GITHUB_ENV | |
echo "EOF" >> $GITHUB_ENV | |
shell: /usr/bin/bash -e {0} | |
- name: Code Review | |
run: | | |
echo "Starting code review with model: $MODEL_NAME" | |
echo "PROMPT: $PROMPT" | |
PAYLOAD=$(jq -n \ | |
--arg model "$MODEL_NAME" \ | |
--arg prompt "$PROMPT" \ | |
'{ | |
model: $model, | |
prompt: $prompt, | |
temperature: 0.5, | |
stream: false | |
}') | |
echo "PAYLOAD: $PAYLOAD" | |
echo "Sending request to Ollama API..." | |
RAW_RESPONSE=$(curl -s -X POST http://localhost:11434/api/generate \ | |
-H "Content-Type: application/json" \ | |
-d "$PAYLOAD" || { echo "API call failed"; exit 1; }) | |
echo "RAW RESPONSE:\n$RAW_RESPONSE" | |
# Try to extract the response, fallback to a default message if not found | |
REVIEW=$(echo "$RAW_RESPONSE" | jq -r '.response // empty') | |
if [ -z "$REVIEW" ] || [ "$REVIEW" = "null" ]; then | |
echo "Error: .response field is missing or empty in the API response." | |
REVIEW="Model did not return a valid review. RAW_RESPONSE: $RAW_RESPONSE" | |
fi | |
echo "Final review content:" | |
echo "$REVIEW" | |
# Export REVIEW for the next step | |
echo "REVIEW<<EOF" >> $GITHUB_ENV | |
echo "$REVIEW" >> $GITHUB_ENV | |
echo "EOF" >> $GITHUB_ENV | |
- name: Post Review Comment | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const review = process.env.REVIEW || 'No review generated due to an error.'; | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
body: `### 🤖 AI Review\n\n${review}` | |
}); | |
- name: Add Job Summary | |
run: | | |
echo "### AI Code Review Completed" >> $GITHUB_STEP_SUMMARY | |
echo "Model: $MODEL_NAME" >> $GITHUB_STEP_SUMMARY | |
echo "Prompt used:" >> $GITHUB_STEP_SUMMARY | |
echo "$PROMPT" >> $GITHUB_STEP_SUMMARY | |
shell: /usr/bin/bash -e {0} |