fix(CI6): robust datetime-parsing for Cloud Build REST tidsstempel
This commit is contained in:
parent
be2e3b4583
commit
c8cc45318f
|
|
@ -34,7 +34,6 @@ def _get_access_token() -> str:
|
|||
|
||||
|
||||
def _list_builds_rest(project_id: str, region: str, trigger_id: str, page_size: int = 1):
|
||||
"""Kaller Cloud Build v1 REST direkte mot regional endpoint."""
|
||||
import urllib.request
|
||||
import json
|
||||
token = _get_access_token()
|
||||
|
|
@ -48,6 +47,32 @@ def _list_builds_rest(project_id: str, region: str, trigger_id: str, page_size:
|
|||
return json.loads(resp.read())
|
||||
|
||||
|
||||
def _parse_ts(ts: str):
|
||||
"""Parser RFC3339-tidsstempel robust, returnerer datetime eller None."""
|
||||
from datetime import datetime, timezone
|
||||
if not ts:
|
||||
return None
|
||||
ts = ts.rstrip("Z")
|
||||
# Fjern eventuell timezone-offset (+00:00) og mikrosekunder
|
||||
for fmt in (
|
||||
"%Y-%m-%dT%H:%M:%S.%f",
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
):
|
||||
try:
|
||||
# Klipp bort offset-del før parsing
|
||||
clean = ts.split("+")[0].split("-")[:3]
|
||||
# Rekonstruer uten offset
|
||||
clean_ts = ts[:19] # YYYY-MM-DDTHH:MM:SS
|
||||
if "." in ts:
|
||||
frac = ts[20:26].split("+")[0].split("-")[0] if len(ts) > 20 else "000000"
|
||||
clean_ts = ts[:19] + "." + frac.ljust(6, "0")[:6]
|
||||
return datetime.strptime(clean_ts, "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc)
|
||||
return datetime.strptime(clean_ts, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)
|
||||
except Exception:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
class PushStaticRequest(BaseModel):
|
||||
file_path: str
|
||||
content: str
|
||||
|
|
@ -106,10 +131,6 @@ async def push_static(req: PushStaticRequest, _: bool = Depends(verify_token)):
|
|||
|
||||
@app.get("/tools/get_build_status")
|
||||
async def get_build_status(_: bool = Depends(verify_token)):
|
||||
"""
|
||||
Henter siste build fra osvauco-agent-main-trigger via REST mot
|
||||
cloudbuild.googleapis.com/v1/projects/{p}/locations/{r}/builds
|
||||
"""
|
||||
try:
|
||||
data = _list_builds_rest(PROJECT_ID, REGION, BUILD_TRIGGER_ID, page_size=1)
|
||||
builds = data.get("builds", [])
|
||||
|
|
@ -118,18 +139,9 @@ async def get_build_status(_: bool = Depends(verify_token)):
|
|||
b = builds[0]
|
||||
status_str = b.get("status", "unknown").lower()
|
||||
subs = b.get("substitutions", {})
|
||||
start = b.get("startTime")
|
||||
finish = b.get("finishTime")
|
||||
duration_s = None
|
||||
if start and finish:
|
||||
from datetime import datetime, timezone
|
||||
fmt = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||
try:
|
||||
t0 = datetime.strptime(start, fmt).replace(tzinfo=timezone.utc)
|
||||
t1 = datetime.strptime(finish, fmt).replace(tzinfo=timezone.utc)
|
||||
duration_s = int((t1 - t0).total_seconds())
|
||||
except Exception:
|
||||
pass
|
||||
t0 = _parse_ts(b.get("startTime"))
|
||||
t1 = _parse_ts(b.get("finishTime"))
|
||||
duration_s = int((t1 - t0).total_seconds()) if t0 and t1 else None
|
||||
return {
|
||||
"status": status_str,
|
||||
"build_id": b.get("id", ""),
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user