Codium-ai / pr-agent

🚀CodiumAI PR-Agent: An AI-Powered 🤖 Tool for Automated Pull Request Analysis, Feedback, Suggestions and More! 💻🔍
Apache License 2.0
5.75k stars 534 forks source link

AttributeError when calling get_git_provider_with_context() #993

Closed danstis closed 3 months ago

danstis commented 3 months ago

I have a self hosted instance of PR-Agent in github app mode running that has been working fine for a long time. It has recently stopped working and this error is raised when it attempts to process a comment:

[2024-06-23 03:39:38 +0000] [25] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/site-packages/starlette_context/middleware/raw_middleware.py", line 92, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 75, in app
    await response(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/responses.py", line 162, in __call__
    await self.background()
  File "/usr/local/lib/python3.10/site-packages/starlette/background.py", line 45, in __call__
    await task()
  File "/usr/local/lib/python3.10/site-packages/starlette/background.py", line 28, in __call__
    await self.func(*self.args, **self.kwargs)
  File "/app/pr_agent/servers/github_app.py", line 273, in handle_request
    await handle_comments_on_pr(body, event, sender, sender_id, action, log_context, agent)
  File "/app/pr_agent/servers/github_app.py", line 114, in handle_comments_on_pr
    provider = get_git_provider_with_context(pr_url=api_url)
  File "/app/pr_agent/git_providers/__init__.py", line 47, in get_git_provider_with_context
    if is_context_env and context.get("git_provider", {}).get("pr_url", {}):
AttributeError: 'NoneType' object has no attribute 'get'

It seems to me that the context.get("git_provider", {}) is returning no value, which means that it can't perform a get on it.

I tested replacing this line: pr_agent/git_providers/init.py:49

    if is_context_env and context.get("git_provider", {}).get("pr_url", {}):

with this:

    if (
        is_context_env
        and context.get("git_provider")
        and context.get("git_provider").get("pr_url")
    ):

It started working again for me, not sure if this is the correct fix here, but thought I would suggest it.

danstis commented 3 months ago

I raised a PR with this same fix, in case it is the correct path forward here.

mrT23 commented 3 months ago

i think this should solve it (in a cleaner way): https://github.com/Codium-ai/pr-agent/commit/3f3e9909fe972f8f75e12d4bb8c49e8ee887e219

danstis commented 3 months ago

Thanks, seems to work well with the current main branch.