Skip to content

Feature/refactor code #28

Feature/refactor code

Feature/refactor code #28

Workflow file for this run

name: PR Code Review with LLM
# Trigger the workflow when a pull request is opened or updated
on:
pull_request_target:
branches:
- main
jobs:
code-review:
runs-on: ubuntu-latest
permissions:
issues: write
env:
MODEL_NAME: "llama3.2:latest"
steps:
# Step 1: Checkout the repository code
- name: Checkout Repository
uses: actions/checkout@v4
with:
# Checks out the code from the pull request's head branch.
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
# Step 2: Fetch the base branch (e.g., main) from the remote repository
- name: Fetch Base Branch
run: |
git fetch origin ${{ github.base_ref }}
# Step 3: Generate the diff between the base branch and the PR branch
- name: Generate Diff
id: generate-diff
run: |
echo "Generating diff between ${{ github.base_ref }} and ${{ github.head_ref }}"
git diff --unified=5 origin/${{ github.base_ref }}...${{ github.head_ref }} > changes.diff
# Step 4: Sanitize the diff file
- 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
# Step 5: Upload the diff file as an artifact (optional)
- name: Upload Diff as Artifact
uses: actions/upload-artifact@v4
with:
name: sanitized-pr-diff
path: sanitized_diff.txt
# Step 6: Install Ollama
- name: Install Ollama
run: |
curl -fsSL https://ollama.com/install.sh | sh
ollama --version
# Step 7: Pull the specified model
- name: Pull Model
run: |
ollama pull ${{ env.MODEL_NAME }} || { echo "Failed to pull model"; exit 1; }
ollama list
# Step 8: Read the diff file and prepare the prompt for Ollama
# - name: Prepare Prompt
# run: |
# DIFF=$(cat sanitized_diff.txt)
# PROMPT=$(echo "Please review the following code changes and provide feedback:\n\n$DIFF\n\nFeedback:" | sed 's/"/\\"/g')
# echo "prompt<<EOF" >> $GITHUB_ENV
# echo "$PROMPT" >> $GITHUB_ENV
# echo "EOF" >> $GITHUB_ENV
# shell: /usr/bin/bash -e {0}
- name: Prepare Prompt
run: |
DIFF=$(cat sanitized_diff.txt)
PROMPT=$(echo "Please review the following code changes and provide feedback:\n\n$DIFF\n\nFeedback:" | jq -sR .)
echo "prompt=$PROMPT" >> $GITHUB_ENV
shell: /usr/bin/bash -e {0}
# Step 9: Perform code review using Ollama
# - name: Code Review
# run: |
# RAW_RESPONSE=$(curl -s -X POST http://localhost:11434/api/generate \
# -d '{
# "model": "'"${{ env.MODEL_NAME }}"'",
# "prompt": "'"${{ env.prompt }}"'",
# "temperature": 0.5,
# "stream": false
# }' || { echo "API call failed"; exit 1; })
# echo "RAW RESPONSE:\n$RAW_RESPONSE"
- name: Code Review
run: |
PAYLOAD=$(jq -n \
--arg model "$MODEL_NAME" \
--arg prompt "$prompt" \
'{
model: $model,
prompt: $prompt,
temperature: 0.5,
stream: false
}')
RAW_RESPONSE=$(curl -s -X POST http://localhost:11434/api/generate \
-d "$PAYLOAD" || { echo "API call failed"; exit 1; })
echo "RAW RESPONSE:\n$RAW_RESPONSE"
# Step 9: Optionally save the response as an artifact
# - name: Save Response as Artifact
# run: |
# echo "$RAW_RESPONSE" > response.json
# if: always()