-
Notifications
You must be signed in to change notification settings - Fork 0
98 lines (83 loc) · 3.31 KB
/
code_review.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: Code Review
on:
pull_request:
types: [ labeled ]
jobs:
code-review:
runs-on: ubuntu-latest
#if: contains(github.event.pull_request.labels.*.name, 'review')
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
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
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)
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()