forked from cirolini/genai-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (36 loc) · 1.45 KB
/
main.py
File metadata and controls
44 lines (36 loc) · 1.45 KB
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
"""This project aims to automate code review using the ChatGPT language model."""
import argparse
import openai
import os
from github import Github
# Adding command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--openai_api_key', help='Your OpenAI API Key')
parser.add_argument('--github_token', help='Your Github Token')
parser.add_argument('--github_pr_id', help='Your Github PR ID')
args = parser.parse_args()
# Authenticating with the OpenAI API
openai.api_key = args.openai_api_key
# Authenticating with the Github API
g = Github(args.github_token)
# Selecting the repository
repo = g.get_repo(os.getenv('GITHUB_REPOSITORY'))
# Get pull request
pull_request = repo.get_pull(int(args.github_pr_id))
commits = pull_request.get_commits()
for commit in commits:
# Getting the modified files in the commit
files = commit.files
for file in files:
# Getting the file name and content
filename = file.filename
content = repo.get_contents(filename, ref=commit.sha).decoded_content
# Sending the code to ChatGPT
response = openai.Completion.create(
engine="text-davinci-002",
prompt=(f"Explain Code:\n```{content}```"),
temperature=0.5,
max_tokens=2048
)
# Adding a comment to the pull request with ChatGPT's response
pull_request.create_issue_comment(f"ChatGPT's response to explain this code:\n {response['choices'][0]['text']}")