Beispiel #1
0
async def main(request: Request) -> Response:
    try:
        body = await request.read()
        secret = os.environ.get("GITHUB_SECRET")
        event = Event.from_http(request.headers, body, secret=secret)
        if event.event == "ping":
            return Response(status=200)
        logger.info(
            "event=%s delivery_id=%s",
            f"{event.event}:{event.data['action']}",
            event.delivery_id,
        )
        async with ClientSession() as session:
            gh = GitHubAPI(
                event.data["installation"]["id"],
                session,
                "dhruvmanila/algorithms-keeper",
                cache=cache,
            )
            # Give GitHub some time to reach internal consistency.
            await asyncio.sleep(1)
            await main_router.dispatch(event, gh)
        if gh.rate_limit is not None:  # pragma: no cover
            logger.info(
                "ratelimit=%s, time_remaining=%s",
                f"{gh.rate_limit.remaining}/{gh.rate_limit.limit}",
                gh.rate_limit.reset_datetime - datetime.now(timezone.utc),
            )
        return Response(status=200)
    except Exception as err:
        logger.exception(err)
        return Response(status=500, text=str(err))
Beispiel #2
0
 async def event_from_request(self, request):
     """Get an event object out of HTTP request."""
     event = Event.from_http(
         request.headers,
         await request.read(),
         secret=self._config.webhook_secret,
     )
     await self.pre_process_webhook_event(event)
     return event
Beispiel #3
0
 async def _dispatch(self, request):
     try:
         event = Event.from_http(request.headers,
                                 await request.read(),
                                 secret=self._verification)
         await self._github_router.dispatch(event)
     except Exception as e:
         logger.exception(e)
         return Response(status=500)
     else:
         return Response(status=200)
Beispiel #4
0
async def dispatch(request):
    github = request.app.plugins['github']
    payload = await request.read()

    try:
        event = Event.from_http(request.headers, payload, secret=github.verify)
        await github.router.dispatch(event, app=request.app)
    except ValidationFailure as e:
        LOG.debug('Github webhook failed verification: %s, %s',
                  request.headers, payload)
        return Response(status=401)
    except Exception as e:
        LOG.exception(e)
        return Response(status=500)
    else:
        return Response(status=200)
Beispiel #5
0
 async def dispatch_webhook(self, headers, body):
     event = Event.from_http(headers, body, secret=self.webhook_secret)
     print(
         f"GH webhook received: type={event.event}, delivery id={event.delivery_id}"
     )
     # Wait a bit to give Github's eventual consistency time to catch up
     await anyio.sleep(1)
     installation_id = glom(event.data, "installation.id", default=None)
     if installation_id is None:
         print("No associated installation; not dispatching")
         return
     client = self.client_for(installation_id)
     for route in self._routes[event.event]:
         if _all_match(event.data, route.restrictions):
             print(f"Routing to {route.async_fn!r}")
             await route.async_fn(event.event, event.data, client)
     try:
         limit = client.rate_limit.remaining
     except AttributeError:
         pass
     else:
         print(f"Rate limit for install {installation_id}: {limit}")
Beispiel #6
0
async def main(request: web.Request) -> web.Response:
    try:
        body = await request.read()
        secret = os.environ.get("GITHUB_SECRET")
        event = Event.from_http(request.headers, body, secret=secret)
        if event.event == "ping":
            return web.Response(status=200)
        logger.info(
            "event=%(event)s delivery_id=%(delivery_id)s",
            {
                "event": f"{event.event}:{event.data['action']}",
                "delivery_id": event.delivery_id,
            },
        )
        async with aiohttp.ClientSession() as session:
            gh = GitHubAPI(session,
                           "dhruvmanila/algorithms-keeper",
                           cache=cache)
            # Give GitHub some time to reach internal consistency.
            await asyncio.sleep(1)
            await router.dispatch(event, gh)
        try:
            logger.info(
                "ratelimit=%(ratelimit)s time_remaining=%(time_remaining)s",
                {
                    "ratelimit":
                    f"{gh.rate_limit.remaining}/{gh.rate_limit.limit}",
                    "time_remaining":
                    gh.rate_limit.reset_datetime -
                    datetime.datetime.now(datetime.timezone.utc),
                },
            )
        except AttributeError:
            pass
        return web.Response(status=200)
    except Exception as err:
        logger.exception(err)
        return web.Response(status=500, text=str(err))