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
15 changes: 13 additions & 2 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ on:
jobs:
review:
runs-on: ubuntu-latest

permissions:
contents: read
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -20,6 +25,12 @@ jobs:
run: |
pip install -r requirements.txt

- name: Run code review
- name: Run LLM code review
env:
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PYTHONPATH: ${{ github.workspace }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_REF: ${{ github.ref }}
run: |
python reviewbot/main.py
python3 -m reviewbot.main
Binary file added reviewbot/__pycache__/main.cpython-310.pyc
Binary file not shown.
Binary file added reviewbot/__pycache__/utils.cpython-310.pyc
Binary file not shown.
11 changes: 7 additions & 4 deletions reviewbot/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import requests
from reviewbot.utils import get_diff
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number

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

response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)

print("Raw Groq response:", response.status_code, response.text)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]

def main():
diff = get_diff()
review = review_with_groq(diff)
print("::notice file=test_module/hello.py,line=1::" + review.replace("\n", " "))
post_pr_comment(review)

if __name__ == "__main__":
main()
68 changes: 61 additions & 7 deletions reviewbot/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,64 @@
# reviewbot/utils.py
import os
import requests
import json

def get_pr_number():
event_path = os.getenv("GITHUB_EVENT_PATH")
if not event_path:
raise Exception("GITHUB_EVENT_PATH not set")

with open(event_path, 'r') as f:
event = json.load(f)

return event["pull_request"]["number"]

def get_diff():
# In real setup, parse `git diff` or PR files
return "def hello(name):\n print('Hello ' + name)"
repo = os.environ.get("GITHUB_REPOSITORY") # e.g. "owner/repo"
pr_number = get_pr_number()

token = os.environ.get("GITHUB_TOKEN")
if not token:
raise Exception("Missing GITHUB_TOKEN environment variable.")

headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github.v3+json",
}

url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}/files"

diffs = []
page = 1
while True:
response = requests.get(url, headers=headers, params={"page": page, "per_page": 100})
response.raise_for_status()
files = response.json()
if not files:
break

for f in files:
patch = f.get("patch")
if patch:
diffs.append(f"File: {f['filename']}\n{patch}")

page += 1

return "\n\n".join(diffs)

def post_pr_comment(body: str):
repo = os.getenv("GITHUB_REPOSITORY")
token = os.getenv("GITHUB_TOKEN")
pr_number = get_pr_number()

url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json"
}
payload = {
"body": body
}

def fake_llm_review(diff):
if "print" in diff:
return "Consider using logging instead of print for better production code quality."
return "No issues found."
response = requests.post(url, headers=headers, json=payload)
print("GitHub API response:", response.status_code, response.text)
response.raise_for_status()
10 changes: 5 additions & 5 deletions test_module/hello.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# test_module/hello.py
# Hello.py: A sample python script used for testing reviews

def hello(name):
print("Hello " + name)

hello("World")
def helloWorld():
print("Hello World")
helloWorld()