From 1a8fce15055702a4bae1480850eaf5ea8f3eb0ac Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Fri, 28 Jul 2023 02:44:11 +0300 Subject: [PATCH 1/3] Updated handling of installation id --- pr_agent/git_providers/github_provider.py | 3 ++- pr_agent/servers/github_app.py | 12 ++++++++---- pyproject.toml | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 7f617937..d3d3645e 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -5,6 +5,7 @@ from urllib.parse import urlparse from github import AppAuthentication, Auth, Github, GithubException from retry import retry +from starlette_context import context from pr_agent.config_loader import settings @@ -17,7 +18,7 @@ from ..servers.utils import RateLimitExceeded class GithubProvider(GitProvider): def __init__(self, pr_url: Optional[str] = None, incremental=IncrementalPR(False)): self.repo_obj = None - self.installation_id = settings.get("GITHUB.INSTALLATION_ID") + self.installation_id = context.get("installation_id", None) self.github_client = self._get_github_client() self.repo = None self.pr_num = None diff --git a/pr_agent/servers/github_app.py b/pr_agent/servers/github_app.py index 8050117f..d23cccd4 100644 --- a/pr_agent/servers/github_app.py +++ b/pr_agent/servers/github_app.py @@ -4,6 +4,9 @@ import sys import uvicorn from fastapi import APIRouter, FastAPI, HTTPException, Request, Response +from starlette.middleware import Middleware +from starlette_context import context +from starlette_context.middleware import RawContextMiddleware from pr_agent.agent.pr_agent import PRAgent from pr_agent.config_loader import settings @@ -36,7 +39,9 @@ async def handle_github_webhooks(request: Request, response: Response): verify_signature(body_bytes, webhook_secret, signature_header) logging.debug(f'Request body:\n{body}') - + installation_id = body.get("installation", {}).get("id") + context["installation_id"] = installation_id + return await handle_request(body) @@ -48,8 +53,6 @@ async def handle_request(body: Dict[str, Any]): body: The request body. """ action = body.get("action") - installation_id = body.get("installation", {}).get("id") - settings.set("GITHUB.INSTALLATION_ID", installation_id) agent = PRAgent() if action == 'created': @@ -85,7 +88,8 @@ async def root(): def start(): # Override the deployment type to app settings.set("GITHUB.DEPLOYMENT_TYPE", "app") - app = FastAPI() + middleware = [Middleware(RawContextMiddleware)] + app = FastAPI(middleware=middleware) app.include_router(router) uvicorn.run(app, host="0.0.0.0", port=3000) diff --git a/pyproject.toml b/pyproject.toml index 03df2480..ac9a4889 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ dependencies = [ "aiohttp~=3.8.4", "atlassian-python-api==3.39.0", "GitPython~=3.1.32", + "starlette-context==0.3.6" ] [project.urls] From a8780f722d9c251f1981c61d709e4dfd747590b9 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Fri, 28 Jul 2023 03:22:25 +0300 Subject: [PATCH 2/3] Handle marketplace hook --- pr_agent/servers/github_app.py | 39 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/pr_agent/servers/github_app.py b/pr_agent/servers/github_app.py index d23cccd4..a9ba1de5 100644 --- a/pr_agent/servers/github_app.py +++ b/pr_agent/servers/github_app.py @@ -23,21 +23,9 @@ async def handle_github_webhooks(request: Request, response: Response): Verifies the request signature, parses the request body, and passes it to the handle_request function for further processing. """ logging.debug("Received a GitHub webhook") - - try: - body = await request.json() - except Exception as e: - logging.error("Error parsing request body", e) - raise HTTPException(status_code=400, detail="Error parsing request body") from e - - body_bytes = await request.body() - signature_header = request.headers.get('x-hub-signature-256', None) - - webhook_secret = getattr(settings.github, 'webhook_secret', None) - - if webhook_secret: - verify_signature(body_bytes, webhook_secret, signature_header) - + + body = await get_body(request) + logging.debug(f'Request body:\n{body}') installation_id = body.get("installation", {}).get("id") context["installation_id"] = installation_id @@ -45,6 +33,27 @@ async def handle_github_webhooks(request: Request, response: Response): return await handle_request(body) +@router.post("/api/v1/marketplace_webhooks") +async def handle_marketplace_webhooks(request: Request, response: Response): + body = await get_body(request) + logging.info(f'Request body:\n{body}') + +async def get_body(request): + try: + body = await request.json() + except Exception as e: + logging.error("Error parsing request body", e) + raise HTTPException(status_code=400, detail="Error parsing request body") from e + body_bytes = await request.body() + signature_header = request.headers.get('x-hub-signature-256', None) + webhook_secret = getattr(settings.github, 'webhook_secret', None) + if webhook_secret: + verify_signature(body_bytes, webhook_secret, signature_header) + return body + + + + async def handle_request(body: Dict[str, Any]): """ Handle incoming GitHub webhook requests. From 63a703c0005060f76757607485e56af2e3752f97 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Fri, 28 Jul 2023 11:30:51 +0300 Subject: [PATCH 3/3] Handle marketplace hook --- pr_agent/git_providers/github_provider.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index d3d3645e..dac92e89 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -18,7 +18,10 @@ from ..servers.utils import RateLimitExceeded class GithubProvider(GitProvider): def __init__(self, pr_url: Optional[str] = None, incremental=IncrementalPR(False)): self.repo_obj = None - self.installation_id = context.get("installation_id", None) + try: + self.installation_id = context.get("installation_id", None) + except Exception: + self.installation_id = None self.github_client = self._get_github_client() self.repo = None self.pr_num = None