Skip to content
Open

Dev #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: test
run-name: ${{ github.actor }} runs test
on:
pull_request:
types: [opened]
jobs:
check-bats-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12.x'
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The code is unnecessarily complex. You could simplify it by using the poetry add command directly instead of installing poetry separately.

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Run review
env:
GIT_API_TOKEN: ${{ secrets.GIT_API_TOKEN }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
PR_REPO_NAME: ${{ github.event.pull_request.head.repo.full_name }}
PR_BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
BASE_BRANCH_NAME: ${{ github.event.pull_request.base.ref }}
run: poetry run python create_pr_comments.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
87 changes: 87 additions & 0 deletions create_pr_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import requests
import json
import os
import logging
from dotenv import load_dotenv
from run_gemini import get_review

logger = logging.getLogger(__name__)
logging.basicConfig(encoding="utf-8", level=logging.DEBUG)

load_dotenv(override=True)


def create_pull_request_comment(
repository, pull_number, github_api_token, body, commit_id, path, line, side
):

pr_url = f"https://api.github.com/repos/{repository}/pulls/{pull_number}/comments"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {github_api_token}",
"X-GitHub-Api-Version": "2022-11-28",
}
data = {
"body": body,
"commit_id": commit_id,
"path": path,
"line": int(line),
"side": side,
}

logger.debug(data)

response = requests.post(pr_url, headers=headers, json=data)
try:
response.raise_for_status()
except:
logger.exception(response)

return response.json()


def parse_comments(text):
comments = []
lines = text.splitlines()
for line in lines:
if line[0] == "{" and line[-1] == "}":
try:
comment = json.loads(line)
if not all(key in comment for key in ["body", "path", "line", "side"]):
raise ValueError("Invalid format: Required key does not exist.")
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The code could be improved by using a dedicated error handling block instead of the general exception block.

comments.append(comment)
except json.JSONDecodeError:
raise ValueError(f"Invalid format: {line}")
return comments


if __name__ == "__main__":
git_api_token = os.environ["GIT_API_TOKEN"]
pr_number = os.environ["PULL_REQUEST_NUMBER"]
commit_id = os.environ["COMMIT_ID"]
pr_repository = os.environ["PR_REPO_NAME"]
pr_branch_name = os.environ["PR_BRANCH_NAME"]
base_branch_name = os.environ["BASE_BRANCH_NAME"]
google_api_key = os.environ["GOOGLE_API_KEY"]

raw_review = get_review(
google_api_key, pr_branch_name, f"{base_branch_name}", logger
)
review_comments = parse_comments(raw_review)

for comment in review_comments:

try:
comment_data = create_pull_request_comment(
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The code does not handle the case when the get_review function returns an error. It should handle the case when the function returns an empty string.

pr_repository,
pr_number,
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The code does not handle the case when the parse_comments function returns an empty list. It should handle the case when the function returns an empty list.

git_api_token,
comment["body"],
commit_id,
comment["path"],
comment["line"],
comment["side"],
)
logger.info(comment_data)
except:
logger.exception(comment)
Loading