|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import time |
| 5 | +from typing import Annotated |
| 6 | + |
| 7 | +import git |
| 8 | +import koji |
| 9 | +from fastmcp.exceptions import ToolError |
| 10 | +from pydantic import Field |
| 11 | + |
| 12 | +from common.constants import BREWHUB_URL |
| 13 | +from common.utils import init_kerberos_ticket, KerberosError |
| 14 | + |
| 15 | +SYNC_TIMEOUT = 1 * 60 * 60 # seconds |
| 16 | + |
| 17 | + |
| 18 | +async def create_zstream_branch( |
| 19 | + package: Annotated[str, Field(description="Package name")], |
| 20 | + branch: Annotated[str, Field(description="Name of the branch to create")] |
| 21 | +) -> str: |
| 22 | + """ |
| 23 | + Creates a new Z-Stream branch for the specified package in internal dist-git. |
| 24 | + """ |
| 25 | + try: |
| 26 | + principal = await init_kerberos_ticket() |
| 27 | + except KerberosError as e: |
| 28 | + raise ToolError(f"Failed to initialize Kerberos ticket: {e}") from e |
| 29 | + username = principal.split("@", maxsplit=1)[0] |
| 30 | + try: |
| 31 | + with tempfile.TemporaryDirectory() as path: |
| 32 | + repo = await asyncio.to_thread( |
| 33 | + git.Repo.clone_from, |
| 34 | + f"ssh://{username}@pkgs.devel.redhat.com/rpms/{package}", |
| 35 | + path, |
| 36 | + ) |
| 37 | + if branch in [ref.name.split("/")[-1] for ref in repo.remotes.origin.refs]: |
| 38 | + return f"Z-Stream branch {branch} already exists, no need to create it" |
| 39 | + # find the correct base for our new branch: |
| 40 | + # - get candidate tag corresponding to the branch |
| 41 | + # - get the latest build the candidate tag inherited (from Y-Stream or previous Z-Stream) |
| 42 | + # - get the ref the inherited build originated from |
| 43 | + # - use that as a base for the new branch |
| 44 | + # this allows us to create Z-Stream branches even if a higher Z-Stream branch already exists |
| 45 | + session = koji.ClientSession(BREWHUB_URL) |
| 46 | + candidate_tag = f"{branch}-candidate" |
| 47 | + builds = await asyncio.to_thread( |
| 48 | + session.listTagged, |
| 49 | + package=package, |
| 50 | + tag=candidate_tag, |
| 51 | + latest=True, |
| 52 | + inherit=True, |
| 53 | + strict=True, |
| 54 | + ) |
| 55 | + if not builds: |
| 56 | + raise RuntimeError(f"There are no builds of {package} in {candidate_tag}") |
| 57 | + [build] = builds |
| 58 | + metadata = await asyncio.to_thread(session.getBuild, build["build_id"], strict=True) |
| 59 | + ref = metadata["source"].split("#")[-1] |
| 60 | + await asyncio.to_thread(repo.remotes.origin.push, f"{ref}:refs/heads/{branch}") |
| 61 | + # wait until the new branch is synced to GitLab |
| 62 | + token = os.environ["GITLAB_TOKEN"] |
| 63 | + start_time = time.monotonic() |
| 64 | + while time.monotonic() - start_time < SYNC_TIMEOUT: |
| 65 | + if await asyncio.to_thread( |
| 66 | + repo.git.ls_remote, |
| 67 | + f"https://oauth2:{token}@gitlab.com/redhat/rhel/rpms/{package}", |
| 68 | + branch, |
| 69 | + branches=True, |
| 70 | + ): |
| 71 | + return f"Succesfully created Z-Stream branch {branch}" |
| 72 | + await asyncio.sleep(30) |
| 73 | + raise RuntimeError(f"The {branch} branch wasn't synced to GitLab after {SYNC_TIMEOUT} seconds") |
| 74 | + except Exception as e: |
| 75 | + raise ToolError(f"Failed to create Z-Stream branch: {e}") from e |
0 commit comments