Skip to content

Commit

Permalink
add repo_slug to payload
Browse files Browse the repository at this point in the history
This reverts commit 75c40da.
  • Loading branch information
paulgessinger committed Jan 10, 2025
1 parent 97dab84 commit a1497e8
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions src/ci_relay/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,20 @@ async def add_failure_status(gh: GitHubAPI, head_sha, repo_url, message):
await gh.post(f"{repo_url}/check-runs", data=payload)


def make_repo_slug(full_name: str) -> str:
return full_name.replace("/", "_")


async def handle_check_suite(
gh: GitHubAPI, session: aiohttp.ClientSession, data: Mapping[str, Any], gl: GitLabAPI
gh: GitHubAPI,
session: aiohttp.ClientSession,
data: Mapping[str, Any],
gl: GitLabAPI,
):
sender = data["sender"]["login"]
org = data["organization"]["login"]
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"]

Expand Down Expand Up @@ -217,6 +225,7 @@ async def handle_check_suite(
await trigger_pipeline(
gh,
repo_url=repo_url,
repo_slug=repo_slug,
head_sha=head_sha,
session=session,
clone_url=clone_url,
Expand All @@ -226,11 +235,15 @@ async def handle_check_suite(


async def handle_push(
gh: GitHubAPI, session: aiohttp.ClientSession, data: Mapping[str, Any], gl: GitLabAPI
gh: GitHubAPI,
session: aiohttp.ClientSession,
data: Mapping[str, Any],
gl: GitLabAPI,
):
sender = data["sender"]["login"]
org = data["organization"]["login"]
repo_url = data["repository"]["url"]
repo_slug = make_repo_slug(data["repository"]["full_name"])
if repo_url.startswith("https://github.com/"):
repo_url = f"https://api.github.com/repos/{data['repository']['full_name']}"
head_sha = data["after"]
Expand All @@ -254,11 +267,14 @@ async def handle_push(
data["repository"]["full_name"],
)

await cancel_pipelines_if_redundant(gl=gl, head_ref=data["ref"], clone_url=data["repository"]["clone_url"])
await cancel_pipelines_if_redundant(
gl=gl, head_ref=data["ref"], clone_url=data["repository"]["clone_url"]
)

await trigger_pipeline(
gh,
repo_url=repo_url,
repo_slug=repo_slug,
head_sha=head_sha,
session=session,
clone_url=data["repository"]["clone_url"],
Expand Down Expand Up @@ -352,21 +368,41 @@ async def get_author_in_team(gh: GitHubAPI, author: str, org: str) -> bool:

return False


async def cancel_pipelines_if_redundant(gl: GitLabAPI, head_ref: str, clone_url: str):
logger.debug("Checking for redundant pipelines")
for scope in ["running", "pending"]:
async for pipeline in gl.getiter(f"/projects/{config.GITLAB_PROJECT_ID}/pipelines", {"scope": scope}):
async for pipeline in gl.getiter(
f"/projects/{config.GITLAB_PROJECT_ID}/pipelines", {"scope": scope}
):
variables = {}
for item in await gl.getitem(f"/projects/{config.GITLAB_PROJECT_ID}/pipelines/{pipeline['id']}/variables"):
for item in await gl.getitem(
f"/projects/{config.GITLAB_PROJECT_ID}/pipelines/{pipeline['id']}/variables"
):
variables[item["key"]] = item["value"]

if variables["HEAD_REF"] == head_ref and variables["CLONE_URL"] == clone_url:
logger.debug("Cancel pipeline %d for %s on %s", pipeline["id"], head_ref, clone_url)
if (
variables["HEAD_REF"] == head_ref
and variables["CLONE_URL"] == clone_url
):
logger.debug(
"Cancel pipeline %d for %s on %s",
pipeline["id"],
head_ref,
clone_url,
)
if not config.STERILE:
await gl.post(f"/projects/{config.GITLAB_PROJECT_ID}/pipelines/{pipeline['id']}/cancel", data=None)
await gl.post(
f"/projects/{config.GITLAB_PROJECT_ID}/pipelines/{pipeline['id']}/cancel",
data=None,
)


async def handle_synchronize(
gh: GitHubAPI, session: aiohttp.ClientSession, data: Mapping[str, Any], gl: GitLabAPI
gh: GitHubAPI,
session: aiohttp.ClientSession,
data: Mapping[str, Any],
gl: GitLabAPI,
):
pr = data["pull_request"]

Expand All @@ -382,6 +418,7 @@ async def handle_synchronize(
logger.debug("Allow team is: %s", config.ALLOW_TEAM)

repo_url = pr["base"]["repo"]["url"]
repo_slug = make_repo_slug(pr["base"]["repo"]["full_name"])
head_sha = pr["head"]["sha"]

for login, label in [(author, "author"), (source_repo_login, "source repo login")]:
Expand All @@ -398,13 +435,16 @@ async def handle_synchronize(

logger.debug("%s is in team", label)

await cancel_pipelines_if_redundant(gl=gl, head_ref=pr["head"]["ref"], clone_url=pr["head"]["repo"]["clone_url"])
await cancel_pipelines_if_redundant(
gl=gl, head_ref=pr["head"]["ref"], clone_url=pr["head"]["repo"]["clone_url"]
)

await trigger_pipeline(
gh,
session,
head_sha=head_sha,
repo_url=repo_url,
repo_slug=repo_slug,
clone_url=pr["head"]["repo"]["clone_url"],
installation_id=data["installation"]["id"],
head_ref=pr["head"]["ref"],
Expand All @@ -416,6 +456,7 @@ async def trigger_pipeline(
session,
head_sha: str,
repo_url: str,
repo_slug: str,
installation_id: int,
clone_url: str,
head_ref: str,
Expand All @@ -432,6 +473,7 @@ async def trigger_pipeline(
data = {
"installation_id": installation_id,
"repo_url": repo_url,
"repo_slug": repo_slug,
"head_sha": head_sha,
"config_url": ci_config_file["download_url"],
"clone_url": clone_url,
Expand Down Expand Up @@ -464,14 +506,17 @@ async def trigger_pipeline(
message = "Unknown error"
try:
message = info["message"]["base"]
except KeyError:
except KeyError:
pass
logger.debug("Pipeline was not created: %s", message)
await add_failure_status(gh, head_sha=head_sha, repo_url=repo_url, message=message)
await add_failure_status(
gh, head_sha=head_sha, repo_url=repo_url, message=message
)
else:
resp.raise_for_status()
logger.debug("Triggered pipeline on gitlab")


def gitlab_to_github_status(gitlab_status: str) -> str:
if gitlab_status in (
"created",
Expand Down

0 comments on commit a1497e8

Please sign in to comment.