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

Test19 #19

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 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'
- 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 }}
Copy link
Owner Author

Choose a reason for hiding this comment

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

I suggest using the google-api-python-client library to interact with the GitHub API instead of directly using requests. This library provides a more convenient and robust way to handle authentication and API requests.

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.")
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(
pr_repository,
pr_number,
git_api_token,
comment["body"],
commit_id,
comment["path"],
comment["line"],
comment["side"],
)
logger.info(comment_data)
except:
logger.exception(comment)
Loading
Loading