Migrate to uvicorn: Flask/SocketIO → FastAPI + native WebSocket (#53)

* Initial plan

* Migrate from Flask/SocketIO to FastAPI/uvicorn with native WebSocket support

Co-authored-by: rudolf1 <5525769+rudolf1@users.noreply.github.com>

* Consolidate three WebSocket endpoints into single /ws connection

Co-authored-by: rudolf1 <5525769+rudolf1@users.noreply.github.com>

* Replace event/data envelope with named message fields

Co-authored-by: rudolf1 <5525769+rudolf1@users.noreply.github.com>

* Fix bad merge: remove duplicate lines in app.py and restore forEach loop in app.js

Agent-Logs-Url: https://github.com/rudolf1/brencher/sessions/ac246b64-cf2e-4cd3-ab28-5cd9caf28944

Co-authored-by: rudolf1 <5525769+rudolf1@users.noreply.github.com>

* x

* x

* x

* x

* x

* x

* x

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rudolf1 <5525769+rudolf1@users.noreply.github.com>
Co-authored-by: rudolf <rudolfss13@gmail.com>
This commit is contained in:
Copilot
2026-03-31 16:48:03 +02:00
committed by GitHub
co-authored by rudolf1 admin
parent 313c34424a
commit 694283e7c4
7 changed files with 322 additions and 276 deletions
+2 -2
View File
@@ -18,12 +18,12 @@ RUN uv venv .venv && \
COPY backend/ ./backend/
COPY frontend/ ./frontend/
# Expose port for Flask
# Expose port for the web server
EXPOSE 5001
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PATH="/app/.venv/bin:$PATH"
# Start the Flask app
# Start the app with uvicorn
CMD ["python3", "backend/app.py"]
+199 -162
View File
@@ -1,29 +1,28 @@
import asyncio
import json
import logging
import os
import signal
import sys
import threading
import time
import traceback
import types
from dataclasses import asdict, replace
from typing import List, Dict, Any, Optional
from typing import TypeVar
from typing import Any, Dict, List, Optional, Set, Tuple, TypeVar
import socketio as socketio_client
import websockets
from dotenv import load_dotenv
from flask import Flask, send_from_directory
from flask.json.provider import DefaultJSONProvider
from flask_socketio import SocketIO, emit
from fastapi import FastAPI, Response, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from configs.gmail_mcp import gmail_mcp
from enironment import Environment, wrap_in_cached
from enironment import AbstractStep, Environment, wrap_in_cached
from processing import reset_caches
from steps.git import GitClone
from steps.step import CachingStep
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.getLogger('werkzeug').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Load environment variables from .env file
@@ -43,44 +42,68 @@ class DataclassJSONEncoder(json.JSONEncoder):
return str(o)
custom_json = types.SimpleNamespace()
custom_json.dumps = lambda obj, **kwargs: json.dumps(obj, cls=DataclassJSONEncoder, **kwargs)
custom_json.loads = json.loads
def custom_json_dumps(obj: Any) -> str:
return json.dumps(obj, cls=DataclassJSONEncoder)
class DataclassJSONProvider(DefaultJSONProvider):
def dumps(self, obj: Any, **kwargs: Any) -> str:
return json.dumps(obj, cls=DataclassJSONEncoder, **kwargs)
app = FastAPI()
def loads(self, s: str | bytes, **kwargs: Any) -> Any:
return json.loads(s, **kwargs)
app = Flask(__name__, static_folder='frontend')
app.json = DataclassJSONProvider(app)
socketio = SocketIO(app, cors_allowed_origins="*", json=custom_json)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
FRONTEND_DIR = os.path.join(os.path.dirname(__file__), '../frontend')
# In-memory state
environments: Dict[str, Environment] = {}
environments_slaves: Dict[str, Environment] = {}
environments_slaves: Dict[str, Any] = {}
branches_slaves: Dict[str, Dict[str, Any]] = {}
state_lock = threading.Lock()
# Single set of WebSocket connections
ws_connections: Set[WebSocket] = set()
@app.route('/')
def serve_index() -> Any:
return send_from_directory(FRONTEND_DIR, 'index.html')
# Event loop reference for thread-safe async calls
_event_loop: Optional[asyncio.AbstractEventLoop] = None
# Slave connection state
slave_ws_task: Optional[asyncio.Task[None]] = None
slave_send_queue: asyncio.Queue[Dict[str, Any]] = asyncio.Queue()
slave_url = os.getenv('SLAVE_BRENCHER')
environment_update_event = threading.Event()
@app.route('/<path:path>')
def serve_static(path: str) -> Any:
return send_from_directory(FRONTEND_DIR, path)
# --- Static File Serving ---
@app.get("/")
async def serve_index() -> FileResponse:
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
# --- WebSocket Endpoints ---
from flask_socketio import Namespace
@app.get("/state")
async def serve_state() -> Response:
return Response(content=custom_json_dumps(get_global_envs_to_emit()), media_type="application/json")
@app.get("/branches")
async def serve_branches_route() -> Response:
return Response(content=custom_json_dumps(get_global_branches_to_emit()), media_type="application/json")
@app.get("/{path:path}")
async def serve_static(path: str) -> FileResponse:
file_path = os.path.join(FRONTEND_DIR, path)
if os.path.exists(file_path) and os.path.isfile(file_path):
return FileResponse(file_path)
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
# --- Utility Functions ---
T = TypeVar('T')
@@ -101,60 +124,6 @@ def merge_dicts(a: Dict[str, T], b: Dict[str, T]) -> Dict[str, T]:
return result
slave_url = os.getenv('SLAVE_BRENCHER')
remote_sio = None
if slave_url:
logger.info(f"SLAVE_BRENCHER set, will connect to master at {slave_url}")
remote_sio = socketio_client.Client(logger=False, engineio_logger=False)
# Handlers for events from master -> re-emit locally (marked so we don't loop)
@remote_sio.on('branches', namespace='/ws/branches')
def _remote_branches(data: Any) -> None:
global branches_slaves
branches_slaves = data
try:
socketio.emit('branches', get_global_branches_to_emit(), namespace='/ws/branches')
except Exception as e:
logger.error(f"Error forwarding remote branches locally: {e}")
@remote_sio.on('environments', namespace='/ws/environment')
def _remote_environments(data: Any) -> None:
global environments_slaves
environments_slaves = data
try:
socketio.emit('environments', get_global_envs_to_emit(), namespace='/ws/environment')
except Exception as e:
logger.error(f"Error forwarding remote environments locally: {e}")
@remote_sio.on('error', namespace='/ws/errors')
def _remote_error(data: Any) -> None:
try:
socketio.emit('error', data, namespace='/ws/errors')
except Exception as e:
logger.error(f"Error forwarding remote errors locally: {e}")
@remote_sio.event
def connect() -> None:
logger.info("Connected to master brencher (SLAVE mode).")
@remote_sio.event
def disconnect() -> None:
logger.info("Disconnected from master brencher (SLAVE mode).")
class BranchesNamespace(Namespace):
def on_connect(self, auth: Optional[Any] = None) -> None:
emit('branches', get_global_branches_to_emit())
def on_update(self, data: Any) -> None:
emit('branches', get_global_branches_to_emit())
def get_local_envs_to_emit() -> Dict[str, Dict[str, Any]]:
env_dtos: Dict[str, Dict[str, Any]] = {}
for env in environments.values():
@@ -193,16 +162,14 @@ def get_local_envs_to_emit() -> Dict[str, Dict[str, Any]]:
def get_global_envs_to_emit() -> Any:
global environments, environments_slaves
local_envs = get_local_envs_to_emit()
merge_result = merge_dicts(local_envs, environments_slaves) # type: ignore[misc]
# logger.info(f"Local keys: {local_envs.keys()}")
# logger.info(f"Slave keys: {environments_slaves.keys()}")
merge_result = merge_dicts(local_envs, environments_slaves)
common_keys = set(local_envs.keys()) & set(environments_slaves.keys())
if len(common_keys) > 0:
socketio.emit('error', {'message': f"Conflict: both master and slave have environment with id {common_keys}"},
namespace='/ws/errors')
_schedule_async(broadcast_error({'message': f"Conflict: both master and slave have environment with id {common_keys}"}))
return merge_result
def get_local_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
global environments
branches: Dict[str, Dict[str, List[Any]]] = {}
@@ -215,71 +182,154 @@ def get_local_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
if isinstance(step, CachingStep) and isinstance(step.step, GitClone):
branches[k] = {**step.step.get_branches()}
except BaseException as e:
logger.error(f"Error fetching branches for environment {env.id}: {str(e)}")
stack = traceback.format_exception(type(e), e, e.__traceback__)
logger.error(f"Error fetching branches for environment {env.id}: {str(e)}\n{''.join(stack)}")
return branches
def get_global_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
global branches_slaves
local_branches: Dict[str, Dict[str, List[Any]]] = get_local_branches_to_emit()
return merge_dicts(local_branches, branches_slaves)
environment_update_event = threading.Event()
# --- Async Helper for Calling from Sync Threads ---
def _schedule_async(coro: Any) -> None:
"""Schedule a coroutine from a sync thread using the saved event loop."""
if _event_loop is not None and _event_loop.is_running():
asyncio.run_coroutine_threadsafe(coro, _event_loop)
@app.route('/state')
def serve_state() -> Any:
return get_global_envs_to_emit()
# --- WebSocket Broadcasting Functions ---
async def broadcast(event: str, data: Any) -> None:
"""Broadcast a message to all connected WebSocket clients."""
disconnected = set()
message = custom_json_dumps({event: data})
for websocket in list(ws_connections):
try:
await websocket.send_text(message)
except Exception as e:
logger.error(f"Error sending to websocket: {e}")
disconnected.add(websocket)
for ws in disconnected:
ws_connections.discard(ws)
@app.route('/branches')
def serve_branches() -> Any:
return get_global_branches_to_emit()
async def broadcast_branches(data: Any) -> None:
await broadcast("branches", data)
class EnvironmentNamespace(Namespace):
def on_connect(self, auth: Optional[Any] = None) -> None:
emit('environments', get_global_envs_to_emit())
def on_update(self, data: Dict[str, Any]) -> None:
global environments, remote_sio
logger.info(f"Received environment update: {data}")
if data.get('id') == '':
reset_caches(list(environments.values()))
else:
for env in environments.values():
if env.id == data.get('id'):
env.branches = data.get('branches', env.branches)
logger.info(f"Updated environment {env.id} branches to {env.branches}")
environment_update_event.set()
if remote_sio and remote_sio is not None and remote_sio.connected:
remote_sio.emit('update', data, namespace='/ws/environment')
logger.info(f"Updated slave")
emit('environments', get_global_envs_to_emit(), namespace='/ws/environment')
async def broadcast_environments(data: Any) -> None:
await broadcast("environments", data)
class ErrorsNamespace(Namespace):
def on_connect(self, auth: Optional[Any] = None) -> None:
pass
def on_error(self, data: Any) -> None:
emit('error', data)
async def broadcast_error(data: Any) -> None:
await broadcast("error", data)
socketio.on_namespace(BranchesNamespace('/ws/branches'))
socketio.on_namespace(EnvironmentNamespace('/ws/environment'))
socketio.on_namespace(ErrorsNamespace('/ws/errors'))
# --- Single WebSocket Endpoint ---
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> None:
await websocket.accept()
ws_connections.add(websocket)
try:
# Send initial state on connect
await websocket.send_text(custom_json_dumps({
"branches": get_global_branches_to_emit(),
}))
await websocket.send_text(custom_json_dumps({
"environments": get_global_envs_to_emit(),
}))
while True:
data = await websocket.receive_text()
message = json.loads(data)
if "update" in message:
update_data = message.get("update") or {}
logger.info(f"Received environment update: {update_data}")
if update_data.get('id') == '':
reset_caches(list(environments.values()))
else:
for env in environments.values():
if env.id == update_data.get('id'):
env.branches = update_data.get('branches', env.branches)
logger.info(f"Updated environment {env.id} branches to {env.branches}")
environment_update_event.set()
if slave_ws_task is not None:
await slave_send_queue.put({"update": update_data})
await broadcast_environments(get_global_envs_to_emit())
except WebSocketDisconnect as e:
ws_connections.discard(websocket)
except Exception as e:
logger.error(f"WebSocket error: {e}")
await broadcast_error({'message': 'WebSocket exception'})
ws_connections.discard(websocket)
def sigchld_handler(signum, frame): # type: ignore[no-untyped-def]
"""Reap zombie processes"""
# --- Slave Connection ---
async def connect_to_slave() -> None:
"""Connect to a slave brencher instance via a single WebSocket."""
global branches_slaves, environments_slaves
if not slave_url:
return
logger.info(f"SLAVE_BRENCHER set, will connect to slave at {slave_url}")
while True:
try:
ws_url = slave_url.replace('http://', 'ws://').replace('https://', 'wss://') + '/ws'
async with websockets.connect(ws_url) as ws:
logger.info("Connected to slave WebSocket")
async def handle_messages() -> None:
global branches_slaves, environments_slaves
async for msg in ws:
parsed = json.loads(msg)
if "branches" in parsed:
branches_slaves = parsed["branches"]
await broadcast_branches(get_global_branches_to_emit())
elif "environments" in parsed:
environments_slaves = parsed["environments"]
await broadcast_environments(get_global_envs_to_emit())
elif "error" in parsed:
await broadcast_error(parsed["error"])
async def handle_sends() -> None:
while True:
msg = await slave_send_queue.get()
await ws.send(json.dumps(msg))
await asyncio.gather(
handle_messages(),
handle_sends(),
)
except Exception as e:
logger.error(f"Could not connect to SLAVE_BRENCHER {slave_url}: {e}")
await asyncio.sleep(60)
# --- SIGCHLD Handler ---
def sigchld_handler(signum: int, frame: Any) -> None:
"""Reap zombie processes."""
while True:
try:
pid, status = os.waitpid(-1, os.WNOHANG)
@@ -289,14 +339,22 @@ def sigchld_handler(signum, frame): # type: ignore[no-untyped-def]
break
import signal
# --- Startup Event ---
@app.on_event("startup")
async def startup_event() -> None:
global _event_loop, slave_ws_task
_event_loop = asyncio.get_event_loop()
if slave_url:
slave_ws_task = asyncio.create_task(connect_to_slave())
# --- App Class (used by CLI and integration tests) ---
class App:
def __init__(self, cli_env_ids_str: str, dry_run: bool = False) -> None:
global environments
# Register the handler
signal.signal(signal.SIGCHLD, sigchld_handler)
import configs.brencher
@@ -356,15 +414,10 @@ class App:
def processing_thread(self) -> None:
while True:
import processing
def emit_envs() -> None:
try:
socketio.emit('branches', get_global_branches_to_emit(), namespace='/ws/branches')
except Exception as e:
logger.error(f"Error emitting branches: {str(e)}")
try:
socketio.emit('environments', get_global_envs_to_emit(), namespace='/ws/environment')
except Exception as e:
logger.error(f"Error emitting environments: {str(e)}")
_schedule_async(broadcast_branches(get_global_branches_to_emit()))
_schedule_async(broadcast_environments(get_global_envs_to_emit()))
with state_lock:
logger.info(f"Processing")
@@ -374,34 +427,18 @@ class App:
environment_update_event.wait(timeout=1 * 60)
environment_update_event.clear()
def _connect_remote(self) -> None:
global remote_sio
print("Connecting to SLAVE_BRENCHER...")
while True:
try:
if remote_sio is not None and not remote_sio.connected:
remote_sio.connect(slave_url, namespaces=['/ws/branches', '/ws/environment', '/ws/errors'])
except Exception as e:
logger.error(f"Could not connect to SLAVE_BRENCHER {slave_url}: {e}")
time.sleep(60)
def run(self) -> None:
processing = threading.Thread(target=self.processing_thread)
processing.daemon = True
processing.start()
t = threading.Thread(target=self._connect_remote, daemon=True)
t.start()
def runWeb(self, port: int) -> None:
self.run()
socketio.run(app, host='0.0.0.0', port=port, debug=False, allow_unsafe_werkzeug=True)
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=port, log_level='info')
if __name__ == '__main__':
import os
import sys
cli_env_ids_list = sys.argv[1:]
cli_env_ids_str: str
if len(cli_env_ids_list) == 0:
+11 -6
View File
@@ -4,6 +4,7 @@ import os
import shutil
import tempfile
from dataclasses import dataclass
import traceback
from typing import List, Tuple, Set, Dict, Any, Mapping, runtime_checkable
import git
@@ -140,12 +141,16 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
# Extract commit ids for the selected branches
commit_ids: Dict[Commit, str] = {}
for branch_pair in self.env.branches:
branch_name, desired_commit = branch_pair
if desired_commit == 'HEAD':
commit = repo.commit(f'origin/{branch_name}')
else:
commit = repo.commit(desired_commit)
commit_ids[commit] = branch_name
try:
branch_name, desired_commit = branch_pair
if desired_commit == 'HEAD':
commit = repo.commit(f'origin/{branch_name}')
else:
commit = repo.commit(desired_commit)
commit_ids[commit] = branch_name
except BaseException as e:
stack = traceback.format_exception(type(e), e, e.__traceback__)
logger.error(f"Error finding desired commits for branch {branch_pair}: {str(e)}\n{''.join(stack)}")
return commit_ids
def progress(self) -> CheckoutAndMergeResult:
+102 -96
View File
@@ -2,10 +2,8 @@
// You can add your Socket.IO and UI code here
console.log('app.js loaded');
// WebSocket namespaces
const WS_BRANCHES = '/ws/branches';
const WS_ENVIRONMENT = '/ws/environment';
const WS_ERRORS = '/ws/errors';
// WebSocket endpoint
const WS_URL = '/ws';
// DOM refs
const branchesList = document.getElementById('branches-list');
@@ -36,10 +34,8 @@ const serverSelectedBranchesByEnv = {};
// Populated from any step result that contains a `columns` key (e.g. GitUnmerge)
const columnsByEnv = {};
// Socket instances
let wsBranches = null;
let wsEnv = null;
let wsErr = null;
// Single WebSocket connection
let ws = null;
function showStatus(message, isError = false) {
statusMessage.textContent = message;
@@ -50,14 +46,16 @@ function showStatus(message, isError = false) {
closeStatus.onclick = () => statusBar.classList.add('hidden');
function checkForPendingChanges() {
function isChangesPending() {
// Determine if any environment has changed selections
const changed = Object.keys(selectedBranchesByEnv).some(envId => {
return Object.keys(selectedBranchesByEnv).some(envId => {
const localSel = [...(selectedBranchesByEnv[envId] || [])].sort();
const serverSel = [...(serverSelectedBranchesByEnv[envId] || [])].sort();
return JSON.stringify(localSel) !== JSON.stringify(serverSel);
});
if (changed) applyChangesBtn.classList.remove('hidden');
}
function checkForPendingChanges() {
if (isChangesPending()) applyChangesBtn.classList.remove('hidden');
else applyChangesBtn.classList.add('hidden');
}
@@ -330,7 +328,9 @@ toggleJobSpoiler = function (key, safeId) {
}
};
refreshBranchesBtn.onclick = () => {
wsEnv.emit('update', {id: ""});
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ update: { id: "" } }));
}
showStatus('Refreshing...');
};
@@ -354,95 +354,100 @@ applyChangesBtn.onclick = () => {
updateEnvironment();
};
// Socket.IO setup
function setupSocketIO() {
wsBranches = io(WS_BRANCHES);
wsEnv = io(WS_ENVIRONMENT);
wsErr = io(WS_ERRORS);
// Single WebSocket setup
function setupWebSockets() {
// Close existing connection before reconnecting
if (ws && ws.readyState !== WebSocket.CLOSED) {
ws.onclose = null; // Prevent recursive reconnect
ws.close();
}
wsBranches.on('branches', data => {
// data: { envId: { branchName: commits[] } }
branches = Object.entries(data).flatMap(([envId, branchMap]) =>
Object.entries(branchMap).map(([branchName, commitList]) => {
const envObj = (environmentsRaw.find((e) => e.id === envId) || [null])[0];
return {
envId,
envName: envObj ? (envObj.name || envObj.id) : envId,
branch: branchName,
commits: commitList
};
})
);
filterBranches();
showStatus('Branches updated.');
});
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.host;
wsEnv.on('environments', data => {
payload = data || {};
environmentsRaw = Object.values(payload);
ws = new WebSocket(`${protocol}//${host}${WS_URL}`);
ws.onopen = () => console.log('WebSocket connected');
ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
if ('branches' in message) {
const data = message.branches;
// data: { envId: { branchName: commits[] } }
branches = Object.entries(data).flatMap(([envId, branchMap]) =>
Object.entries(branchMap).map(([branchName, commitList]) => {
const envObj = (environmentsRaw.find((e) => e.id === envId) || [null])[0];
return { envId, envName: envObj ? (envObj.name || envObj.id) : envId, branch: branchName, commits: commitList };
})
);
filterBranches();
showStatus('Branches updated.');
} else if ('environments' in message) {
const data = message.environments;
payload = data || {};
environmentsRaw = Object.values(payload);
// Sync selections and deployed commits per environment
environmentsRaw.forEach((envObj) => {
if (!envObj) return;
const envId = envObj.id || envObj.name || 'unknown';
// Branch selections
if (Array.isArray(envObj.branches) && envObj.branches.length > 0) {
if (Array.isArray(envObj.branches[0])) {
selectedBranchesByEnv[envId] = [...envObj.branches];
} else {
selectedBranchesByEnv[envId] = envObj.branches.map(b => [b, 'HEAD']);
}
serverSelectedBranchesByEnv[envId] = [...selectedBranchesByEnv[envId]];
} else if (!selectedBranchesByEnv[envId]) {
selectedBranchesByEnv[envId] = [];
serverSelectedBranchesByEnv[envId] = [];
}
// Extract columns from all job statuses
columnsByEnv[envId] = {};
const jobsArr = envObj.pipeline || [];
if (Array.isArray(jobsArr)) {
jobsArr.forEach(job => {
if (job.status == null || job.error) return;
const status = job.status;
// New structured format: status is an object with a `columns` key
if (typeof status === 'object' && !Array.isArray(status) && status.columns &&
typeof status.columns === 'object') {
Object.entries(status.columns).forEach(([colName, colData]) => {
if (colData && typeof colData === 'object' && !Array.isArray(colData)) {
columnsByEnv[envId][colName] = {...colData};
// Sync selections and column data per environment
environmentsRaw.forEach((envObj) => {
if (!envObj) return;
const envId = envObj.id || envObj.name || 'unknown';
if (!isChangesPending()) {
if (Array.isArray(envObj.branches) && envObj.branches.length > 0) {
if (Array.isArray(envObj.branches[0])) {
selectedBranchesByEnv[envId] = [...envObj.branches];
} else {
selectedBranchesByEnv[envId] = envObj.branches.map(b => [b, 'HEAD']);
}
});
}
// Legacy list format from GitUnmerge: array of [branch_name, commit_hash]
if (job.name === 'GitUnmerge' && Array.isArray(status)) {
const deployedCol = {};
status.forEach(([branchName, commitHash]) => {
if (typeof branchName === 'string' && typeof commitHash === 'string') {
deployedCol[branchName] = commitHash.substring(0, 8);
}
});
if (Object.keys(deployedCol).length > 0) {
columnsByEnv[envId]['Deployed'] = deployedCol;
serverSelectedBranchesByEnv[envId] = [...selectedBranchesByEnv[envId]];
} else if (!selectedBranchesByEnv[envId]) {
selectedBranchesByEnv[envId] = [];
serverSelectedBranchesByEnv[envId] = [];
}
}
// Extract columns from all job statuses
columnsByEnv[envId] = {};
const jobsArr = envObj.pipeline || [];
if (Array.isArray(jobsArr)) {
jobsArr.forEach(job => {
if (job.status == null || job.error) return;
const status = job.status;
// New structured format: status is an object with a `columns` key
if (typeof status === 'object' && !Array.isArray(status) && status.columns &&
typeof status.columns === 'object') {
Object.entries(status.columns).forEach(([colName, colData]) => {
if (colData && typeof colData === 'object' && !Array.isArray(colData)) {
columnsByEnv[envId][colName] = {...colData};
}
});
}
});
}
});
// Rebuild branches list environment names for rows already present
branches = branches.map(b => {
const envEntry = environmentsRaw.find((e) => (e.id || e.name) === b.envId);
return { ...b, envName: envEntry ? (envEntry.name || envEntry.id) : b.envName };
});
filterBranches();
renderJobs();
showStatus('Environments updated.');
} else if ('error' in message) {
console.error('Error from server:', message.error);
showStatus(message.error.message || 'Unknown error', true);
}
});
// Rebuild branches list environment names for rows already present
branches = branches.map(b => {
const envEntry = environmentsRaw.find((e) => (e.id || e.name) === b.envId);
return {...b, envName: envEntry ? (envEntry.name || envEntry.id) : b.envName};
});
filterBranches();
renderJobs();
showStatus('Environments updated.');
});
wsEnv.on('disconnect', () => showStatus('Disconnected from environment websocket.', true));
wsBranches.on('disconnect', () => showStatus('Disconnected from branches websocket.', true));
wsErr.on('error', data => showStatus(data.message || 'Unknown error', true));
checkForPendingChanges();
} catch (e) {
console.error('Error processing WebSocket message:', e);
}
};
ws.onclose = () => {
showStatus('Disconnected from server.', true);
setTimeout(() => setupWebSockets(), 5000);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
}
function updateEnvironment() {
@@ -451,7 +456,9 @@ function updateEnvironment() {
const localSel = [...(selectedBranchesByEnv[envId] || [])].sort();
const serverSel = [...(serverSelectedBranchesByEnv[envId] || [])].sort();
if (JSON.stringify(localSel) !== JSON.stringify(serverSel)) {
wsEnv.emit('update', {id: envId, branches: selectedBranchesByEnv[envId]});
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ update: { id: envId, branches: selectedBranchesByEnv[envId] } }));
}
// Optimistically sync server state
serverSelectedBranchesByEnv[envId] = [...selectedBranchesByEnv[envId]];
branchFilter.value = '';
@@ -460,9 +467,8 @@ function updateEnvironment() {
checkForPendingChanges();
showStatus('Changes applied (pending server confirmation).');
}
// Initial load
setupSocketIO();
setupWebSockets();
showStatus('Loading branches...');
renderBranches();
renderJobs();
-1
View File
@@ -47,7 +47,6 @@
</main>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.6.1/socket.io.js"></script>
<script src="app.js"></script>
</body>
</html>
+5 -5
View File
@@ -45,19 +45,19 @@ disallow_any_unimported = False
[mypy-docker.*]
ignore_missing_imports = True
[mypy-flask_socketio.*]
ignore_missing_imports = True
[mypy-dotenv.*]
ignore_missing_imports = True
[mypy-yaml.*]
ignore_missing_imports = True
[mypy-flask.*]
[mypy-websockets.*]
ignore_missing_imports = True
[mypy-socketio.*]
[mypy-uvicorn.*]
ignore_missing_imports = True
[mypy-fastapi.*]
ignore_missing_imports = True
[mypy-pytest.*]
+3 -4
View File
@@ -1,10 +1,10 @@
flask
flask-socketio
fastapi
uvicorn[standard]
websockets
gitpython
python-dotenv
docker
PyYAML
websocket-client
pytest
pytest-asyncio
@@ -12,6 +12,5 @@ mypy
ty
types-PyYAML
types-requests
types-flask-socketio
types-docker
testcontainers