Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
paulgessinger committed Jan 10, 2025
1 parent 877e081 commit 88a6208
Showing 2 changed files with 17 additions and 38 deletions.
45 changes: 11 additions & 34 deletions src/ci_relay/github.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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
from sanic.log import logger
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:
10 changes: 6 additions & 4 deletions src/ci_relay/web.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 88a6208

Please sign in to comment.