Skip to content

Commit 365ebfe

Browse files
committed
fix
Signed-off-by: Kenta Kozuka <kenta.kozuka@gmail.com>
1 parent 24fa01c commit 365ebfe

File tree

7 files changed

+456
-77
lines changed

7 files changed

+456
-77
lines changed

action.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ runs:
2727
uses: actions/setup-python@v4
2828
with:
2929
python-version: "3.11"
30+
3031
- name: Install Python library
3132
run: |
32-
poetry install --no-interaction
33+
poetry install --no-root --no-interaction
3334
shell: bash
3435

3536
- name: Run review
@@ -38,4 +39,4 @@ runs:
3839
env:
3940
PERSONAL_ACCESS_TOKEN: ${{ inputs.PERSONAL_ACCESS_TOKEN }}
4041
OPENAI_API_KEY: ${{ inputs.OPENAI_API_KEY }}
41-
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}"
42+
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}

poetry.lock

Lines changed: 383 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
[tool.poetry]
2-
authors = ["Kenta Kozuka <kenta.kozuka@gmail.com>"]
3-
description = ""
42
name = "github-pr-review"
5-
packages = [{include = "github_pr_review"}]
6-
readme = "README.md"
73
version = "0.1.0"
4+
description = ""
5+
authors = ["Kenta Kozuka <kenta.kozuka@gmail.com>"]
6+
readme = "README.md"
7+
packages = [{include = "github_pr_review"}]
88

99
[tool.poetry.dependencies]
10-
langchain = "^0.0.194"
11-
openai = "^0.27.8"
1210
python = "^3.11"
11+
pygithub = "^1.58.2"
12+
openai = "^0.27.8"
13+
langchain = "^0.0.194"
14+
15+
16+
[tool.poetry.group.dev.dependencies]
17+
black = "^23.3.0"
1318

1419
[build-system]
15-
build-backend = "poetry.core.masonry.api"
1620
requires = ["poetry-core"]
21+
build-backend = "poetry.core.masonry.api"

src/main.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,77 @@
11
import os
22

33
from github import Github
4-
from src.review.review import review
4+
import openai
5+
from langchain.chat_models import ChatOpenAI
6+
from langchain.schema import (
7+
SystemMessage,
8+
HumanMessage,
9+
)
10+
11+
CHAR_SOFT_LIMIT = 9000
12+
FILE_SYSTEM_EN = 'You are an experienced software developer. You will act as a reviewer for a GitHub Pull Request titled "{}".'
13+
FILE_QUESTION_EN = "The following is a GitHub patch. Please summarize the key changes and identify potential problems. Start with the most important findings.\n\n"
14+
SUMMARY_SYSTEM_EN = "Here is a set of summaries for software source code patches. Each summary starts with a ------ line. Please write an overall summary considering all the individual summary. Please present the potential issues and errors first, following by the most important findings, in your summary.\n\n{}"
15+
16+
FILE_SYSTEM_JP = 'あなたは経験豊富なソフトウェア開発者です。"{}"というタイトルのGitHub Pull Requestのレビュアーをしてもらいます。'
17+
FILE_QUESTION_JP = "以下はGitHubのパッチです。主要な変更点をまとめ、潜在的な問題点を特定してください。最も重要な発見から始めてください。\n\n"
18+
SUMMARY_SYSTEM_JP = "ここでは、ソフトウェアのソースコードパッチに関する要約を紹介します。それぞれの要約は、------の行で始まります。個々の要約をすべて考慮した全体的な要約を作成してください。潜在的な問題やエラーを最初に提示し、次に最も重要な発見を要約してください。\n\n{}"
519

620

721
def main():
822
pat = os.getenv("PERSONAL_ACCESS_TOKEN")
923
repo_name = os.getenv("GITHUB_REPOSITORY")
1024
issue_number = os.getenv("GITHUB_ISSUE_NUMBER")
11-
openai_api_key = os.getenv("OPENAI_API_KEY")
1225

1326
github_client = Github(pat)
1427
repo = github_client.get_repo(repo_name)
15-
issue = repo.get_issue(number=int(issue_number))
28+
issue = repo.get_issue(int(issue_number))
29+
30+
if not issue.pull_request:
31+
return
32+
33+
openai.api_key = os.getenv("OPENAI_API_KEY")
34+
chat = ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", request_timeout=600)
35+
system = FILE_SYSTEM_JP.format(issue.title)
36+
37+
# Iterate all patches.
38+
reviews = []
39+
reviews_summary = ""
40+
review_details = ""
41+
files = issue.as_pull_request().get_files()
42+
for file in files:
43+
question = FILE_QUESTION_JP + file.patch[:CHAR_SOFT_LIMIT]
44+
print(question)
1645

17-
comments = issue.get_comments()
46+
messages = [
47+
SystemMessage(content=system),
48+
HumanMessage(content=question),
49+
]
50+
ai_message = chat(messages)
51+
# For summary.
52+
if len(reviews_summary) < CHAR_SOFT_LIMIT:
53+
reviews_summary += "------\n"
54+
reviews_summary += ai_message.content
55+
reviews_summary += "\n"
56+
# For details.
57+
review_details += "\n---\n\n**{}**\n".format(file.filename)
58+
review_details += "{}\n".format(ai_message.content)
1859

19-
prompt = prompt_template.format(
20-
issue_title=issue.title, comments_section=comments_section
21-
)
60+
# Summarize all patches.
61+
question = SUMMARY_SYSTEM_JP.format(reviews_summary)
62+
messages = [
63+
SystemMessage(content=system),
64+
HumanMessage(content=question),
65+
]
66+
print(question)
2267

23-
# Truncate prompt to 4000 characters because 4000 token is the limit.
24-
prompt = prompt[:4000]
25-
print(prompt)
26-
review_text = review(prompt, openai_api_key)
68+
ai_message = chat(messages)
69+
resp = ai_message.content
70+
resp += "\n\n## Details\n\n"
71+
resp += review_details
2772

28-
issue.create_comment(f"\n{review_text}")
73+
issue.create_comment(resp)
2974

3075

3176
if __name__ == "__main__":
32-
main()
77+
main()

src/reviewer/__init__.py

Whitespace-only changes.

src/reviewer/reviewer.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)