Skip to content

Commit 67394ff

Browse files
authored
Utilize Groq to comment MR suggestions (#1)
* Add two-sum example * Allow GH action to post review as comment
1 parent 4e0bc87 commit 67394ff

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

.github/workflows/code-review.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ on:
77
jobs:
88
review:
99
runs-on: ubuntu-latest
10+
11+
permissions:
12+
contents: read
13+
pull-requests: write
14+
1015
steps:
1116
- name: Checkout code
1217
uses: actions/checkout@v3
@@ -20,6 +25,12 @@ jobs:
2025
run: |
2126
pip install -r requirements.txt
2227
23-
- name: Run code review
28+
- name: Run LLM code review
29+
env:
30+
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
PYTHONPATH: ${{ github.workspace }}
33+
GITHUB_REPOSITORY: ${{ github.repository }}
34+
GITHUB_REF: ${{ github.ref }}
2435
run: |
25-
python reviewbot/main.py
36+
python3 -m reviewbot.main
1.24 KB
Binary file not shown.
1.01 KB
Binary file not shown.

reviewbot/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import requests
3-
from reviewbot.utils import get_diff
3+
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
44

55
def review_with_groq(diff):
66
api_key = os.environ.get("GROQ_API_KEY")
@@ -9,21 +9,24 @@ def review_with_groq(diff):
99
"Content-Type": "application/json",
1010
}
1111
payload = {
12-
"model": "mixtral-8x7b-32768",
12+
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
1313
"messages": [
1414
{"role": "system", "content": "You are a senior software engineer doing code reviews."},
15-
{"role": "user", "content": f"Please review this code diff:\n\n{diff}"}
15+
{"role": "user", "content": f"Please review this code diff, list improvements in bullet points:\n\n{diff}"}
1616
],
1717
"temperature": 0.3,
1818
}
1919

2020
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
21+
22+
print("Raw Groq response:", response.status_code, response.text)
23+
response.raise_for_status()
2124
return response.json()["choices"][0]["message"]["content"]
2225

2326
def main():
2427
diff = get_diff()
2528
review = review_with_groq(diff)
26-
print("::notice file=test_module/hello.py,line=1::" + review.replace("\n", " "))
29+
post_pr_comment(review)
2730

2831
if __name__ == "__main__":
2932
main()

reviewbot/utils.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
1-
# reviewbot/utils.py
1+
import os
2+
import requests
3+
import json
4+
5+
def get_pr_number():
6+
event_path = os.getenv("GITHUB_EVENT_PATH")
7+
if not event_path:
8+
raise Exception("GITHUB_EVENT_PATH not set")
9+
10+
with open(event_path, 'r') as f:
11+
event = json.load(f)
12+
13+
return event["pull_request"]["number"]
214

315
def get_diff():
4-
# In real setup, parse `git diff` or PR files
5-
return "def hello(name):\n print('Hello ' + name)"
16+
repo = os.environ.get("GITHUB_REPOSITORY") # e.g. "owner/repo"
17+
pr_number = get_pr_number()
18+
19+
token = os.environ.get("GITHUB_TOKEN")
20+
if not token:
21+
raise Exception("Missing GITHUB_TOKEN environment variable.")
22+
23+
headers = {
24+
"Authorization": f"Bearer {token}",
25+
"Accept": "application/vnd.github.v3+json",
26+
}
27+
28+
url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}/files"
29+
30+
diffs = []
31+
page = 1
32+
while True:
33+
response = requests.get(url, headers=headers, params={"page": page, "per_page": 100})
34+
response.raise_for_status()
35+
files = response.json()
36+
if not files:
37+
break
38+
39+
for f in files:
40+
patch = f.get("patch")
41+
if patch:
42+
diffs.append(f"File: {f['filename']}\n{patch}")
43+
44+
page += 1
45+
46+
return "\n\n".join(diffs)
47+
48+
def post_pr_comment(body: str):
49+
repo = os.getenv("GITHUB_REPOSITORY")
50+
token = os.getenv("GITHUB_TOKEN")
51+
pr_number = get_pr_number()
52+
53+
url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
54+
headers = {
55+
"Authorization": f"Bearer {token}",
56+
"Accept": "application/vnd.github+json"
57+
}
58+
payload = {
59+
"body": body
60+
}
661

7-
def fake_llm_review(diff):
8-
if "print" in diff:
9-
return "Consider using logging instead of print for better production code quality."
10-
return "No issues found."
62+
response = requests.post(url, headers=headers, json=payload)
63+
print("GitHub API response:", response.status_code, response.text)
64+
response.raise_for_status()

test_module/hello.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# test_module/hello.py
1+
# Hello.py: A sample python script used for testing reviews
22

3-
def hello(name):
4-
print("Hello " + name)
5-
6-
hello("World")
3+
def helloWorld():
4+
print("Hello World")
5+
6+
helloWorld()

0 commit comments

Comments
 (0)