#!/usr/bin/env python3 """Check TabataGo CI status via Gitea API.""" import urllib.request, json, base64, sys, os REPO = "millianlmx/tabatago" BASE = "https://gitea.1000co.fr/api/v1" TOKEN_FILE = "/opt/data/projects/tabatago/.gtb64" def get_token(): with open(TOKEN_FILE) as f: return base64.b64decode(f.read().strip()).decode() def api(path, expect_json=True): url = f"{BASE}{path}" req = urllib.request.Request(url, headers={"Authorization": f"token {get_token()}"}) resp = urllib.request.urlopen(req) if expect_json: return json.loads(resp.read()) else: return resp.read().decode('utf-8', 'replace') # 1. Get latest runs runs_data = api(f"/repos/{REPO}/actions/runs?limit=3") runs = runs_data['workflow_runs'] for run in runs: rid = run['id'] rn = run['run_number'] status = run['status'] conclusion = run.get('conclusion', 'N/A') commit_msg = run.get('title', '?') print(f"Run #{rn} (id={rid}) status={status} conclusion={conclusion}") print(f" Commit: {commit_msg}") # Get jobs for this run jobs_data = api(f"/repos/{REPO}/actions/runs/{rid}/jobs") jobs = jobs_data if isinstance(jobs_data, list) else jobs_data.get('jobs', []) for j in jobs: jid = j['id'] jname = j['name'] jstatus = j.get('status', '?') jconclusion = j.get('conclusion', 'N/A') print(f" Job: {jname} status={jstatus} conclusion={jconclusion}") if jconclusion == 'failure': # Fetch last portion of logs try: log_text = api(f"/repos/{REPO}/actions/jobs/{jid}/logs", expect_json=False) lines = log_text.split('\n') print(f"\n --- LOG TAIL ({len(lines)} total lines) ---") # Find errors error_lines = [l for l in lines if '##[error]' in l or 'error:' in l.lower() or 'Error' in l or 'fatal' in l.lower()] for el in error_lines[-20:]: print(f" {el.strip()[:300]}") # Show last 30 lines for context print(f"\n --- LAST 30 LINES ---") for l in lines[-30:]: print(f" {l.strip()[:300]}") except Exception as e: print(f" Failed to fetch logs for job {jid}: {e}") print() # Summary latest = runs[0] print(f"\nLATEST RUN STATUS: #{latest['run_number']} conclusion={latest.get('conclusion')}") if latest.get('conclusion') == 'success': print("CI IS GREEN!") sys.exit(0) elif latest.get('conclusion') == 'failure': print("CI IS RED - needs fix") sys.exit(1) elif latest.get('status') == 'in_progress': print("CI IS RUNNING - check back later") sys.exit(2) else: print(f"CI STATUS: {latest.get('conclusion')}") sys.exit(3)