diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml new file mode 100644 index 00000000..31d1c982 --- /dev/null +++ b/.github/workflows/review.yaml @@ -0,0 +1,16 @@ +on: + pull_request: + issue_comment: +jobs: + pr_agent_job: + runs-on: ubuntu-latest + name: Run pr agent on every pull request + steps: + - name: PR Agent action step + id: pragent + uses: Codium-ai/pr-agent@feature/github_action + env: + OPENAI_KEY: ${{ secrets.OPENAI_KEY }} + OPENAI_ORG: ${{ secrets.OPENAI_ORG }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/Dockerfile.github_action b/Dockerfile.github_action new file mode 100644 index 00000000..c7211787 --- /dev/null +++ b/Dockerfile.github_action @@ -0,0 +1,10 @@ +FROM python:3.10 as base + +WORKDIR /app +ADD requirements.txt . +RUN pip install -r requirements.txt && rm requirements.txt +ENV PYTHONPATH=/app +ADD pr_agent pr_agent +ADD github_action/entrypoint.sh / +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] diff --git a/action.yaml b/action.yaml new file mode 100644 index 00000000..b592abee --- /dev/null +++ b/action.yaml @@ -0,0 +1,5 @@ +name: 'PR Agent' +description: 'Summarize, review and suggest improvements for pull requests' +runs: + using: 'docker' + image: 'Dockerfile.github_action' diff --git a/github_action/entrypoint.sh b/github_action/entrypoint.sh new file mode 100644 index 00000000..4d493c7c --- /dev/null +++ b/github_action/entrypoint.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python /app/pr_agent/servers/github_action_runner.py diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py new file mode 100644 index 00000000..953b0b8f --- /dev/null +++ b/pr_agent/servers/github_action_runner.py @@ -0,0 +1,58 @@ +import asyncio +import json +import os + +from pr_agent.config_loader import settings +from pr_agent.tools.pr_questions import PRQuestions +from pr_agent.tools.pr_reviewer import PRReviewer + + +async def run_action(): + GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME', None) + if not GITHUB_EVENT_NAME: + print("GITHUB_EVENT_NAME not set") + return + GITHUB_EVENT_PATH = os.environ.get('GITHUB_EVENT_PATH', None) + if not GITHUB_EVENT_PATH: + print("GITHUB_EVENT_PATH not set") + return + event_payload = json.load(open(GITHUB_EVENT_PATH, 'r')) + RUNNER_DEBUG = os.environ.get('RUNNER_DEBUG', None) + if not RUNNER_DEBUG: + print("RUNNER_DEBUG not set") + OPENAI_KEY = os.environ.get('OPENAI_KEY', None) + if not OPENAI_KEY: + print("OPENAI_KEY not set") + return + OPENAI_ORG = os.environ.get('OPENAI_ORG', None) + GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN', None) + if not GITHUB_TOKEN: + print("GITHUB_TOKEN not set") + return + settings.set("OPENAI.KEY", OPENAI_KEY) + if OPENAI_ORG: + settings.set("OPENAI.ORG", OPENAI_ORG) + settings.set("GITHUB.USER_TOKEN", GITHUB_TOKEN) + settings.set("GITHUB.DEPLOYMENT_TYPE", "user") + if GITHUB_EVENT_NAME == "pull_request": + action = event_payload.get("action", None) + if action in ["opened", "reopened"]: + pr_url = event_payload.get("pull_request", {}).get("url", None) + if pr_url: + await PRReviewer(pr_url).review() + + elif GITHUB_EVENT_NAME == "issue_comment": + action = event_payload.get("action", None) + if action in ["created", "edited"]: + comment_body = event_payload.get("comment", {}).get("body", None) + if comment_body: + pr_url = event_payload.get("issue", {}).get("pull_request", {}).get("url", None) + if pr_url: + if comment_body.strip().lower() == "review": + await PRReviewer(pr_url).review() + elif comment_body.lstrip().lower().startswith("answer"): + await PRQuestions(pr_url, comment_body).answer() + + +if __name__ == '__main__': + asyncio.run(run_action())