1
- name : PR Code Review with Ollama
1
+ name : PR Code Review with LLM
2
2
3
3
# Trigger the workflow when a pull request is opened or updated
4
4
on :
@@ -14,58 +14,74 @@ jobs:
14
14
env :
15
15
MODEL_NAME : " llama3.2:latest"
16
16
steps :
17
- # Checkout the repository code
17
+ # Step 1: Checkout the repository code
18
18
- name : Checkout Repository
19
19
uses : actions/checkout@v4
20
20
with :
21
21
# Checks out the code from the pull request's head branch.
22
22
ref : ${{ github.event.pull_request.head.ref }}
23
23
fetch-depth : 0
24
-
25
- # Fetches the base branch (e.g., main) from the remote repository
24
+
25
+ # Step 2: Fetch the base branch (e.g., main) from the remote repository
26
26
- name : Fetch Base Branch
27
27
run : |
28
28
git fetch origin ${{ github.base_ref }}
29
29
30
- # - name: Fetch Pull Request Branch
31
- # run: git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-branch
32
-
30
+ # Step 3: Generate the diff between the base branch and the PR branch
33
31
- name : Generate Diff
34
- id : generate-diff
32
+ id : generate-diff
35
33
run : |
36
34
echo "Generating diff between ${{ github.base_ref }} and ${{ github.head_ref }}"
37
- DIFF=$(git diff origin/${{ github.base_ref }}...${{ github.head_ref }})
38
- echo "diff_output=$DIFF" >> $GITHUB_ENV
39
-
40
- # Step 4: Output the diff (optional)
41
- - name : Print Diff
42
- run : |
43
- echo "Diff Output:"
44
- echo "${{ env.diff_output }}"
35
+ git diff origin/${{ github.base_ref }}...${{ github.head_ref }} > changes.diff
45
36
46
- # Step 5: Save the diff to a file (optional)
47
- - name : Save diff to file
48
- run : |
49
- echo "${{ env.diff_output }}" > changes.diff
37
+ # Step 4: Upload the diff file as an artifact (optional)
38
+ - name : Upload Diff as Artifact
39
+ uses : actions/upload-artifact@v3
40
+ with :
41
+ name : pr-diff
42
+ path : changes.diff
50
43
44
+ # Step 5: Install Ollama
51
45
- name : Install Ollama
52
46
run : |
53
47
curl -fsSL https://ollama.com/install.sh | sh
54
48
ollama --version
55
49
50
+ # Step 6: Pull the specified model
56
51
- name : Pull Model
57
52
run : |
58
53
ollama pull ${{ env.MODEL_NAME }} || { echo "Failed to pull model"; exit 1; }
59
54
ollama list
60
55
56
+ # Step 7: Read the diff file and prepare the prompt for Ollama
57
+ - name : Prepare Prompt
58
+ id : prepare-prompt
59
+ run : |
60
+ DIFF=$(cat changes.diff)
61
+ PROMPT=$(echo "Please review the following code changes and provide feedback:\n\n$DIFF\n\nFeedback:" | sed 's/"/\\"/g')
62
+ echo "prompt=$PROMPT" >> $GITHUB_ENV
63
+
64
+ # Step 8: Perform code review using Ollama
61
65
- name : Code Review
62
66
run : |
63
- prompt=$(echo "Please review the following code changes and provide feedback:\n\n${{ env.diff_output }}\n\nFeedback:" | sed 's/"/\\"/g')
64
67
RAW_RESPONSE=$(curl -s -X POST http://localhost:11434/api/generate \
65
68
-d '{
66
69
"model": "'"${{ env.MODEL_NAME }}"'",
67
- "prompt": "'"${prompt}"'",
70
+ "prompt": "'"${{ env. prompt } }"'",
68
71
"temperature": 0.5,
69
72
"stream": false
70
73
}' || { echo "API call failed"; exit 1; })
71
74
echo "RAW RESPONSE:\n$RAW_RESPONSE"
75
+
76
+ # Step 9: Optionally save the response as an artifact
77
+ - name : Save Response as Artifact
78
+ run : |
79
+ echo "$RAW_RESPONSE" > response.json
80
+ if : always()
81
+
82
+ - name : Upload Response as Artifact
83
+ uses : actions/upload-artifact@v3
84
+ with :
85
+ name : ollama-response
86
+ path : response.json
87
+ if : always()
0 commit comments