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.<domain>.
This commit is contained in:
@@ -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.<domain>``. 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]:
|
||||
|
||||
38
tests/api/test_gitea_client_base.py
Normal file
38
tests/api/test_gitea_client_base.py
Normal file
@@ -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"
|
||||
Reference in New Issue
Block a user