lando-client 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lando_client/__init__.py +17 -0
- lando_client/client.py +119 -0
- lando_client/config.py +62 -0
- lando_client-0.1.0.dist-info/METADATA +10 -0
- lando_client-0.1.0.dist-info/RECORD +6 -0
- lando_client-0.1.0.dist-info/WHEEL +4 -0
lando_client/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from lando_client.client import (
|
|
2
|
+
PATCH_FORMAT_GIT,
|
|
3
|
+
PATCH_FORMAT_HG,
|
|
4
|
+
LandoAPIError,
|
|
5
|
+
LandoClient,
|
|
6
|
+
encode_patch,
|
|
7
|
+
)
|
|
8
|
+
from lando_client.config import LandoSettings
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"LandoAPIError",
|
|
12
|
+
"LandoClient",
|
|
13
|
+
"LandoSettings",
|
|
14
|
+
"PATCH_FORMAT_GIT",
|
|
15
|
+
"PATCH_FORMAT_HG",
|
|
16
|
+
"encode_patch",
|
|
17
|
+
]
|
lando_client/client.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Small shared Lando API client."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from lando_client.config import LandoSettings
|
|
8
|
+
|
|
9
|
+
TREEHERDER_URL = "https://treeherder.mozilla.org"
|
|
10
|
+
|
|
11
|
+
# The patch formats Lando accepts (`PatchFormat` in lando.main.scm.helpers).
|
|
12
|
+
PATCH_FORMAT_GIT = "git-format-patch"
|
|
13
|
+
PATCH_FORMAT_HG = "hgexport"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class LandoAPIError(Exception):
|
|
17
|
+
"""Lando rejected a request (or answered with something unusable)."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def encode_patch(patch: bytes) -> str:
|
|
21
|
+
"""Base64-encode one patch for the ``patches`` array."""
|
|
22
|
+
return base64.b64encode(patch).decode("ascii")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LandoClient:
|
|
26
|
+
def __init__(self, settings: LandoSettings | None = None) -> None:
|
|
27
|
+
self.settings = settings or LandoSettings.from_env()
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def base_url(self) -> str:
|
|
31
|
+
return self.settings.url.rstrip("/")
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def try_patches_url(self) -> str:
|
|
35
|
+
return f"{self.base_url}/api/try/patches"
|
|
36
|
+
|
|
37
|
+
def job_url(self, job_id: int) -> str:
|
|
38
|
+
"""Lando's own page for a landing job (its live status)."""
|
|
39
|
+
return f"{self.base_url}/landings/{job_id}"
|
|
40
|
+
|
|
41
|
+
def treeherder_url(self, job_id: int, repo_name: str = "try") -> str:
|
|
42
|
+
"""Treeherder's view of a Lando try job.
|
|
43
|
+
|
|
44
|
+
Keyed by ``landoCommitID`` rather than a revision because the push has
|
|
45
|
+
no revision yet: Lando applies the patches asynchronously, so this URL
|
|
46
|
+
is valid (if initially empty) the moment the job is created.
|
|
47
|
+
"""
|
|
48
|
+
return (
|
|
49
|
+
f"{TREEHERDER_URL}/jobs?repo={repo_name}"
|
|
50
|
+
f"&landoInstance={self.settings.instance_id}"
|
|
51
|
+
f"&landoCommitID={job_id}"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
async def submit_try_patches(
|
|
55
|
+
self,
|
|
56
|
+
patches: list[str],
|
|
57
|
+
base_commit: str,
|
|
58
|
+
*,
|
|
59
|
+
base_commit_vcs: str = "git",
|
|
60
|
+
patch_format: str = PATCH_FORMAT_GIT,
|
|
61
|
+
repo_name: str = "try",
|
|
62
|
+
) -> int:
|
|
63
|
+
"""Submit a base64-encoded patch series to a Try repo; return the job id.
|
|
64
|
+
|
|
65
|
+
``patches`` are applied in order on top of ``base_commit``, which must be
|
|
66
|
+
a full 40-character hash of a *published* commit (Lando maps it to the
|
|
67
|
+
try repo's own SCM when ``base_commit_vcs`` differs, so a firefox git
|
|
68
|
+
sha is fine for the Mercurial ``try``).
|
|
69
|
+
"""
|
|
70
|
+
payload = {
|
|
71
|
+
"repo_name": repo_name,
|
|
72
|
+
"base_commit": base_commit,
|
|
73
|
+
"base_commit_vcs": base_commit_vcs,
|
|
74
|
+
"patch_format": patch_format,
|
|
75
|
+
"patches": patches,
|
|
76
|
+
}
|
|
77
|
+
async with httpx.AsyncClient(timeout=self.settings.timeout_seconds) as client:
|
|
78
|
+
response = await client.post(
|
|
79
|
+
self.try_patches_url,
|
|
80
|
+
json=payload,
|
|
81
|
+
headers={
|
|
82
|
+
"Authorization": f"Bearer {self.settings.access_token}",
|
|
83
|
+
"User-Agent": self.settings.user_agent,
|
|
84
|
+
},
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
if response.status_code >= 400:
|
|
88
|
+
raise LandoAPIError(_error_detail(response))
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
return int(response.json()["id"])
|
|
92
|
+
except (ValueError, KeyError, TypeError) as exc:
|
|
93
|
+
raise LandoAPIError(
|
|
94
|
+
f"Lando accepted the push ({response.status_code}) but returned no "
|
|
95
|
+
"job id"
|
|
96
|
+
) from exc
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _error_detail(response: httpx.Response) -> str:
|
|
100
|
+
"""A readable message for a failed Lando response.
|
|
101
|
+
|
|
102
|
+
Lando reports errors as RFC 7807 problem details (``title``/``detail``),
|
|
103
|
+
but a proxy or a 5xx can still answer with HTML, so fall back to the status
|
|
104
|
+
line rather than raising a ``JSONDecodeError`` over the real failure.
|
|
105
|
+
"""
|
|
106
|
+
try:
|
|
107
|
+
problem = response.json()
|
|
108
|
+
except ValueError:
|
|
109
|
+
problem = None
|
|
110
|
+
|
|
111
|
+
if isinstance(problem, dict):
|
|
112
|
+
parts = [str(problem[key]) for key in ("title", "detail") if problem.get(key)]
|
|
113
|
+
if parts:
|
|
114
|
+
return f"Lando returned HTTP {response.status_code}: {': '.join(parts)}"
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
f"Lando returned HTTP {response.status_code} ({response.reason_phrase}) "
|
|
118
|
+
f"for {response.request.url}"
|
|
119
|
+
)
|
lando_client/config.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Configuration for :class:`LandoClient`."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
from urllib.parse import urlsplit
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, Field, model_validator
|
|
7
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
8
|
+
|
|
9
|
+
# Lando deployment names, keyed by the host they serve. This pairing is not
|
|
10
|
+
# something Lando exposes over its API: `instance_id` is a *client-side* label,
|
|
11
|
+
# declared in mozilla-central's `.lando.ini` (one `instance_id`/`api_domain`
|
|
12
|
+
# pair per section) and mapped back to a host by Treeherder itself.
|
|
13
|
+
INSTANCE_IDS_BY_HOST = {
|
|
14
|
+
"lando.moz.tools": "lando-prod-2025",
|
|
15
|
+
"lando-dev.allizom.org": "lando-dev-2025",
|
|
16
|
+
"api.lando.services.mozilla.com": "lando-prod",
|
|
17
|
+
"api.dev.lando.nonprod.cloudops.mozgcp.net": "lando-dev",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class LandoSettings(BaseModel):
|
|
22
|
+
"""Where Lando lives and how to authenticate against it.
|
|
23
|
+
|
|
24
|
+
``access_token`` is an OIDC bearer token for the account that owns the
|
|
25
|
+
pushes: Lando's Try endpoint authenticates the *user* (it has no API-key
|
|
26
|
+
mode), and the try repository's permissions are checked against that user.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
access_token: Annotated[str, Field(min_length=1)]
|
|
30
|
+
url: str = "https://lando.moz.tools"
|
|
31
|
+
instance_id: str | None = None
|
|
32
|
+
timeout_seconds: int = 60
|
|
33
|
+
user_agent: str = "Lando-User/Hackbot"
|
|
34
|
+
|
|
35
|
+
@model_validator(mode="after")
|
|
36
|
+
def _resolve_instance_id(self) -> LandoSettings:
|
|
37
|
+
host = urlsplit(self.url).hostname
|
|
38
|
+
resolved = INSTANCE_IDS_BY_HOST.get(host)
|
|
39
|
+
if not resolved and not self.instance_id:
|
|
40
|
+
known_hosts = ", ".join(sorted(INSTANCE_IDS_BY_HOST))
|
|
41
|
+
raise ValueError(
|
|
42
|
+
f"Unknown Lando host {host!r}: cannot tell which deployment "
|
|
43
|
+
"Treeherder should link to. Set LANDO_INSTANCE_ID explicitly "
|
|
44
|
+
f"(known hosts: {known_hosts})."
|
|
45
|
+
)
|
|
46
|
+
if resolved and self.instance_id and self.instance_id != resolved:
|
|
47
|
+
raise ValueError(
|
|
48
|
+
f"LANDO_INSTANCE_ID {self.instance_id!r} does not match the "
|
|
49
|
+
f"known deployment for {host!r} ({resolved!r})."
|
|
50
|
+
)
|
|
51
|
+
if resolved and not self.instance_id:
|
|
52
|
+
self.instance_id = resolved
|
|
53
|
+
|
|
54
|
+
return self
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def from_env(cls) -> LandoSettings:
|
|
58
|
+
return _LandoEnvSettings()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class _LandoEnvSettings(LandoSettings, BaseSettings):
|
|
62
|
+
model_config = SettingsConfigDict(env_prefix="LANDO_", extra="ignore")
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lando-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Small shared Lando API client (httpx-based)
|
|
5
|
+
Requires-Python: >=3.14
|
|
6
|
+
Requires-Dist: httpx>=0.26.0
|
|
7
|
+
Requires-Dist: pydantic-settings>=2.1.0
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
10
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
lando_client/__init__.py,sha256=JHHdxBHz5c57K713JpsUDeGZXlP07Lhkt5Y4LrszpZs,322
|
|
2
|
+
lando_client/client.py,sha256=tuKh05eFLVsqux_7Qz4N4k3CIdT-TpUjf1W2kQ4LkT0,4054
|
|
3
|
+
lando_client/config.py,sha256=K62aaQyN-mF38dqnEz3rSSf_Tq8OvUaC0aUrd3nnE0c,2441
|
|
4
|
+
lando_client-0.1.0.dist-info/METADATA,sha256=x4jlF1dbi8aAqLZAfw4G_Jihyig0A1xsP_KUS7gReA4,321
|
|
5
|
+
lando_client-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
6
|
+
lando_client-0.1.0.dist-info/RECORD,,
|