forge-tools/frg/context.py

72 lines
1.8 KiB
Python
Raw Permalink Normal View History

"""
Repo/git-aware contextual data
This module takes care of anything that needs to be pulled from the
invocation environment (i.e. git context, ...) to make things work.
"""
2024-11-02 22:22:43 +00:00
import logging
import pydantic
2024-11-04 01:59:35 +00:00
import frg.git as git
2024-11-02 22:22:43 +00:00
logger = logging.getLogger(__name__)
class GitContext(pydantic.BaseModel):
"""
Contextual data based on where the invocation
to the tool is made.
"""
repo_name: str
repo_author: str
current_branch: str
host: str
def parse_remote_url(remote_url: str) -> tuple[str, str, str]:
"""Parses the remote URL attached to a repository to detect the host, repo owner and name."""
if "@" in remote_url:
_, url = remote_url.removesuffix(".git").split("@")
host, full_repo_name = url.split(":")
owner, repo = full_repo_name.split("/")
return ("https://" + host, owner, repo)
if remote_url.startswith("http"):
url = (
remote_url.removeprefix("http://")
.removeprefix("https://")
.removesuffix(".git")
)
url_parts = url.split("/")
repo = url_parts[-1]
owner = url_parts[-2]
host = "https://" + "/".join(url_parts[:-2])
return (host, owner, repo)
return ("", "", "")
def get_git_context(*, domain_aliases: dict[str, str] | None = None) -> GitContext:
2024-11-02 22:22:43 +00:00
"""Gathers contextual data from the environment."""
if not domain_aliases:
domain_aliases = dict()
2024-11-04 01:59:35 +00:00
current_branch = git.get_current_branch().stdout
remote_url = git.get_current_remote_url().stdout
host, owner, repo = parse_remote_url(remote_url)
if host in domain_aliases:
2024-11-02 22:22:43 +00:00
logger.info(f"Remapped domain: {host} -> {domain_aliases[host]}")
host = domain_aliases[host]
return GitContext(
repo_name=repo, repo_author=owner, host=host, current_branch=current_branch
)