fix(opax): replace cloudbuild_v1 import with REST API in /opax/build-status

This commit is contained in:
chrischristiansen-glitch 2026-06-13 15:09:22 +02:00
parent a070768707
commit d25caf114d

44
main.py
View File

@ -12,7 +12,7 @@ CG5c: POST /notify/sms — Twilio SMS, Guard+-tier, spike-varsler.
CG5-onboard: GET /onboard + POST /onboard/complete klient-onboarding wizard. CG5-onboard: GET /onboard + POST /onboard/complete klient-onboarding wizard.
DOCS: GET /docs/{path} proxy til privat GitHub-repo via ADC/PAT. DOCS: GET /docs/{path} proxy til privat GitHub-repo via ADC/PAT.
OQ-29: GET /notify/channels list konfigurerte delivery-kanaler (env-var-basert). OQ-29: GET /notify/channels list konfigurerte delivery-kanaler (env-var-basert).
OQ-31: GET /opax/build-status ekte Cloud Build-status. OQ-31: GET /opax/build-status ekte Cloud Build-status via REST API.
CG4-credits: GET /billing/credits kreditt-saldo, burn rate og tom-dato. CG4-credits: GET /billing/credits kreditt-saldo, burn rate og tom-dato.
Krever GOOGLE_CREDIT_TOTAL_USD i Cloud Run env for runway-beregning. Krever GOOGLE_CREDIT_TOTAL_USD i Cloud Run env for runway-beregning.
""" """
@ -31,6 +31,8 @@ from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any from typing import List, Optional, Dict, Any
import datetime import datetime
import httpx import httpx
import google.auth
import google.auth.transport.requests
from cachetools import cached, TTLCache from cachetools import cached, TTLCache
from agent import run, authorize_mode from agent import run, authorize_mode
@ -174,40 +176,18 @@ async def proxy_doc(path: str):
return Response(content=resp.text, media_type="text/plain; charset=utf-8") return Response(content=resp.text, media_type="text/plain; charset=utf-8")
# ── OQ-31: Cloud Build status ───────────────────────────────────────────────── # ── OQ-31: Cloud Build status (REST API) ─────────────────────────────────────
@app.get("/opax/build-status") @app.get("/opax/build-status")
async def build_status(): async def build_status():
try: try:
from google.cloud.devtools import cloudbuild_v1 creds, project = google.auth.default()
client = cloudbuild_v1.CloudBuildClient() creds.refresh(google.auth.transport.requests.Request())
request_obj = cloudbuild_v1.ListBuildsRequest( url = f"https://cloudbuild.googleapis.com/v1/projects/{project}/builds?pageSize=5"
project_id=PROJECT_ID, r = httpx.get(url, headers={"Authorization": f"Bearer {creds.token}"})
filter='trigger_id!=""', builds = r.json().get("builds", [])
page_size=5, return {"builds": [{"id": b["id"], "status": b["status"],
) "branch": b.get("substitutions", {}).get("BRANCH_NAME", ""),
builds = list(client.list_builds(request=request_obj)) "createTime": b["createTime"]} for b in builds]}
if not builds:
return JSONResponse({"status": "unknown", "message": "Ingen builds funnet"})
b = builds[0]
status_map = {
1: "queued", 2: "working", 3: "success",
4: "failure", 5: "internal_error", 6: "timeout", 7: "cancelled"
}
status_str = status_map.get(int(b.status), "unknown")
duration_s = None
if b.start_time and b.finish_time:
duration_s = int(b.finish_time.seconds - b.start_time.seconds)
return JSONResponse({
"status": status_str,
"build_id": b.id,
"trigger_id": b.build_trigger_id or "",
"branch": (b.substitutions or {}).get("BRANCH_NAME", "main"),
"commit": (b.substitutions or {}).get("SHORT_SHA", ""),
"duration_s": duration_s,
"start_time": str(b.start_time) if b.start_time else None,
"finish_time": str(b.finish_time) if b.finish_time else None,
"log_url": b.log_url or "",
})
except Exception as e: except Exception as e:
import logging import logging
logging.getLogger(__name__).error(f"[/opax/build-status] Failed: {e}", exc_info=True) logging.getLogger(__name__).error(f"[/opax/build-status] Failed: {e}", exc_info=True)