-
Notifications
You must be signed in to change notification settings - Fork 0
Utilize Groq to comment MR suggestions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
178fd58 to
3f3e415
Compare
Code ReviewThe provided code diff appears to be implementing a code review bot using GitHub Actions and the Groq API. Here are some improvements: Security and Best Practices
Code Quality and Readability
Functionality
Performance
Code Suggestions
Updated CodeHere is an updated version of the code incorporating some of these suggestions:
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4@1234567890abcdef
- name: Install dependencies
run: |
pip install -r requirements.txt
- 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: |
python3 -m reviewbot.main
import os
import requests
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
def review_with_groq(diff: str) -> str:
api_key = os.environ.get("GROQ_API_KEY")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"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, list improvements in bullet points:\n\n{diff}"}
],
"temperature": 0.3,
}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload, timeout=30)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except requests.RequestException as e:
print(f"Error: {e}")
return ""
def main():
diff = get_diff()
review = review_with_groq(diff)
post_pr_comment(review)
if __name__ == "__main__":
main()
import os
import requests
import json
def get_pr_number() -> int:
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() -> str:
github_repository = os.environ.get("GITHUB_REPOSITORY")
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/{github_repository}/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", "").strip()
if patch:
diffs.append(f"File: {f['filename']}\n{patch}")
page += 1
return "\n\n".join(diffs)
def post_pr_comment(body: str):
github_repository = os.getenv("GITHUB_REPOSITORY")
token = os.getenv("GITHUB_TOKEN")
pr_number = get_pr_number()
url = f"https://api.github.com/repos/{github_repository}/issues/{pr_number}/comments"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json"
}
payload = {
"body": body
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
except requests.RequestException as e:
print(f"Error: {e}") |
|
Here are the improvements in bullet points: Code Quality and Best Practices
Security
Performance
Readability and Maintainability
Testing
Code Style
Here is an updated version of the code incorporating some of these suggestions: .github/workflows/code-review.yml name: Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install -r requirements.txt
- 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: |
python3 -m reviewbot.mainreviewbot/main.py import os
import requests
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
GROQ_API_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
def review_with_groq(diff):
try:
api_key = os.environ.get("GROQ_API_KEY")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"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, list improvements in bullet points:\n\n{diff}"}
],
"temperature": 0.3,
}
response = requests.post(GROQ_API_ENDPOINT, headers=headers, json=payload)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return ""
def main():
diff = get_diff()
review = review_with_groq(diff)
post_pr_comment(review)
if __name__ == "__main__":
main()reviewbot/utils.py import os
import requests
import json
GITHUB_API_ENDPOINT = "https://api.github.com"
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():
repo = os.environ.get("GITHUB_REPOSITORY")
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"{GITHUB_API_ENDPOINT}/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"{GITHUB_API_ENDPOINT}/repos/{repo}/issues/{pr_number}/comments"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json"
}
payload = {
"body": body
}
response = requests.post(url, headers=headers, json=payload)
print("GitHub API response:", response.status_code, response.text)
response.raise_for_status() |
Code ReviewThe provided code diff appears to be implementing a code review bot using GitHub Actions and the Groq API. Here are some improvements: Security and Best Practices
Code Quality and Readability
Functionality
Performance
Code SuggestionsHere are some specific code suggestions:
permissions:
contents: read
pull-requests: write
groq_response = response.json()
review = groq_response["choices"][0]["message"]["content"]
github_response = response.json()
try:
# API call
except requests.RequestException as e:
print(f"Error: {e}")Updated CodeHere is an updated version of the code incorporating some of these suggestions:
name: Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install dependencies
run: |
pip install -r requirements.txt
- 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: |
python3 -m reviewbot.main
import os
import requests
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
def review_with_groq(diff: str) -> str:
api_key = os.environ.get("GROQ_API_KEY")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"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, list improvements in bullet points:\n\n{diff}"}
],
"temperature": 0.3,
}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
response.raise_for_status()
groq_response = response.json()
review = groq_response["choices"][0]["message"]["content"]
return review
except requests.RequestException as e:
print(f"Error: {e}")
return ""
def main():
diff = get_diff()
review = review_with_groq(diff)
post_pr_comment(review)
if __name__ == "__main__":
main()
import os
import requests
import json
def get_pr_number() -> int:
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() -> str:
repo = os.environ.get("GITHUB_REPOSITORY")
pr_number = get_pr_number()
github_token = os.environ.get("GITHUB_TOKEN")
if not github_token:
raise Exception("Missing GITHUB_TOKEN environment variable.")
headers = {
"Authorization": f"Bearer {github_token}",
"Accept": "application/vnd.github.v3+json",
}
url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}/files"
diffs = []
page = 1
while True:
try:
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
except requests.RequestException as e:
print(f"Error: {e}")
break
return "\n\n".join(diffs)
def post_pr_comment(body: str):
repo = os.getenv("GITHUB_REPOSITORY")
github_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 {github_token}",
"Accept": "application/vnd.github+json"
}
payload = {
"body": body
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
except requests.RequestException as e:
print(f"Error: {e}") |
* Add two-sum example * Allow GH action to post review as comment
Description
This contains the initial changes for implementing code review bot using GitHub actions and Groq API.