-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #21
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
base: main
Are you sure you want to change the base?
Dev #21
Changes from all commits
5aeed7f
dc446df
e617560
7b0b2cc
a1722d0
4480f6a
4a01924
d0ac707
ef6123a
0316762
9a82834
0104ac8
e10a08a
7c05a27
67a409c
96af70e
6789d78
16d26a7
a1d66c5
0be6709
341cb80
bb9f554
cd67b91
dd5ceca
b23350f
7562bdd
7b265a6
80fc587
2655f61
71552dc
74ea348
e535c4a
4c405b9
f20d967
6f4caa4
573b0c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 }} | ||
| BASE_BRANCH_NAME: ${{ github.event.pull_request.base.ref }} | ||
| run: poetry run python create_pr_comments.py | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .env |
| 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.") | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code does not handle the case when the |
||
| pr_repository, | ||
| pr_number, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code does not handle the case when the |
||
| git_api_token, | ||
| comment["body"], | ||
| commit_id, | ||
| comment["path"], | ||
| comment["line"], | ||
| comment["side"], | ||
| ) | ||
| logger.info(comment_data) | ||
| except: | ||
| logger.exception(comment) | ||
There was a problem hiding this comment.
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 addcommand directly instead of installing poetry separately.