From 5742a9be1e822babd06a8fe57e5d78d3c5d83a0f Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 17:46:12 +0300 Subject: [PATCH 01/12] Github custom action development --- .github/workflows/review.yaml | 13 +++++++++++ Dockerfile.github_action | 10 +++++++++ action.yaml | 10 +++++++++ github_action/entrypoint.sh | 2 ++ pr_agent/servers/github_action_runner.py | 28 ++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 .github/workflows/review.yaml create mode 100644 Dockerfile.github_action create mode 100644 action.yaml create mode 100644 github_action/entrypoint.sh create mode 100644 pr_agent/servers/github_action_runner.py diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml new file mode 100644 index 00000000..93ad8040 --- /dev/null +++ b/.github/workflows/review.yaml @@ -0,0 +1,13 @@ +on: [push] + +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 + with: + openai-key: "TBD: openai key" + 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..3b1949ce --- /dev/null +++ b/action.yaml @@ -0,0 +1,10 @@ +name: 'PR Agent' +description: 'Summarize, review and suggest improvements for pull requests' +inputs: + openai-key: # id of input + description: 'OpenAI Key' + required: true + default: '' +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..39f7469d --- /dev/null +++ b/pr_agent/servers/github_action_runner.py @@ -0,0 +1,28 @@ +import json +import os + + +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')) + GITHUB_REPOSITORY = os.environ.get('GITHUB_REPOSITORY', None) + if not GITHUB_REPOSITORY: + print("GITHUB_REPOSITORY not set") + return + print(event_payload) + print(GITHUB_REPOSITORY) + print(GITHUB_EVENT_NAME) + print(GITHUB_EVENT_PATH) + print("Hello from run_action") + RUNNER_DEBUG = os.environ.get('RUNNER_DEBUG', None) + if not RUNNER_DEBUG: + print("RUNNER_DEBUG not set") + return + print(RUNNER_DEBUG) From 9ca6b789a7819015938c7671e6e387fe4e6ce689 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:02:38 +0300 Subject: [PATCH 02/12] Github custom action development - WIP --- pr_agent/servers/github_action_runner.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 39f7469d..7589b2f5 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -26,3 +26,7 @@ def run_action(): print("RUNNER_DEBUG not set") return print(RUNNER_DEBUG) + + +if __name__ == '__main__': + run_action() From 38c8d187d214fc2c4987bf4bd60819a615c9d9d5 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:16:25 +0300 Subject: [PATCH 03/12] Github custom action development - WIP --- .github/workflows/review.yaml | 2 +- pr_agent/servers/github_action_runner.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml index 93ad8040..91a49c07 100644 --- a/.github/workflows/review.yaml +++ b/.github/workflows/review.yaml @@ -1,4 +1,4 @@ -on: [push] +on: [push, pull_request] jobs: pr_agent_job: diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 7589b2f5..6b9141bf 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -16,17 +16,20 @@ def run_action(): if not GITHUB_REPOSITORY: print("GITHUB_REPOSITORY not set") return - print(event_payload) - print(GITHUB_REPOSITORY) - print(GITHUB_EVENT_NAME) - print(GITHUB_EVENT_PATH) - print("Hello from run_action") RUNNER_DEBUG = os.environ.get('RUNNER_DEBUG', None) if not RUNNER_DEBUG: print("RUNNER_DEBUG not set") return + ### DEBUG + print(event_payload) + print(GITHUB_REPOSITORY) + print(GITHUB_EVENT_NAME) + print(GITHUB_EVENT_PATH) print(RUNNER_DEBUG) + if GITHUB_EVENT_NAME == "pull_request": + print("PR event") + if __name__ == '__main__': run_action() From 4033303c1f92714012d59504b90f9fe90b82d32f Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:18:23 +0300 Subject: [PATCH 04/12] Github custom action development - WIP --- pr_agent/servers/github_action_runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 6b9141bf..6127b9d2 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -19,7 +19,6 @@ def run_action(): RUNNER_DEBUG = os.environ.get('RUNNER_DEBUG', None) if not RUNNER_DEBUG: print("RUNNER_DEBUG not set") - return ### DEBUG print(event_payload) print(GITHUB_REPOSITORY) From f337d76af63747bcc82c78328d2baf59c51020e3 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:32:28 +0300 Subject: [PATCH 05/12] Github custom action development - WIP --- .github/workflows/review.yaml | 6 ++--- pr_agent/servers/github_action_runner.py | 31 +++++++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml index 91a49c07..6b8a09e0 100644 --- a/.github/workflows/review.yaml +++ b/.github/workflows/review.yaml @@ -8,6 +8,6 @@ jobs: - name: PR Agent action step id: pragent uses: Codium-ai/pr-agent@feature/github_action - with: - openai-key: "TBD: openai key" - + env: + OPENAI_KEY: ${{ secrets.OPENAI_KEY }} + OPENAI_ORG: ${{ secrets.OPENAI_ORG }} diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 6127b9d2..27b50433 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -1,6 +1,9 @@ import json import os +from pr_agent.config_loader import settings +from pr_agent.tools.pr_reviewer import PRReviewer + def run_action(): GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME', None) @@ -12,22 +15,32 @@ def run_action(): print("GITHUB_EVENT_PATH not set") return event_payload = json.load(open(GITHUB_EVENT_PATH, 'r')) - GITHUB_REPOSITORY = os.environ.get('GITHUB_REPOSITORY', None) - if not GITHUB_REPOSITORY: - print("GITHUB_REPOSITORY not set") - return 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 ### DEBUG print(event_payload) - print(GITHUB_REPOSITORY) - print(GITHUB_EVENT_NAME) - print(GITHUB_EVENT_PATH) - print(RUNNER_DEBUG) + 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": - print("PR event") + action = event_payload.get("action", None) + if action in ["opened", "reopened"]: + pr_url = event_payload.get("pull_request", {}).get("url", None) + if pr_url: + PRReviewer(pr_url).review() if __name__ == '__main__': From 4bb46d9faa672869376411c49a6eb258a8577895 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:37:32 +0300 Subject: [PATCH 06/12] Github custom action development - WIP --- .github/workflows/review.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml index 6b8a09e0..698c525e 100644 --- a/.github/workflows/review.yaml +++ b/.github/workflows/review.yaml @@ -1,4 +1,4 @@ -on: [push, pull_request] +on: [pull_request] jobs: pr_agent_job: @@ -11,3 +11,5 @@ jobs: env: OPENAI_KEY: ${{ secrets.OPENAI_KEY }} OPENAI_ORG: ${{ secrets.OPENAI_ORG }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + From f6d4a214caa733a18be9616c7bbbc0fcc14967fc Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:40:03 +0300 Subject: [PATCH 07/12] Github custom action development - WIP --- pr_agent/servers/github_action_runner.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 27b50433..171e9100 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -1,3 +1,4 @@ +import asyncio import json import os @@ -5,7 +6,7 @@ from pr_agent.config_loader import settings from pr_agent.tools.pr_reviewer import PRReviewer -def run_action(): +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") @@ -40,8 +41,8 @@ def run_action(): if action in ["opened", "reopened"]: pr_url = event_payload.get("pull_request", {}).get("url", None) if pr_url: - PRReviewer(pr_url).review() + await PRReviewer(pr_url).review() if __name__ == '__main__': - run_action() + asyncio.run(run_action()) From e51e443adc01ad49492a96be527886401131c6f8 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:54:11 +0300 Subject: [PATCH 08/12] Github custom action development - WIP --- .github/workflows/review.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml index 698c525e..e5af8c77 100644 --- a/.github/workflows/review.yaml +++ b/.github/workflows/review.yaml @@ -1,5 +1,8 @@ -on: [pull_request] - +on: + pull_request: + types: [opened, reopened] + issue_comment: + types: [created, edited] jobs: pr_agent_job: runs-on: ubuntu-latest From e2323dfb9f5ba27e5d8ed873982914cf7bf2840f Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:54:40 +0300 Subject: [PATCH 09/12] Github custom action development - WIP --- pr_agent/servers/github_action_runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 171e9100..1a2c8714 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -30,7 +30,7 @@ async def run_action(): return ### DEBUG print(event_payload) - + print(GITHUB_EVENT_NAME) settings.set("OPENAI.KEY", OPENAI_KEY) if OPENAI_ORG: settings.set("OPENAI.ORG", OPENAI_ORG) From f466d79031e4cd8c006d247c4b22abf692ebc0e6 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 18:59:54 +0300 Subject: [PATCH 10/12] Github custom action development - WIP --- .github/workflows/review.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/review.yaml b/.github/workflows/review.yaml index e5af8c77..31d1c982 100644 --- a/.github/workflows/review.yaml +++ b/.github/workflows/review.yaml @@ -1,8 +1,6 @@ on: pull_request: - types: [opened, reopened] issue_comment: - types: [created, edited] jobs: pr_agent_job: runs-on: ubuntu-latest From 1c1aad2806a8149432dc726608baa3568462af06 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 19:08:10 +0300 Subject: [PATCH 11/12] Github custom action development - WIP --- pr_agent/servers/github_action_runner.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 1a2c8714..953b0b8f 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -3,6 +3,7 @@ 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 @@ -28,9 +29,6 @@ async def run_action(): if not GITHUB_TOKEN: print("GITHUB_TOKEN not set") return - ### DEBUG - print(event_payload) - print(GITHUB_EVENT_NAME) settings.set("OPENAI.KEY", OPENAI_KEY) if OPENAI_ORG: settings.set("OPENAI.ORG", OPENAI_ORG) @@ -43,6 +41,18 @@ async def run_action(): 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()) From ea1cd7ae45ac526d414abdeb0feb3c4d6e427123 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 13 Jul 2023 19:14:44 +0300 Subject: [PATCH 12/12] Github custom action development - WIP --- action.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/action.yaml b/action.yaml index 3b1949ce..b592abee 100644 --- a/action.yaml +++ b/action.yaml @@ -1,10 +1,5 @@ name: 'PR Agent' description: 'Summarize, review and suggest improvements for pull requests' -inputs: - openai-key: # id of input - description: 'OpenAI Key' - required: true - default: '' runs: using: 'docker' image: 'Dockerfile.github_action'