fix(opax): replace cloudbuild_v1 import with REST API in /opax/build-status
This commit is contained in:
parent
a070768707
commit
d25caf114d
44
main.py
44
main.py
|
|
@ -12,7 +12,7 @@ CG5c: POST /notify/sms — Twilio SMS, Guard+-tier, spike-varsler.
|
|||
CG5-onboard: GET /onboard + POST /onboard/complete — klient-onboarding wizard.
|
||||
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-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.
|
||||
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
|
||||
import datetime
|
||||
import httpx
|
||||
import google.auth
|
||||
import google.auth.transport.requests
|
||||
from cachetools import cached, TTLCache
|
||||
|
||||
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")
|
||||
|
||||
|
||||
# ── OQ-31: Cloud Build status ─────────────────────────────────────────────────
|
||||
# ── OQ-31: Cloud Build status (REST API) ─────────────────────────────────────
|
||||
@app.get("/opax/build-status")
|
||||
async def build_status():
|
||||
try:
|
||||
from google.cloud.devtools import cloudbuild_v1
|
||||
client = cloudbuild_v1.CloudBuildClient()
|
||||
request_obj = cloudbuild_v1.ListBuildsRequest(
|
||||
project_id=PROJECT_ID,
|
||||
filter='trigger_id!=""',
|
||||
page_size=5,
|
||||
)
|
||||
builds = list(client.list_builds(request=request_obj))
|
||||
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 "",
|
||||
})
|
||||
creds, project = google.auth.default()
|
||||
creds.refresh(google.auth.transport.requests.Request())
|
||||
url = f"https://cloudbuild.googleapis.com/v1/projects/{project}/builds?pageSize=5"
|
||||
r = httpx.get(url, headers={"Authorization": f"Bearer {creds.token}"})
|
||||
builds = r.json().get("builds", [])
|
||||
return {"builds": [{"id": b["id"], "status": b["status"],
|
||||
"branch": b.get("substitutions", {}).get("BRANCH_NAME", ""),
|
||||
"createTime": b["createTime"]} for b in builds]}
|
||||
except Exception as e:
|
||||
import logging
|
||||
logging.getLogger(__name__).error(f"[/opax/build-status] Failed: {e}", exc_info=True)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user