Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@ jobs:
pull-requests: write # Allows the workflow to write comments on pull requests
env:
MODEL_NAME: "llama3.2:latest"
steps:
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
Expand All @@ -47,13 +45,13 @@ jobs:
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
Expand All @@ -63,11 +61,17 @@ jobs:
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
Expand All @@ -82,33 +86,24 @@ jobs:
- 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="Please review the following code changes, summarize the changes, and provide feedback:

$DIFF

Feedback:"

PROMPT="Please review the following code changes, summarize the changes, and provide a short feedback:\n\n$DIFF\n\nFeedback:"
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" \
Expand All @@ -118,25 +113,20 @@ jobs:
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
Expand All @@ -153,4 +143,12 @@ jobs:
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}
14 changes: 14 additions & 0 deletions src/MyWebApi/MyWebApi/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public InfoController(ILogger<InfoController> logger)
_logger.LogInformation("InfoController instantiated.");
}

/// <summary>
/// Gets a simple message.
/// This is a sample message to demonstrate the API functionality.
/// </summary>
/// <returns>
/// A string message indicating the API is working.
/// </returns>
[HttpGet(Name = "Get")]
public IActionResult Get()
{
Expand All @@ -23,6 +30,13 @@ public IActionResult Get()
return Ok(message);
}

/// <summary>
/// Gets a hello message.
/// This is a sample message to demonstrate the API functionality.
/// </summary>
/// <returns>
/// A string message indicating the API is working.
/// </returns>
[HttpGet("hello")]
public IActionResult Hello()
{
Expand Down