From ccae75c805c53ecedd859d18b1656c302b90c7f2 Mon Sep 17 00:00:00 2001 From: kert Date: Wed, 11 Nov 2026 13:19:30 -0500 Subject: [PATCH] fix(api): Gitea client falls back to the public URL off the compose net GiteaClient defaulted to cfg.services.git (http://git:3000), which only resolves inside the compose network. The host-side bib-refresh-iom timer's IOM alert step died on it every night (and before Nov 11 it was also writing a worktree's bib). Use GITEA_API_URL when set, the internal service when its name resolves, else https://git.. --- src/api/clients/gitea/client.py | 27 ++++++++++++++++---- tests/api/test_gitea_client_base.py | 38 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/api/test_gitea_client_base.py diff --git a/src/api/clients/gitea/client.py b/src/api/clients/gitea/client.py index 038483f..bdec7ca 100644 --- a/src/api/clients/gitea/client.py +++ b/src/api/clients/gitea/client.py @@ -5,6 +5,27 @@ from __future__ import annotations from api.clients.base import Client +def default_base_url() -> str: + """``GITEA_API_URL`` when set; else the compose service + (``cfg.services.git``) when its name resolves — inside the compose + network — else the public ``https://git.``. Host-side jobs + (the nightly bib-refresh-iom timer) cannot resolve ``git``.""" + import os + import socket + from urllib.parse import urlsplit + + from conf import cfg + + if os.environ.get("GITEA_API_URL"): + return os.environ["GITEA_API_URL"].rstrip("/") + internal = f"{cfg.services.git}/api/v1" + try: + socket.getaddrinfo(urlsplit(internal).hostname or "", None) + except OSError: + return f"https://git.{cfg.platform.domain}/api/v1" + return internal + + class GiteaClient(Client): """Client for the Gitea REST API v1. @@ -23,11 +44,7 @@ class GiteaClient(Client): base_url: str = "", **kw, ) -> None: - if not base_url: - from conf import cfg - - base_url = f"{cfg.services.git}/api/v1" - super().__init__(base_url, **kw) + super().__init__(base_url or default_base_url(), **kw) self._token = token def _default_headers(self) -> dict[str, str]: diff --git a/tests/api/test_gitea_client_base.py b/tests/api/test_gitea_client_base.py new file mode 100644 index 0000000..9d10695 --- /dev/null +++ b/tests/api/test_gitea_client_base.py @@ -0,0 +1,38 @@ +"""GiteaClient picks an API base that resolves where it runs.""" + +from __future__ import annotations + +import socket + +import pytest + +from api.clients.gitea.client import GiteaClient + + +@pytest.fixture(autouse=True) +def _no_env(monkeypatch): + monkeypatch.delenv("GITEA_API_URL", raising=False) + + +def test_inside_compose_uses_the_internal_service(monkeypatch): + monkeypatch.setattr(socket, "getaddrinfo", lambda *a, **k: [("ok",)]) + assert GiteaClient("t").base_url == "http://git:3000/api/v1" + + +def test_on_the_host_falls_back_to_the_public_url(monkeypatch): + # The nightly bib-refresh-iom timer runs on the host, where the + # compose name "git" does not resolve; it failed every night. + def nxdomain(*a, **k): + raise socket.gaierror(-2, "Name or service not known") + + monkeypatch.setattr(socket, "getaddrinfo", nxdomain) + assert GiteaClient("t").base_url == "https://git.fhirworx.io/api/v1" + + +def test_env_override_wins(monkeypatch): + monkeypatch.setenv("GITEA_API_URL", "https://example.test/api/v1/") + assert GiteaClient("t").base_url == "https://example.test/api/v1" + + +def test_explicit_base_url_is_untouched(): + assert GiteaClient("t", base_url="http://x/api/v1").base_url == "http://x/api/v1"