From 88a6208b81a19928aa688e1479eece46ed2cc45d Mon Sep 17 00:00:00 2001 From: Paul Gessinger Date: Fri, 10 Jan 2025 11:25:49 +0100 Subject: [PATCH] some cleanup --- src/ci_relay/github.py | 45 +++++++++++------------------------------- src/ci_relay/web.py | 10 ++++++---- 2 files changed, 17 insertions(+), 38 deletions(-) diff --git a/src/ci_relay/github.py b/src/ci_relay/github.py index a425616..e024161 100644 --- a/src/ci_relay/github.py +++ b/src/ci_relay/github.py @@ -1,12 +1,7 @@ -import asyncio import hmac -import io -from tracemalloc import start -from typing import Any, List, Mapping +from typing import Any, Mapping import json import gidgethub -import dateutil.parser -from pprint import pprint import textwrap from gidgethub.routing import Router @@ -14,7 +9,6 @@ from sanic import Sanic from gidgethub.abc import GitHubAPI from gidgethub.sansio import Event -from gidgethub import BadRequest from gidgetlab.abc import GitLabAPI import aiohttp @@ -25,7 +19,12 @@ def create_router(): router = Router() @router.register("pull_request") - async def on_pr(event: Event, gh: GitHubAPI, app: Sanic, gl: GitLabAPI): + async def on_pr( + event: Event, + gh: GitHubAPI, + app: Sanic, + gl: GitLabAPI, + ): pr = event.data["pull_request"] logger.debug("Received pull_request event on PR #%d", pr["number"]) @@ -65,7 +64,7 @@ async def on_push(event: Event, gh: GitHubAPI, app: Sanic, gl: GitLabAPI): return router -async def get_installed_repos(gh: GitHubAPI) -> List[str]: +async def get_installed_repos(gh: GitHubAPI) -> dict[str, Any]: return await gh.getitem("/installation/repositories") @@ -87,7 +86,7 @@ async def add_rejection_status(gh: GitHubAPI, head_sha, repo_url): "head_branch": "", "head_sha": head_sha, "output": { - "title": f"Pipeline refused", + "title": "Pipeline refused", "summary": "No pipeline was triggered for this user", }, } @@ -109,7 +108,7 @@ async def add_failure_status(gh: GitHubAPI, head_sha, repo_url, message): "head_branch": "", "head_sha": head_sha, "output": { - "title": f"Pipeline could not be created", + "title": "Pipeline could not be created", "summary": message, }, } @@ -138,7 +137,6 @@ async def handle_check_suite( repo_url = data["repository"]["url"] repo_slug = make_repo_slug(data["repository"]["full_name"]) head_sha = data["check_suite"]["head_sha"] - head_branch = data["check_suite"]["head_branch"] if data["check_suite"]["app"]["id"] != config.APP_ID: logger.debug("Ignoring rerequest for check suite from other app") @@ -282,25 +280,6 @@ async def handle_push( head_ref=data["ref"], ) - # payload = { - # "name": "CI Bridge", - # "status": "queued", - # "head_branch": "", - # "head_sha": head_sha, - # "output": { - # "title": f"Queued on GitLab CI", - # "summary": "", - # }, - # } - - # logger.debug( - # "Posting check run status for sha %s to GitHub: %s", - # head_sha, - # f"{repo_url}/check-runs", - # ) - - # res = await gh.post(f"{repo_url}/check-runs", data=payload) - async def handle_rerequest( gh: GitHubAPI, session: aiohttp.ClientSession, data: Mapping[str, Any] @@ -355,9 +334,7 @@ async def get_author_in_team(gh: GitHubAPI, author: str, org: str) -> bool: return True try: - membership = await gh.getitem( - f"/orgs/{org}/teams/{allow_team}/memberships/{author}" - ) + await gh.getitem(f"/orgs/{org}/teams/{allow_team}/memberships/{author}") return True except gidgethub.BadRequest as e: if e.status_code != 404: diff --git a/src/ci_relay/web.py b/src/ci_relay/web.py index 985dc7e..4ea0596 100644 --- a/src/ci_relay/web.py +++ b/src/ci_relay/web.py @@ -1,5 +1,4 @@ import hmac -import logging from sanic import Sanic, response import aiohttp @@ -76,7 +75,6 @@ async def handle_webhook(request): project_id = payload["project_id"] pipeline_id = payload["pipeline_id"] - pipeline, variables, project, job = await asyncio.gather( gitlab.get_pipeline( project_id, pipeline_id, session=app.ctx.aiohttp_session @@ -136,12 +134,16 @@ async def handle_webhook(request): gh = await client_for_installation(app, installation_id) - gl = gidgetlab.aiohttp.GitLabAPI(app.ctx.aiohttp_session, requester="acts",access_token=config.GITLAB_ACCESS_TOKEN, url=config.GITLAB_API_URL) + gl = gidgetlab.aiohttp.GitLabAPI( + app.ctx.aiohttp_session, + requester="acts", + access_token=config.GITLAB_ACCESS_TOKEN, + url=config.GITLAB_API_URL, + ) logger.debug("Dispatching event %s", event.event) await app.ctx.github_router.dispatch(event, gh, app=app, gl=gl) - @app.route("/webhook", methods=["POST"]) async def github(request): logger.debug("Webhook received")