[skip ci] fix ots
This commit is contained in:
3 files changed
+107
-6
No files matched your search
@@ -19,7 +19,7 @@ import tempfile
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, replace
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -139,8 +139,45 @@ def _artifact(raw: Any) -> Artifact | None:
|
||||
)
|
||||
|
||||
|
||||
def _run(client: httpx.Client, settings: EnvSettings, run_id: int) -> dict[str, Any]:
|
||||
try:
|
||||
response = client.get(_api(settings, f"/actions/runs/{run_id}"))
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
except (httpx.HTTPError, ValueError) as exc:
|
||||
raise UpdateUnavailable(f"Could not read Gitea run {run_id}: {exc}") from exc
|
||||
return payload if isinstance(payload, dict) else {}
|
||||
|
||||
|
||||
def _with_run(
|
||||
client: httpx.Client,
|
||||
settings: EnvSettings,
|
||||
artifact: Artifact,
|
||||
runs: dict[int, dict[str, Any]],
|
||||
) -> Artifact:
|
||||
"""Gitea's artifact list can omit the run's branch, number and conclusion."""
|
||||
if artifact.run_number and artifact.branch and artifact.conclusion:
|
||||
return artifact
|
||||
if not artifact.run_id:
|
||||
return artifact
|
||||
run = runs.get(artifact.run_id)
|
||||
if run is None:
|
||||
run = _run(client, settings, artifact.run_id)
|
||||
runs[artifact.run_id] = run
|
||||
return replace(
|
||||
artifact,
|
||||
run_number=int(run.get("run_number") or artifact.run_number),
|
||||
branch=str(run.get("head_branch") or artifact.branch),
|
||||
commit=str(run.get("head_sha") or artifact.commit),
|
||||
conclusion=str(run.get("conclusion") or artifact.conclusion),
|
||||
)
|
||||
|
||||
|
||||
def _list_artifacts(
|
||||
client: httpx.Client, settings: EnvSettings, name: str
|
||||
client: httpx.Client,
|
||||
settings: EnvSettings,
|
||||
name: str,
|
||||
runs: dict[int, dict[str, Any]] | None = None,
|
||||
) -> list[Artifact]:
|
||||
try:
|
||||
response = client.get(_api(settings, "/actions/artifacts"), params={"name": name})
|
||||
@@ -152,7 +189,9 @@ def _list_artifacts(
|
||||
entries = payload.get("artifacts") if isinstance(payload, dict) else None
|
||||
if not isinstance(entries, list):
|
||||
return []
|
||||
return [artifact for raw in entries if (artifact := _artifact(raw)) is not None]
|
||||
artifacts = [artifact for raw in entries if (artifact := _artifact(raw)) is not None]
|
||||
run_cache = runs if runs is not None else {}
|
||||
return [_with_run(client, settings, artifact, run_cache) for artifact in artifacts]
|
||||
|
||||
|
||||
def _successful(artifacts: list[Artifact], branch: str) -> list[Artifact]:
|
||||
@@ -268,12 +307,13 @@ def _resolve(settings: EnvSettings) -> ResolvedUpdate:
|
||||
if cached is not None and now - cached[0] < _METADATA_TTL_SECONDS:
|
||||
return cached[1]
|
||||
|
||||
runs: dict[int, dict[str, Any]] = {}
|
||||
with _client(settings) as client:
|
||||
metadata_artifacts = _successful(
|
||||
_list_artifacts(client, settings, METADATA_ARTIFACT),
|
||||
_list_artifacts(client, settings, METADATA_ARTIFACT, runs),
|
||||
settings.gitea_update_branch,
|
||||
)
|
||||
apk_artifacts = _list_artifacts(client, settings, APK_ARTIFACT)
|
||||
apk_artifacts = _list_artifacts(client, settings, APK_ARTIFACT, runs)
|
||||
usable_apk = _successful(apk_artifacts, settings.gitea_update_branch)
|
||||
|
||||
resolved: ResolvedUpdate | None = None
|
||||
|
||||
Reference in new issue
Block a user