Skip to content
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

first draft #1

Merged
merged 15 commits into from
Aug 8, 2024
Merged
42 changes: 42 additions & 0 deletions .github/actions/code-review-action/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: 'Code Review'
description: 'Generate prompt for code review'
#inputs:
# who-to-greet: # id of input
# description: 'Who to greet'
# required: true
# default: 'World'
#outputs:
# random-number:
# description: "Random number"
# value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
using: "composite"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v44

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
shell: bash
run: |
python -m pip install --upgrade pip
pip install PyGithub # requests

- name: Run Code Review
env:
# REVIEW_API_URL: some_url #${{ secrets.REVIEW_API_URL }}
# REVIEW_API_KEY: some_api_ket #${{ secrets.REVIEW_API_KEY }}
PR_NUMBER: ${{ github.event.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
run: |
python code_review.py
98 changes: 98 additions & 0 deletions .github/actions/code-review-action/code_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os
# import requests
# import subprocess
from github import Github

g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
pr = repo.get_pull(int(os.environ['PR_NUMBER']))


def get_file_content(file_path, commit_sha):
try:
return repo.get_contents(file_path, ref=commit_sha).decoded_content.decode()
except:
return "" # Return empty string if file doesn't exist in that commit


def get_file_patch(file):
return file.patch if file.patch else ""


changed_files = '${{ steps.changed-files.outputs.all_changed_files }}'.split()

file_data = []
for file in pr.get_files():
if file.filename in changed_files:
original_content = get_file_content(file.filename, pr.base.sha)
changed_content = get_file_content(file.filename, pr.head.sha)
patch = get_file_patch(file)
file_data.append({
"filename": file.filename,
"originalContent": original_content,
"changedContent": changed_content,
"patch": patch
})

changed_files = '${{ steps.changed-files.outputs.all_changed_files }}'.split()
print("XXX", changed_files)


def format_code_review_data(data_list):
formatted_string = "Beginning of Code Review Data:\n\n"

for item in data_list:
formatted_string += f"=============\n"
formatted_string += f"Beginning of data for file: {item['filename']}\n"
formatted_string += "Original Content:\n"
formatted_string += "```\n"
formatted_string += item['originalContent']
formatted_string += "\n```\n\n"
formatted_string += "Patch:\n"
formatted_string += "```\n"
formatted_string += item['patch']
formatted_string += "\n```\n\n"
formatted_string += "---\n"
formatted_string += f"End of data for file: {item['filename']}\n"
formatted_string += f"=============\n\n"

formatted_string += "End of Code Review Data"

return formatted_string


print("# ##############################################################################")
print(format_code_review_data(file_data))
print("# ##############################################################################")

try:
review_comments = {file: [(1, 'cool')] for file in changed_files}
g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
pr = repo.get_pull(int(os.environ['PR_NUMBER']))

last_commit = list(pr.get_commits())[-1]

for filename, comments in review_comments.items():
for line, comment in comments:
print("XXX", line, comment)
pr.create_review_comment(body=comment, commit=last_commit, path=filename, line=int(line))

print('Code review completed and comments posted.')
except Exception as e:
print(f'Error during code review: {str(e)}')
import traceback

traceback.print_exc()
exit(1)
shell: python
# response = requests.post(
# os.environ['REVIEW_API_URL'],
# json={"files": file_data},
# headers={
# 'Authorization': f"Bearer {os.environ['REVIEW_API_KEY']}",
# 'Content-Type': 'application/json'
# }
# )
# response.raise_for_status()
# review_comments = response.json()
120 changes: 117 additions & 3 deletions .github/workflows/code_review.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Code Review
name: Code Review
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool


on:
pull_request:
Expand All @@ -7,8 +7,122 @@ on:
jobs:
code-review:
runs-on: ubuntu-latest
#if: contains(github.event.pull_request.labels.*.name, 'review')
if: contains(github.event.pull_request.labels.*.name, 'code-review')
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v44

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests PyGithub

- name: Run Code Review
env:
REVIEW_API_URL: some_url #${{ secrets.REVIEW_API_URL }}
REVIEW_API_KEY: some_api_ket #${{ secrets.REVIEW_API_KEY }}
PR_NUMBER: ${{ github.event.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
import os
import requests
import subprocess
from github import Github

g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
pr = repo.get_pull(int(os.environ['PR_NUMBER']))

def get_file_content(file_path, commit_sha):
try:
return repo.get_contents(file_path, ref=commit_sha).decoded_content.decode()
except:
return "" # Return empty string if file doesn't exist in that commit

def get_file_patch(file):
return file.patch if file.patch else ""

changed_files = '${{ steps.changed-files.outputs.all_changed_files }}'.split()

file_data = []
for file in pr.get_files():
if file.filename in changed_files:
original_content = get_file_content(file.filename, pr.base.sha)
changed_content = get_file_content(file.filename, pr.head.sha)
patch = get_file_patch(file)
file_data.append({
"filename": file.filename,
"originalContent": original_content,
"changedContent": changed_content,
"patch": patch
})

changed_files = '${{ steps.changed-files.outputs.all_changed_files }}'.split()
print("XXX", changed_files)

def format_code_review_data(data_list):
formatted_string = "Beginning of Code Review Data:\n\n"

for item in data_list:
formatted_string += f"=============\n"
formatted_string += f"Beginning of data for file: {item['filename']}\n"
formatted_string += "Original Content:\n"
formatted_string += "```\n"
formatted_string += item['originalContent']
formatted_string += "\n```\n\n"
formatted_string += "Patch:\n"
formatted_string += "```\n"
formatted_string += item['patch']
formatted_string += "\n```\n\n"
formatted_string += "---\n"
formatted_string += f"End of data for file: {item['filename']}\n"
formatted_string += f"=============\n\n"

formatted_string += "End of Code Review Data"

return formatted_string

print("# ##############################################################################")
print(format_code_review_data(file_data))
print("# ##############################################################################")

try:
review_comments = { file:[(1,'cool')] for file in changed_files }
g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
pr = repo.get_pull(int(os.environ['PR_NUMBER']))

last_commit = list(pr.get_commits())[-1]

for filename, comments in review_comments.items():
for line, comment in comments:
print("XXX", line, comment)
pr.create_review_comment(body=comment, commit=last_commit, path=filename, line=int(line))

print('Code review completed and comments posted.')
except Exception as e:
print(f'Error during code review: {str(e)}')
import traceback
traceback.print_exc()
exit(1)
shell: python
# response = requests.post(
# os.environ['REVIEW_API_URL'],
# json={"files": file_data},
# headers={
# 'Authorization': f"Bearer {os.environ['REVIEW_API_KEY']}",
# 'Content-Type': 'application/json'
# }
# )
# response.raise_for_status()
# review_comments = response.json()