Skip to content

Commit 1497838

Browse files
authored
Merge pull request #33 from Nayjest/exp_comment_from_code
post comment from app
2 parents 7ea18ca + 62b570e commit 1497838

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

.github/workflows/ai-code-review.yml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
steps:
88
- uses: actions/checkout@v4
99
with: { fetch-depth: 0 }
10-
- name: Set up Python 3.11
10+
- name: Set up Python
1111
uses: actions/setup-python@v5
1212
with: { python-version: "3.13" }
1313
- name: Install AI Code Review tool
@@ -22,17 +22,9 @@ jobs:
2222
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
2323
LLM_API_TYPE: openai
2424
MODEL: "gpt-4.1"
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2526
run: |
2627
ai-code-review
28+
ai-code-review github-comment --token "$GITHUB_TOKEN"
2729
- uses: actions/upload-artifact@v4
28-
with: { name: ai-code-review-results, path: code-review-report.txt }
29-
- name: Comment on PR with review
30-
uses: actions/github-script@v7
31-
with:
32-
script: |
33-
await github.rest.issues.createComment({
34-
owner: context.repo.owner,
35-
repo: context.repo.repo,
36-
issue_number: context.issue.number,
37-
body: require('fs').readFileSync('code-review-report.txt', 'utf8')
38-
});
30+
with: { name: ai-code-review-results, path: code-review-report.txt, code-review-report.json }

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,22 @@ jobs:
3737
steps:
3838
- uses: actions/checkout@v4
3939
with: { fetch-depth: 0 }
40-
- name: Set up Python 3.11
40+
- name: Set up Python
4141
uses: actions/setup-python@v5
4242
with: { python-version: "3.13" }
4343
- name: Install AI Code Review tool
44-
run: pip install ai-code-review==0.3.4
44+
run: pip install ai-code-review==0.4.0
4545
- name: Run AI code review
4646
env:
4747
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
4848
LLM_API_TYPE: openai
4949
MODEL: "gpt-4.1"
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5051
run: |
5152
ai-code-review
53+
ai-code-review github-comment --token ${{ secrets.GITHUB_TOKEN }}
5254
- uses: actions/upload-artifact@v4
53-
with: { name: ai-code-review-results, path: code-review-report.txt }
54-
- name: Comment on PR with review
55-
uses: actions/github-script@v7
56-
with:
57-
script: |
58-
await github.rest.issues.createComment({
59-
owner: context.repo.owner,
60-
repo: context.repo.repo,
61-
issue_number: context.issue.number,
62-
body: require('fs').readFileSync('code-review-report.txt', 'utf8')
63-
});
55+
with: { name: ai-code-review-results, path: code-review-report.txt, code-review-report.json }
6456
```
6557
6658
> ⚠️ Make sure to add `LLM_API_KEY` to your repository’s GitHub secrets.

ai_code_review/cli.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import logging
23
import sys
34
import os
45
import shutil
@@ -9,10 +10,11 @@
910
from .core import review
1011
from .report_struct import Report
1112
from git import Repo
13+
import requests
1214

1315
from .constants import ENV_CONFIG_FILE
1416
from .bootstrap import bootstrap
15-
17+
from .project_config import ProjectConfig
1618

1719
app = async_typer.AsyncTyper(
1820
pretty_exceptions_show_locals=False,
@@ -52,3 +54,59 @@ async def remote(url=typer.Option(), branch=typer.Option()):
5254
await review()
5355
finally:
5456
os.chdir(prev_dir)
57+
58+
59+
@app.async_command(help="Leave a GitHub PR comment with the review.")
60+
async def github_comment(
61+
token: str = typer.Option(
62+
os.environ.get("GITHUB_TOKEN", ""), help="GitHub token (or set GITHUB_TOKEN env var)"
63+
),
64+
):
65+
"""
66+
Leaves a comment with the review on the current GitHub pull request.
67+
"""
68+
file = "code-review-report.txt"
69+
if not os.path.exists(file):
70+
print(f"Review file not found: {file}")
71+
raise typer.Exit(4)
72+
73+
with open(file, "r", encoding="utf-8") as f:
74+
body = f.read()
75+
76+
if not token:
77+
print("GitHub token is required (--token or GITHUB_TOKEN env var).")
78+
raise typer.Exit(1)
79+
80+
github_env = ProjectConfig.load().prompt_vars["github_env"]
81+
repo = github_env.get("github_repo", "")
82+
pr_env_val = github_env.get("github_pr_number", "")
83+
logging.info(f"github_pr_number = {pr_env_val}")
84+
85+
# e.g. could be "refs/pull/123/merge" or a direct number
86+
if "/" in pr_env_val and "pull" in pr_env_val:
87+
# refs/pull/123/merge
88+
try:
89+
pr_num_candidate = pr_env_val.strip("/").split("/")
90+
idx = pr_num_candidate.index("pull")
91+
pr = int(pr_num_candidate[idx + 1])
92+
except Exception:
93+
pr = 0
94+
else:
95+
try:
96+
pr = int(pr_env_val)
97+
except Exception:
98+
pr = 0
99+
100+
api_url = f"https://api.github.com/repos/{repo}/issues/{pr}/comments"
101+
headers = {
102+
"Authorization": f"token {token}",
103+
"Accept": "application/vnd.github+json",
104+
}
105+
data = {"body": body}
106+
107+
resp = requests.post(api_url, headers=headers, json=data)
108+
if 200 <= resp.status_code < 300:
109+
logging.info(f"Posted review comment to PR #{pr} in {repo}")
110+
else:
111+
logging.error(f"Failed to post comment: {resp.status_code} {resp.reason}\n{resp.text}")
112+
raise typer.Exit(5)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ai-code-review"
3-
version = "0.3.4"
3+
version = "0.4.0"
44
description = "LLM-agnostic GitHub AI Code Review Tool with integration to GitHub actions"
55
authors = ["Nayjest <mail@vitaliy.in>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)