Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bee9dc133 | ||
|
|
d5a7bc1e4e | ||
|
|
2e48d11b77 | ||
|
|
8219b743cf | ||
|
|
7938f4f0f0 | ||
|
|
71d1e1c1af | ||
|
|
52692a75b8 | ||
|
|
74bf5aefa2 | ||
|
|
60b5af79fc | ||
|
|
ddce27f1af | ||
|
|
33caa4cb9f | ||
|
|
a9de490ebe | ||
|
|
99f8b73654 | ||
|
|
272504eab3 | ||
|
|
fa9f8791bd | ||
|
|
694c28109d | ||
|
|
920249ec30 | ||
|
|
75126cb2fd | ||
|
|
0e83458044 | ||
|
|
7dc36d7487 | ||
|
|
b0e32f835a | ||
|
|
1c0580750d | ||
|
|
8059cc74ea | ||
|
|
278253f6eb | ||
|
|
e690f6a50c | ||
|
|
98ee950a1f | ||
|
|
e4f90b1f23 | ||
|
|
ba17865a49 | ||
|
|
70f4a4163e | ||
|
|
e5331745be | ||
|
|
94a858d8ef | ||
|
|
57393c933c | ||
|
|
fab47acc95 | ||
|
|
a453e20f49 | ||
|
|
8e77a8b1f6 |
@@ -0,0 +1,44 @@
|
||||
name: Generate OpenAPI JavaScript client
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'openapi.yaml'
|
||||
- '.github/workflows/generate-openapi-client.yml'
|
||||
workflow_dispatch: {}
|
||||
|
||||
jobs:
|
||||
generate:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Java (required by OpenAPI Generator)
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: '17'
|
||||
|
||||
- name: Generate JavaScript client from OpenAPI
|
||||
uses: openapitools/openapi-generator-cli@v2.5.0
|
||||
with:
|
||||
arguments: generate -i openapi.yaml -g javascript -o openapi-client-ts --additional-properties=useES6=true,projectName=brencher-openapi
|
||||
|
||||
- name: Generate Python FastAPI server from OpenAPI
|
||||
uses: openapitools/openapi-generator-cli@v2.5.0
|
||||
with:
|
||||
arguments: generate -i openapi.yaml -g python-fastapi -o openapi-server-py --additional-properties=projectName=brencher-python-server,packageName=brencher_server
|
||||
|
||||
# docker run --rm -v "$PWD":/local openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g python-fastapi -o /local/openapi-server-py --additional-properties=projectName=brencher-python-server,packageName=brencher_server
|
||||
# docker run --rm -v "$PWD":/local openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g javascript -o /local/openapi-client-ts --additional-properties=useES6=true,projectName=brencher-openapi
|
||||
|
||||
# - name: Commit generated client
|
||||
# uses: EndBug/add-and-commit@v9
|
||||
# with:
|
||||
# author_name: github-actions
|
||||
# author_email: actions@github.com
|
||||
# message: "chore: regenerate OpenAPI JavaScript client"
|
||||
# add: openapi-client-ts openapi-server-py
|
||||
# env:
|
||||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -20,16 +20,16 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
token: ${{ secrets.PUSH_TOKEN }}
|
||||
|
||||
- name: Configure git
|
||||
run: |
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git config user.name "GitHub Actions"
|
||||
git config user.email "gitea-actions[bot]@noreply.localhost"
|
||||
git config user.name "Gitea Actions"
|
||||
|
||||
- name: Fetch all branches
|
||||
run: |
|
||||
git remote set-url origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
|
||||
git remote set-url origin "https://x-access-token:${PUSH_TOKEN}@gitea.rudolf.keenetic.link/${GITHUB_REPOSITORY}.git"
|
||||
git fetch --all --prune
|
||||
|
||||
- name: Merge every branch onto main
|
||||
@@ -37,7 +37,6 @@ jobs:
|
||||
run: |
|
||||
set -eo pipefail
|
||||
MAIN=main
|
||||
# Collect remote branch names (exclude HEAD and main)
|
||||
branches=$(git for-each-ref --format='%(refname:short)' refs/remotes/origin/ \
|
||||
| sed 's|^origin/||' \
|
||||
| grep -v -e '^HEAD$' -e '^main$' -e '^origin$' \
|
||||
@@ -49,11 +48,8 @@ jobs:
|
||||
continue
|
||||
fi
|
||||
echo "=== Processing branch: $br ==="
|
||||
# Create/update a local branch from remote
|
||||
git checkout -B "$br" "origin/$br"
|
||||
# Merge origin/main into branch (non-interactive)
|
||||
if git merge --no-edit "origin/$MAIN"; then
|
||||
# Push updated branch to remote (no force)
|
||||
if git push origin "$br"; then
|
||||
echo "Pushed merged $br"
|
||||
else
|
||||
|
||||
@@ -18,16 +18,16 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
token: ${{ secrets.PUSH_TOKEN }}
|
||||
|
||||
- name: Configure git
|
||||
run: |
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git config user.name "GitHub Actions"
|
||||
git config user.email "gitea-actions[bot]@noreply.localhost"
|
||||
git config user.name "Gitea Actions"
|
||||
|
||||
- name: Fetch all branches
|
||||
run: |
|
||||
git remote set-url origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
|
||||
git remote set-url origin "https://x-access-token:${PUSH_TOKEN}@gitea.rudolf.keenetic.link/${GITHUB_REPOSITORY}.git"
|
||||
git fetch --all --prune
|
||||
|
||||
- name: Merge every branch onto main
|
||||
@@ -35,7 +35,6 @@ jobs:
|
||||
run: |
|
||||
set -eo pipefail
|
||||
MAIN=main
|
||||
# Collect remote branch names (exclude HEAD and main)
|
||||
branches=$(git for-each-ref --format='%(refname:short)' refs/remotes/origin/ \
|
||||
| sed 's|^origin/||' \
|
||||
| grep -v -e '^HEAD$' -e '^main$' -e '^origin$' \
|
||||
@@ -47,12 +46,9 @@ jobs:
|
||||
continue
|
||||
fi
|
||||
echo "=== Processing branch: $br ==="
|
||||
# Create/update a local branch from remote
|
||||
git reset --hard HEAD
|
||||
git checkout -B "$br" "origin/$br"
|
||||
# Merge origin/main into branch (non-interactive)
|
||||
if git merge --no-edit --squash "origin/$MAIN"; then
|
||||
# Push updated branch to remote (no force)
|
||||
git reset --soft "origin/$MAIN"
|
||||
git commit -m "Squash"
|
||||
if git push origin "$br" --force-with-lease; then
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
|
||||
|
||||
name: Python package
|
||||
|
||||
on:
|
||||
@@ -10,73 +7,40 @@ on:
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
python-version: [
|
||||
# "3.11.3",
|
||||
"3.12.3",
|
||||
"3.13",
|
||||
"3.14",
|
||||
# "3.15",
|
||||
]
|
||||
checker: [
|
||||
"python -c \"import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')\"",
|
||||
"pip list",
|
||||
"flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics",
|
||||
"flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics",
|
||||
# "ty check backend",
|
||||
"mypy --no-incremental --config-file mypy.ini backend tests",
|
||||
]
|
||||
python-version: ["3.12.3", "3.13", "3.14"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python ${{ matrix.python-version }}, ${{ matrix.checker }}
|
||||
uses: actions/setup-python@v3
|
||||
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
cache: 'pip'
|
||||
cache-dependency-path: |
|
||||
requirements.txt
|
||||
requirements-dev.txt
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
- name: Run checker
|
||||
run: ${{ matrix.checker }}
|
||||
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
python-version: [
|
||||
# "3.11.3",
|
||||
"3.12.3",
|
||||
"3.13",
|
||||
"3.14",
|
||||
# "3.15",
|
||||
]
|
||||
checker: [
|
||||
"pytest",
|
||||
]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python ${{ matrix.python-version }}, ${{ matrix.checker }}
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Install dependencies
|
||||
- name: Lint (flake8)
|
||||
run: |
|
||||
# sudo apt-get update
|
||||
# sudo apt-get upgrade -y
|
||||
# sudo apt-get install -y --no-install-recommends git docker.io
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements-dev.txt
|
||||
playwright install --with-deps chromium
|
||||
- name: Run checker
|
||||
run: "pytest"
|
||||
|
||||
flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics
|
||||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||
|
||||
- name: Type-check (mypy)
|
||||
run: mypy --no-incremental --config-file mypy.ini backend tests
|
||||
@@ -0,0 +1,34 @@
|
||||
name: Verify OpenAPI types
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
verify-openapi-types:
|
||||
name: Verify generated TypeScript matches committed types
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: Generate types from OpenAPI
|
||||
run: |
|
||||
set -euo pipefail
|
||||
npx --yes openapi-typescript openapi.yaml --output generated-openapi-types.ts
|
||||
|
||||
- name: Show diff and fail if different
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if ! command -v diff >/dev/null 2>&1; then echo "diff not found" && exit 2; fi
|
||||
echo "Comparing generated types to frontend/src/types/api.ts"
|
||||
if ! diff -u frontend/src/types/api.ts generated-openapi-types.ts; then
|
||||
echo "OpenAPI types are out of date. Regenerate with: npx openapi-typescript openapi.yaml --output frontend/src/types/api.ts";
|
||||
exit 1;
|
||||
fi
|
||||
+3
-1
@@ -6,4 +6,6 @@ venv
|
||||
.venv
|
||||
LOCAL.md
|
||||
.idea
|
||||
docker-compose.yml.tmp
|
||||
docker-compose.yml.tmp
|
||||
frontend/node_modules
|
||||
frontend/dist
|
||||
+4
-2
@@ -38,9 +38,11 @@ COPY requirements.txt ./
|
||||
RUN uv venv .venv && \
|
||||
uv pip install -r requirements.txt
|
||||
|
||||
# Copy backend and frontend
|
||||
# Copy backend
|
||||
COPY backend/ ./backend/
|
||||
COPY frontend/ ./frontend/
|
||||
|
||||
# Copy built frontend from Stage 1
|
||||
COPY --from=frontend-builder /app/frontend/dist ./frontend/dist
|
||||
|
||||
# Expose port for the web server
|
||||
EXPOSE 5001
|
||||
|
||||
@@ -17,3 +17,6 @@
|
||||
state: str # 'Active' or 'Pause'
|
||||
pipeline: Dict[str, Any]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+331
-2
@@ -23,6 +23,335 @@ load_dotenv("local.env")
|
||||
load_dotenv('/run/secrets/brencher-secrets')
|
||||
|
||||
|
||||
class DataclassJSONEncoder(json.JSONEncoder):
|
||||
def default(self, o: Any) -> Any:
|
||||
if hasattr(o, '__dataclass_fields__'):
|
||||
return asdict(o)
|
||||
if isinstance(o, BaseException):
|
||||
return str(o)
|
||||
try:
|
||||
return super().default(o)
|
||||
except TypeError:
|
||||
return str(o)
|
||||
|
||||
|
||||
def custom_json_dumps(obj: Any) -> str:
|
||||
return json.dumps(obj, cls=DataclassJSONEncoder)
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
FRONTEND_DIR = os.path.join(os.path.dirname(__file__), '../frontend/dist')
|
||||
|
||||
# In-memory state
|
||||
environments: Dict[str, Environment] = {}
|
||||
environments_slaves: Dict[str, Any] = {}
|
||||
branches_slaves: Dict[str, Dict[str, Any]] = {}
|
||||
state_lock = threading.Lock()
|
||||
|
||||
# Active WebSocket connections
|
||||
ws_connections: Dict[WebSocket, Dict[str, Any]] = {}
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
# --- Static File Serving ---
|
||||
|
||||
@app.get("/")
|
||||
async def serve_index() -> FileResponse:
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
|
||||
@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')
|
||||
|
||||
|
||||
def merge_dicts(a: Dict[str, T], b: Dict[str, T]) -> Dict[str, T]:
|
||||
result: Dict[str, T] = {}
|
||||
for k in (a.keys() | b.keys()):
|
||||
if k not in a or k not in b:
|
||||
result[k] = a.get(k, b.get(k)) # type: ignore[assignment]
|
||||
continue
|
||||
if (
|
||||
isinstance(a[k], dict)
|
||||
and isinstance(b[k], dict)
|
||||
):
|
||||
result[k] = merge_dicts(a[k], b[k]) # type: ignore[arg-type,assignment]
|
||||
else:
|
||||
result[k] = b[k]
|
||||
return result
|
||||
|
||||
|
||||
def get_local_envs_to_emit() -> Dict[str, Dict[str, Any]]:
|
||||
env_dtos: Dict[str, Dict[str, Any]] = {}
|
||||
for env in environments.values():
|
||||
pipeline_state: List[Dict[str, Any]] = []
|
||||
for r in env.pipeline:
|
||||
try:
|
||||
if isinstance(r, CachingStep):
|
||||
result = r._result
|
||||
else:
|
||||
result = r.progress()
|
||||
|
||||
if isinstance(result, BaseException):
|
||||
stack = traceback.format_exception(type(result), result, result.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(result), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
else:
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": result,
|
||||
"is_running": False,
|
||||
})
|
||||
except BaseException as e:
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(e), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
env_dtos[env.id] = asdict(replace(env, pipeline=[]))
|
||||
env_dtos[env.id]['pipeline'] = pipeline_state
|
||||
return env_dtos
|
||||
|
||||
|
||||
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)
|
||||
common_keys = set(local_envs.keys()) & set(environments_slaves.keys())
|
||||
if len(common_keys) > 0:
|
||||
_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]]] = {}
|
||||
for k, env in environments.items():
|
||||
branches[k] = {}
|
||||
try:
|
||||
for step in env.pipeline:
|
||||
if isinstance(step, GitClone):
|
||||
branches[k] = {**step.get_branches()}
|
||||
if isinstance(step, CachingStep) and isinstance(step.step, GitClone):
|
||||
branches[k] = {**step.step.get_branches()}
|
||||
except BaseException as 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)
|
||||
|
||||
|
||||
# --- 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)
|
||||
|
||||
|
||||
# --- WebSocket Broadcasting Functions ---
|
||||
|
||||
async def broadcast(event: str, data: Any) -> None:
|
||||
"""Broadcast a message to all connected WebSocket clients that have not seen this data yet."""
|
||||
disconnected = set()
|
||||
message = custom_json_dumps({event: data})
|
||||
|
||||
for websocket in ws_connections.keys():
|
||||
if ws_connections[websocket].get(event) == message:
|
||||
continue
|
||||
try:
|
||||
await websocket.send_text(message)
|
||||
ws_connections[websocket][event] = message
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending to websocket: {e}")
|
||||
disconnected.add(websocket)
|
||||
|
||||
for ws in disconnected:
|
||||
ws_connections.pop(ws, None)
|
||||
|
||||
|
||||
async def broadcast_branches(data: Any) -> None:
|
||||
await broadcast("branches", data)
|
||||
|
||||
|
||||
async def broadcast_environments(data: Any) -> None:
|
||||
await broadcast("environments", data)
|
||||
|
||||
|
||||
async def broadcast_error(data: Any) -> None:
|
||||
await broadcast("error", data)
|
||||
|
||||
|
||||
# --- Single WebSocket Endpoint ---
|
||||
|
||||
@app.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
ws_connections[websocket] = {}
|
||||
|
||||
try:
|
||||
# Send initial state on connect and record it so duplicate broadcasts are suppressed
|
||||
branches_payload = custom_json_dumps({"branches": get_global_branches_to_emit()})
|
||||
envs_payload = custom_json_dumps({"environments": get_global_envs_to_emit()})
|
||||
await websocket.send_text(branches_payload)
|
||||
ws_connections[websocket]["branches"] = branches_payload
|
||||
await websocket.send_text(envs_payload)
|
||||
ws_connections[websocket]["environments"] = envs_payload
|
||||
|
||||
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)
|
||||
if 'dry' in update_data:
|
||||
env.dry = bool(update_data['dry'])
|
||||
logger.info(f"Updated environment {env.id} branches to {env.branches}, dry={env.dry}")
|
||||
reset_caches([env])
|
||||
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.pop(websocket, None)
|
||||
except Exception as e:
|
||||
logger.error(f"WebSocket error: {e}")
|
||||
await broadcast_error({'message': 'WebSocket exception'})
|
||||
ws_connections.pop(websocket, None)
|
||||
|
||||
|
||||
# --- 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)
|
||||
if pid == 0:
|
||||
break
|
||||
except ChildProcessError:
|
||||
break
|
||||
|
||||
|
||||
# --- 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, environments: Dict[str, Environment]) -> None:
|
||||
@@ -48,7 +377,7 @@ class App:
|
||||
"name": r.name,
|
||||
"status": [str(result), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
"is_running": False,
|
||||
})
|
||||
else:
|
||||
pipeline_state.append({
|
||||
@@ -62,7 +391,7 @@ class App:
|
||||
"name": r.name,
|
||||
"status": [str(e), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
"is_running": False,
|
||||
})
|
||||
env_dtos[env.id] = {'id': env.id }
|
||||
env_dtos[env.id]['pipeline'] = pipeline_state
|
||||
|
||||
@@ -5,7 +5,11 @@ from steps.docker import DockerComposeBuild, DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/brencher.git")
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": "https://github.com/rudolf1/brencher.git"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/brencher"},
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/brencher"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="brencher",
|
||||
@@ -45,10 +49,10 @@ deployDocker = DockerSwarmDeploy(
|
||||
docker_compose_path="docker-compose.yml",
|
||||
)
|
||||
|
||||
checkPing = UrlCheck(
|
||||
url="https://brencher.rudolf.keenetic.link/state",
|
||||
expected=checkPingF,
|
||||
)
|
||||
#checkPing = UrlCheck(
|
||||
# url="https://brencher.rudolf.keenetic.link/state",
|
||||
# expected=checkPingF,
|
||||
#)
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://brencher.rudolf.keenetic.link/",
|
||||
@@ -68,7 +72,7 @@ brencher = Environment(
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
checkPing,
|
||||
# checkPing,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
|
||||
@@ -21,7 +21,11 @@ def checkPingF(obj: Any) -> None:
|
||||
raise Exception(f"brencher2 check failed for: {v}")
|
||||
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/brencher.git")
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": "https://github.com/rudolf1/brencher.git"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/brencher"},
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/brencher"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="brencher2",
|
||||
@@ -72,10 +76,10 @@ deployDocker = DockerSwarmDeploy(
|
||||
docker_compose_path="docker-compose.yml",
|
||||
)
|
||||
|
||||
checkPing = UrlCheck(
|
||||
url="https://brencher.rudolf.keenetic.link/state",
|
||||
expected=checkPingF,
|
||||
)
|
||||
#checkPing = UrlCheck(
|
||||
# url="https://brencher.rudolf.keenetic.link/state",
|
||||
# expected=checkPingF,
|
||||
#)
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://brencher.rudolf.keenetic.link/",
|
||||
@@ -98,7 +102,7 @@ brencher2 = Environment(
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
checkPing,
|
||||
# checkPing,
|
||||
logUrls
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
from enironment import Environment
|
||||
from steps.checks import SimpleLog, UrlCheck
|
||||
from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
# Dual-remote clone for the ansible mirror (the deploy target, not brencher itself).
|
||||
# - gitea_hermes is where the feature branch lives (hermes/ansible:*)
|
||||
# - gitea_admin is the live-deploy source (where brencher-bot reads compose files from)
|
||||
#
|
||||
# Why not github? github.com is unreachable from the brencher container -- the
|
||||
# proxy returns HTTP 403 on CONNECT tunnel, so the URL would always fail to
|
||||
# fetch. See backend/configs/docker_socket_proxy.py for the same migration.
|
||||
#
|
||||
# Why drop branchNamePrefix? GitClone only applies its prefix refspec to a
|
||||
# remote named "origin" (backend/steps/git.py ~L104); neither remote here is
|
||||
# named "origin", so the prefix would be a silent no-op. Both remotes fetch
|
||||
# their full refset instead.
|
||||
#
|
||||
# Why two remotes? The deploy step (DockerSwarmDeploy) checks out a specific
|
||||
# branch and uses the resulting checkout as wd for the compose path. With
|
||||
# gitea_hermes as primary, feature branches opened in the fork are picked up;
|
||||
# gitea_admin provides the live-deploy source as a fallback / for cross-fork
|
||||
# PRs. This matches the form in backend/configs/brencher.py after PR #3.
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/ansible"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/ansible"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="gitea",
|
||||
)
|
||||
unmerge = GitUnmerge(clone, dockerSwarmCheck)
|
||||
|
||||
state = SharedStateHolderInMemory(unmerge=unmerge)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=state,
|
||||
push=False,
|
||||
git_user_email="rudolfss13@gmail.com",
|
||||
git_user_name="brencher_bot"
|
||||
)
|
||||
|
||||
deployDocker = DockerSwarmDeploy(
|
||||
wd=clone,
|
||||
buildDocker=None,
|
||||
stackChecker=dockerSwarmCheck,
|
||||
envs=lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
},
|
||||
stack_name="gitea",
|
||||
docker_compose_path="poc/gitea/stack-compose.yml",
|
||||
)
|
||||
|
||||
# checkPing1 = UrlCheck(
|
||||
# url="http://100.70.193.97:8087/api/status",
|
||||
# expected=lambda obj: obj['gateway_running'] == 'true' and obj['auth_required'] == 'true',
|
||||
# )
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://gitea.rudolf.keenetic.link",
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
__all__ = ["gitea"]
|
||||
gitea = Environment(
|
||||
id="gitea",
|
||||
state=state,
|
||||
pipeline=[
|
||||
clone,
|
||||
state,
|
||||
checkoutMerged,
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
# checkPing1,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
@@ -4,7 +4,10 @@ from steps.docker import DockerComposeBuild, DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/uber_backup.git", branchNamePrefix="immoscout")
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/mcps"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/mcps"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="gmail_mcp",
|
||||
@@ -23,7 +26,7 @@ checkoutMerged = CheckoutMerged(clone,
|
||||
buildDocker = DockerComposeBuild(clone,
|
||||
docker_repo_username="",
|
||||
docker_repo_password="",
|
||||
docker_compose_path="gmail-mcp-server/docker-compose.yml",
|
||||
docker_compose_path="docker-compose.yml",
|
||||
docker_repo_url="https://registry.rudolf.keenetic.link",
|
||||
publish=False,
|
||||
envs=lambda: {
|
||||
@@ -39,7 +42,7 @@ deployDocker = DockerSwarmDeploy(
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
},
|
||||
stack_name="gmail_mcp",
|
||||
docker_compose_path="gmail-mcp-server/docker-compose.yml",
|
||||
docker_compose_path="docker-compose.yml",
|
||||
)
|
||||
|
||||
checkPing1 = UrlCheck(
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
from enironment import Environment
|
||||
from steps.checks import SimpleLog
|
||||
from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/ansible"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/ansible"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="hermes",
|
||||
)
|
||||
unmerge = GitUnmerge(clone, dockerSwarmCheck)
|
||||
|
||||
state = SharedStateHolderInMemory(unmerge=unmerge)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=state,
|
||||
push=False,
|
||||
git_user_email="rudolfss13@gmail.com",
|
||||
git_user_name="brencher_bot"
|
||||
)
|
||||
|
||||
deployDocker = DockerSwarmDeploy(
|
||||
wd=clone,
|
||||
buildDocker=None,
|
||||
stackChecker=dockerSwarmCheck,
|
||||
envs=lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
},
|
||||
stack_name="hermes",
|
||||
docker_compose_path="poc/hermes/stack-compose.yml",
|
||||
)
|
||||
|
||||
# checkPing1 = UrlCheck(
|
||||
# url="http://100.70.193.97:8087/api/status",
|
||||
# expected=lambda obj: obj['gateway_running'] == 'true' and obj['auth_required'] == 'true',
|
||||
# )
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://hermes.rudolf.keenetic.link",
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
__all__ = ["hermes"]
|
||||
hermes = Environment(
|
||||
id="hermes",
|
||||
state=state,
|
||||
pipeline=[
|
||||
clone,
|
||||
state,
|
||||
checkoutMerged,
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
# checkPing1,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,94 @@
|
||||
from enironment import Environment
|
||||
from steps.checks import SimpleLog, UrlCheck
|
||||
from steps.docker import DockerComposeBuild, DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
# Dual-remote clone for the ansible mirror (the deploy target, not brencher itself).
|
||||
# - gitea_hermes is where the feature branch lives (hermes/ansible:*)
|
||||
# - gitea_admin is the live-deploy source (where brencher-bot reads compose files from)
|
||||
#
|
||||
# Why not github? github.com is unreachable from the brencher container -- the
|
||||
# proxy returns HTTP 403 on CONNECT tunnel, so the URL would always fail to
|
||||
# fetch. See backend/configs/docker_socket_proxy.py for the same migration.
|
||||
#
|
||||
# Why drop branchNamePrefix? GitClone only applies its prefix refspec to a
|
||||
# remote named "origin" (backend/steps/git.py ~L104); neither remote here is
|
||||
# named "origin", so the prefix would be a silent no-op. Both remotes fetch
|
||||
# their full refset instead.
|
||||
#
|
||||
# Why two remotes? The deploy step (DockerSwarmDeploy) checks out a specific
|
||||
# branch and uses the resulting checkout as wd for the compose path. With
|
||||
# gitea_hermes as primary, feature branches opened in the fork are picked up;
|
||||
# gitea_admin provides the live-deploy source as a fallback / for cross-fork
|
||||
# PRs. This matches the form in backend/configs/brencher.py after PR #3.
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/ansible"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/ansible"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="hermes_infra",
|
||||
)
|
||||
unmerge = GitUnmerge(clone, dockerSwarmCheck)
|
||||
|
||||
state = SharedStateHolderInMemory(unmerge=unmerge)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=state,
|
||||
push=False,
|
||||
git_user_email="rudolfss13@gmail.com",
|
||||
git_user_name="brencher_bot"
|
||||
)
|
||||
|
||||
buildDocker = DockerComposeBuild(clone,
|
||||
docker_repo_username="",
|
||||
docker_repo_password="",
|
||||
docker_compose_path="hermes_squid/stack-compose.yml",
|
||||
docker_repo_url="https://registry.rudolf.keenetic.link",
|
||||
publish=False,
|
||||
build_cache = True,
|
||||
envs=lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
# "user_group": "1000:137"
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
deployDocker = DockerSwarmDeploy(
|
||||
wd=clone,
|
||||
buildDocker=buildDocker,
|
||||
stackChecker=dockerSwarmCheck,
|
||||
envs=lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
},
|
||||
stack_name="hermes_infra",
|
||||
docker_compose_path="hermes_squid/stack-compose.yml",
|
||||
)
|
||||
|
||||
# checkPing1 = UrlCheck(
|
||||
# url="http://100.70.193.97:8088/api/status",
|
||||
# expected=lambda obj: obj['gateway_running'] == 'true' and obj['auth_required'] == 'true',
|
||||
# )
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://hermes.rudolf.keenetic.link",
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
__all__ = ["hermes_infra"]
|
||||
hermes_infra = Environment(
|
||||
id="hermes_infra",
|
||||
state=state,
|
||||
pipeline=[
|
||||
clone,
|
||||
state,
|
||||
checkoutMerged,
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
# checkPing1,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
@@ -4,7 +4,28 @@ from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/uber_backup.git", branchNamePrefix="ansible")
|
||||
# Dual-remote clone for the ansible mirror (the deploy target, not brencher itself).
|
||||
# - gitea_hermes is where the feature branch lives (hermes/ansible:*)
|
||||
# - gitea_admin is the live-deploy source (where brencher-bot reads compose files from)
|
||||
#
|
||||
# Why not github? github.com is unreachable from the brencher container -- the
|
||||
# proxy returns HTTP 403 on CONNECT tunnel, so the URL would always fail to
|
||||
# fetch. See backend/configs/docker_socket_proxy.py for the same migration.
|
||||
#
|
||||
# Why drop branchNamePrefix? GitClone only applies its prefix refspec to a
|
||||
# remote named "origin" (backend/steps/git.py ~L104); neither remote here is
|
||||
# named "origin", so the prefix would be a silent no-op. Both remotes fetch
|
||||
# their full refset instead.
|
||||
#
|
||||
# Why two remotes? The deploy step (DockerSwarmDeploy) checks out a specific
|
||||
# branch and uses the resulting checkout as wd for the compose path. With
|
||||
# gitea_hermes as primary, feature branches opened in the fork are picked up;
|
||||
# gitea_admin provides the live-deploy source as a fallback / for cross-fork
|
||||
# PRs. This matches the form in backend/configs/brencher.py after PR #3.
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/ansible"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/ansible"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="immich",
|
||||
@@ -31,10 +52,10 @@ deployDocker = DockerSwarmDeploy(
|
||||
docker_compose_path="poc/immich/stack-compose.yml",
|
||||
)
|
||||
|
||||
checkPing = UrlCheck(
|
||||
url="https://immich.rudolf.keenetic.link/api/server/ping",
|
||||
expected={"res": "pong"},
|
||||
)
|
||||
#checkPing = UrlCheck(
|
||||
# url="https://immich.rudolf.keenetic.link/api/server/ping",
|
||||
# expected={"res": "pong"},
|
||||
#)
|
||||
logUrls = SimpleLog(message={
|
||||
"userLinks": {
|
||||
"App": "https://immich.rudolf.keenetic.link",
|
||||
@@ -52,7 +73,7 @@ immich = Environment(
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
checkPing,
|
||||
# checkPing,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
|
||||
@@ -3,7 +3,11 @@ from steps.docker import DockerComposeBuild, DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/reverseproxy.git")
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": "https://github.com/rudolf1/reverseproxy.git"},
|
||||
{"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/reverseproxy"},
|
||||
{"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/reverseproxy"},
|
||||
])
|
||||
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name="torrserv_proxy",
|
||||
|
||||
+69
-5
@@ -1,3 +1,4 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
@@ -197,6 +198,32 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
content = f.read()
|
||||
content = re.sub(r'\$\{([^}]+)\}', lambda m: env.get(m.group(1), ""), content)
|
||||
compose = yaml.safe_load(content)
|
||||
version = env["version"]
|
||||
if "configs" in compose:
|
||||
sanitized_version = re.sub(r'[^a-zA-Z0-9_.-]', '_', version)
|
||||
full_prefix = f"{self.stack_name}_"
|
||||
|
||||
def _config_full_name(base_name: str) -> str:
|
||||
full = f"{full_prefix}{base_name}_{sanitized_version}"
|
||||
if len(full) <= 64:
|
||||
return f"{base_name}_{sanitized_version}"
|
||||
return hashlib.sha256(full.encode("utf-8")).hexdigest()[:32]
|
||||
|
||||
config_names = {
|
||||
name: _config_full_name(name)
|
||||
for name in compose["configs"]
|
||||
}
|
||||
compose["configs"] = {
|
||||
config_names.get(name, name): config
|
||||
for name, config in compose["configs"].items()
|
||||
}
|
||||
for service in compose.get("services", {}).values():
|
||||
for config in service.get("configs", []):
|
||||
if isinstance(config, str):
|
||||
config_index = service["configs"].index(config)
|
||||
service["configs"][config_index] = config_names.get(config, config)
|
||||
elif isinstance(config, dict) and "source" in config:
|
||||
config["source"] = config_names.get(config["source"], config["source"])
|
||||
if "services" in compose:
|
||||
for svc in compose["services"].values():
|
||||
if "build" in svc:
|
||||
@@ -268,6 +295,7 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
# Deploy stack to Docker Swarm
|
||||
cmd = [
|
||||
"docker", "stack", "deploy",
|
||||
"--detach=false",
|
||||
"--prune",
|
||||
"-c", tmp_compose_path,
|
||||
self.stack_name
|
||||
@@ -279,12 +307,27 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
swarmEnv = {k: v for k, v in dotenv_values(os.path.join(os.path.dirname(tmp_compose_path), ".env")).items()
|
||||
if v is not None}
|
||||
# merge_dicts(swarmEnv, env)
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
# , cwd=os.path.dirname(tmp_compose_path), env=swarmEnv)
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, cwd=os.path.dirname(tmp_compose_path))
|
||||
#, env=swarmEnv)
|
||||
os.remove(tmp_compose_path)
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Stack deploy failed: {result.stderr}")
|
||||
raise RuntimeError(f"Stack deploy failed: {result.stderr}")
|
||||
logger.info(f"docker stack deploy stdout: {result.stdout or '<empty>'}")
|
||||
logger.info(f"docker stack deploy stderr: {result.stderr or '<empty>'}")
|
||||
stderr_lower = result.stderr.lower()
|
||||
docker_error_markers = (
|
||||
"failed to ",
|
||||
"error response from daemon",
|
||||
"rpc error",
|
||||
"denied",
|
||||
"no such image",
|
||||
)
|
||||
if result.returncode != 0 or any(marker in stderr_lower for marker in docker_error_markers):
|
||||
deploy_output = (
|
||||
f"exit code: {result.returncode}\n"
|
||||
f"stdout:\n{result.stdout or '<empty>'}\n"
|
||||
f"stderr:\n{result.stderr or '<empty>'}"
|
||||
)
|
||||
logger.error(f"Stack deploy failed:\n{deploy_output}")
|
||||
raise RuntimeError(f"Stack deploy failed:\n{deploy_output}")
|
||||
logger.info(f"Stack deployed successfully: {result.stdout}")
|
||||
|
||||
# Check health of all deployed services
|
||||
@@ -308,4 +351,25 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
if not_running:
|
||||
raise BaseException(f"Services not started yet: {', '.join(not_running)}")
|
||||
|
||||
version_mismatches = []
|
||||
for svc_name, svc in expected_services.items():
|
||||
full_svc_name = f"{self.stack_name}_{svc_name}"
|
||||
services_list = client.services.list(filters={"name": full_svc_name})
|
||||
if not services_list:
|
||||
continue
|
||||
service_labels = services_list[0].attrs.get("Spec", {}).get("TaskTemplate", {}).get("ContainerSpec", {}).get("Labels", {})
|
||||
expected_version = svc.get("labels", {}).get("org.brencher.version")
|
||||
actual_version = service_labels.get("org.brencher.version")
|
||||
if expected_version != actual_version:
|
||||
version_mismatches.append(
|
||||
f"{full_svc_name} expected version {expected_version}, got {actual_version}"
|
||||
)
|
||||
update_status = services_list[0].attrs.get("UpdateStatus", {})
|
||||
if update_status.get("State") in {"paused", "rollback_paused", "rollback_started"}:
|
||||
version_mismatches.append(
|
||||
f"{full_svc_name} Swarm update {update_status.get('State')}: {update_status.get('Message', '')}"
|
||||
)
|
||||
if version_mismatches:
|
||||
raise RuntimeError(f"Deployment did not apply requested versions: {'; '.join(version_mismatches)}")
|
||||
|
||||
return f"Stack deployed successfully: {result.stdout}"
|
||||
|
||||
+125
-47
@@ -16,11 +16,62 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GitClone(AbstractStep[str]):
|
||||
def __init__(self, url: str, repo_path: str | None = None,
|
||||
branchNamePrefix: str = "", credEnvPrefix: str = "GIT",
|
||||
def __init__(self,
|
||||
url: str | List[str] | None = None,
|
||||
remotes: List[Dict[str, str]] | None = None,
|
||||
repo_path: str | None = None,
|
||||
branchNamePrefix: str = "",
|
||||
credEnvPrefix: str = "GIT",
|
||||
**kwargs: Any):
|
||||
super().__init__(**kwargs)
|
||||
self.url = url
|
||||
# `remotes` is the explicit form: a list of {"name": "...", "url": "..."}.
|
||||
# `url` is kept as a shorthand: a single string becomes one remote named
|
||||
# "origin"; a list of strings becomes remotes named "origin", "repo1",
|
||||
# "repo2", ... (matches the behavior introduced by the "multiple remotes"
|
||||
# change so existing positional-list callers keep working).
|
||||
if remotes is not None and url is not None:
|
||||
raise ValueError("Pass either 'url' or 'remotes', not both")
|
||||
|
||||
normalized: List[Dict[str, str]] = []
|
||||
if remotes is not None:
|
||||
seen: Set[str] = set()
|
||||
for entry in remotes:
|
||||
if not isinstance(entry, dict):
|
||||
raise TypeError(
|
||||
f"Each entry in 'remotes' must be a dict with 'name' and 'url', got {type(entry).__name__}"
|
||||
)
|
||||
name = entry.get("name")
|
||||
url_value = entry.get("url")
|
||||
if not name or not isinstance(name, str):
|
||||
raise ValueError("Each remote requires a non-empty 'name' (string)")
|
||||
if not url_value or not isinstance(url_value, str):
|
||||
raise ValueError(f"Remote {name!r} requires a non-empty 'url' (string)")
|
||||
if name in seen:
|
||||
raise ValueError(f"Duplicate remote name: {name!r}")
|
||||
prefix_value = entry.get("prefix")
|
||||
if prefix_value is not None and not isinstance(prefix_value, str):
|
||||
raise ValueError(
|
||||
f"Remote {name!r} 'prefix' must be a string when set, got {type(prefix_value).__name__}"
|
||||
)
|
||||
seen.add(name)
|
||||
normalized.append({"name": name, "url": url_value, "prefix": prefix_value or ""})
|
||||
elif url is not None:
|
||||
if isinstance(url, str):
|
||||
raw = [url]
|
||||
else:
|
||||
raw = list(url)
|
||||
for index, repository_url in enumerate(raw):
|
||||
name = "origin" if index == 0 else f"repo{index}"
|
||||
normalized.append({"name": name, "url": repository_url})
|
||||
else:
|
||||
raise ValueError("GitClone requires either 'url' or 'remotes'")
|
||||
|
||||
if len(normalized) == 0:
|
||||
raise ValueError("At least one repository URL is required")
|
||||
|
||||
self.remotes = normalized
|
||||
# Derived view used by repo_path keying and any introspection.
|
||||
self.urls = [r["url"] for r in normalized]
|
||||
self.repo_path = repo_path
|
||||
self.branchNamePrefix = branchNamePrefix
|
||||
self.credEnvPrefix = credEnvPrefix
|
||||
@@ -36,33 +87,38 @@ class GitClone(AbstractStep[str]):
|
||||
return url
|
||||
|
||||
def progress(self) -> str:
|
||||
repo_url = self.url
|
||||
|
||||
self.repo_path = self.repo_path or os.path.join(tempfile.gettempdir(),
|
||||
f"{self.env.id}_{hashlib.sha1(repo_url.encode()).hexdigest()[:5]}")
|
||||
logger.info(f"Cloning repository {repo_url} to {self.repo_path}")
|
||||
f"{self.env.id}_{hashlib.sha1('|'.join(self.urls).encode()).hexdigest()[:5]}")
|
||||
logger.info(f"Cloning repositories {self.remotes} to {self.repo_path}")
|
||||
os.makedirs(self.repo_path, exist_ok=True)
|
||||
try:
|
||||
if os.path.exists(os.path.join(self.repo_path, ".git")):
|
||||
logger.info(f"Repository already cloned at {self.repo_path}, fetching updates.")
|
||||
repo = git.Repo(self.repo_path)
|
||||
if 'origin' not in repo.remotes or repo.remotes.origin.url != self._get_auth_git_url(repo_url):
|
||||
repo.create_remote('origin', self._get_auth_git_url(repo_url))
|
||||
result = repo.remotes.origin.fetch(prune=True)
|
||||
if not result or any(fetch_info.flags & fetch_info.ERROR for fetch_info in result):
|
||||
raise BaseException(f"Failed to fetch updates for {repo_url}")
|
||||
else:
|
||||
repo = git.Repo.init(self.repo_path)
|
||||
repo.remotes.append(repo.create_remote(
|
||||
'origin',
|
||||
self._get_auth_git_url(repo_url)
|
||||
))
|
||||
if self.branchNamePrefix != "":
|
||||
repo.config_writer().set_value('remote "origin"', "fetch",
|
||||
f"+refs/heads/{self.branchNamePrefix}/*:refs/remotes/origin/{self.branchNamePrefix}/*").release()
|
||||
repo.remotes.origin.fetch(prune=True)
|
||||
if not os.path.exists(os.path.join(self.repo_path, ".git")):
|
||||
raise BaseException(f"Failed to clone repository {repo_url} to {self.repo_path}")
|
||||
|
||||
for entry in self.remotes:
|
||||
remote_name = entry["name"]
|
||||
repository_url = entry["url"]
|
||||
auth_url = self._get_auth_git_url(repository_url)
|
||||
if remote_name in repo.remotes:
|
||||
remote = repo.remotes[remote_name]
|
||||
if remote.url != auth_url:
|
||||
remote.set_url(auth_url)
|
||||
else:
|
||||
remote = repo.create_remote(remote_name, auth_url)
|
||||
prefix = entry.get("prefix", "")
|
||||
effective_prefix = prefix or (self.branchNamePrefix if remote_name == "origin" else "")
|
||||
if effective_prefix:
|
||||
with repo.config_writer() as config:
|
||||
config.set_value(f'remote "{remote_name}"', "fetch",
|
||||
f"+refs/heads/{effective_prefix}/*:refs/remotes/{remote_name}/{effective_prefix}/*")
|
||||
result = remote.fetch(prune=True)
|
||||
if not result or any(fetch_info.flags & fetch_info.ERROR for fetch_info in result):
|
||||
raise BaseException(f"Failed to fetch updates for {repository_url}")
|
||||
if not os.path.exists(os.path.join(self.repo_path, ".git")):
|
||||
raise BaseException(f"Failed to clone repositories {self.remotes} to {self.repo_path}")
|
||||
except BaseException as e:
|
||||
logger.error(f"Error during git clone/fetch, removing directory {self.repo_path}: {str(e)}")
|
||||
shutil.rmtree(self.repo_path)
|
||||
@@ -73,21 +129,24 @@ class GitClone(AbstractStep[str]):
|
||||
repo = git.Repo(self.repo_path)
|
||||
result: Dict[str, List[Any]] = {}
|
||||
for ref in repo.refs:
|
||||
if ref.name.startswith('origin/') and not ref.name.startswith('origin/HEAD'):
|
||||
branch_name = ref.name[len('origin/'):]
|
||||
if not branch_name.startswith('auto/'): # Skip auto branches
|
||||
result[branch_name] = []
|
||||
cmt = [ref.commit]
|
||||
for _ in range(10):
|
||||
for commit in cmt:
|
||||
result[branch_name].append({
|
||||
'hexsha': commit.hexsha,
|
||||
'author': commit.author.name,
|
||||
'date': commit.committed_datetime.isoformat(),
|
||||
'message': commit.message.strip()
|
||||
})
|
||||
if not ref.is_remote() or ref.name.endswith('/HEAD'):
|
||||
continue
|
||||
remote_name, branch_name = ref.name.split('/', 1)
|
||||
if branch_name.startswith('auto/'): # Skip auto branches
|
||||
continue
|
||||
branch_key = branch_name if remote_name == 'origin' else ref.name
|
||||
result[branch_key] = []
|
||||
cmt = [ref.commit]
|
||||
for _ in range(10):
|
||||
for commit in cmt:
|
||||
result[branch_key].append({
|
||||
'hexsha': commit.hexsha,
|
||||
'author': commit.author.name,
|
||||
'date': commit.committed_datetime.isoformat(),
|
||||
'message': commit.message.strip()
|
||||
})
|
||||
|
||||
cmt = [p for c in cmt for p in c.parents]
|
||||
cmt = [p for c in cmt for p in c.parents]
|
||||
return result
|
||||
|
||||
|
||||
@@ -112,6 +171,18 @@ def ensure_clean(repo: git.Repo) -> None:
|
||||
raise BaseException(f"Changes in repo: U{repo.untracked_files}")
|
||||
|
||||
|
||||
def _resolve_branch_ref(repo: git.Repo, branch_key: str, default_remote: str) -> str:
|
||||
# Branch keys can be "branch", "origin/branch", "gitea_admin/branch", etc.
|
||||
# If the key has a remote prefix that matches a known remote, use it
|
||||
# verbatim; otherwise fall back to the primary configured remote so
|
||||
# configs that don't name a remote `origin` still work.
|
||||
if "/" in branch_key:
|
||||
remote_name, _ = branch_key.split("/", 1)
|
||||
if remote_name in repo.remotes:
|
||||
return branch_key
|
||||
return f"{default_remote}/{branch_key}"
|
||||
|
||||
|
||||
class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
wd: GitClone
|
||||
|
||||
@@ -146,11 +217,19 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
def find_desired_commits(self, repo: git.Repo, branches: List[Tuple[str, str]]) -> Dict[Commit, str]:
|
||||
# Extract commit ids for the selected branches
|
||||
commit_ids: Dict[Commit, str] = {}
|
||||
# Default remote: prefer the first remote GitClone was configured with
|
||||
# (so non-"origin" configs like github/gitea_admin work without the
|
||||
# caller having to qualify every branch key).
|
||||
default_remote = "origin"
|
||||
if isinstance(getattr(self.wd, "remotes", None), list) and self.wd.remotes:
|
||||
primary = self.wd.remotes[0]["name"]
|
||||
if primary in repo.remotes:
|
||||
default_remote = primary
|
||||
for branch_pair in branches:
|
||||
try:
|
||||
branch_name, desired_commit = branch_pair
|
||||
if desired_commit == 'HEAD':
|
||||
commit = repo.commit(f'origin/{branch_name}')
|
||||
commit = repo.commit(_resolve_branch_ref(repo, branch_name, default_remote))
|
||||
else:
|
||||
commit = repo.commit(desired_commit)
|
||||
commit_ids[commit] = branch_name
|
||||
@@ -229,9 +308,10 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
version = '-'.join([x.hexsha[0:8] for x in sorted_commits])
|
||||
|
||||
for ref in repo.refs:
|
||||
if ref.is_remote() and ref.commit == repo.head.commit and ref.name != 'origin/HEAD':
|
||||
if ref.is_remote() and ref.commit == repo.head.commit and not ref.name.endswith('/HEAD'):
|
||||
logger.info(f"Merge commit {ref.commit} corresponds to branch {ref}")
|
||||
remote_branch_name = ref.name[len('origin/'):]
|
||||
remote_name, branch_name = ref.name.split('/', 1)
|
||||
remote_branch_name = branch_name if remote_name == 'origin' else ref.name
|
||||
break
|
||||
if self.push and remote_branch_name is None:
|
||||
auto_branch_hash = hashlib.sha1(''.join([x.hexsha for x in sorted_commits]).encode()).hexdigest()
|
||||
@@ -292,10 +372,9 @@ class GitUnmerge(AbstractStep[GitUnmergeResult]):
|
||||
commits: List[Tuple[str, str]] = []
|
||||
for v in version_parts:
|
||||
commit = repo.commit(v)
|
||||
# branches = [head.name for head in repo.heads if head.commit.hexsha == commit.hexsha]
|
||||
branches = [ref.name for ref in repo.remotes.origin.refs if ref.commit.hexsha == commit.hexsha]
|
||||
branches = [b[len('origin/'):] for b in branches if
|
||||
b.startswith('origin/') and not b.startswith('origin/HEAD')]
|
||||
branches = [ref.name for ref in repo.refs
|
||||
if ref.is_remote() and not ref.name.endswith('/HEAD') and ref.commit.hexsha == commit.hexsha]
|
||||
branches = [b[len('origin/'):] if b.startswith('origin/') else b for b in branches]
|
||||
|
||||
if len(branches) == 0 or branches[0].startswith('auto/'):
|
||||
if childs is None:
|
||||
@@ -304,10 +383,9 @@ class GitUnmerge(AbstractStep[GitUnmergeResult]):
|
||||
while len(commitsSet) > 0:
|
||||
current = commitsSet.pop()
|
||||
for child in childs.get(current, []):
|
||||
branches = [ref.name for ref in repo.remotes.origin.refs if
|
||||
ref.commit.hexsha == child.hexsha]
|
||||
branches = [b[len('origin/'):] for b in branches if
|
||||
b.startswith('origin/') and not b.startswith('origin/HEAD')]
|
||||
branches = [ref.name for ref in repo.refs
|
||||
if ref.is_remote() and not ref.name.endswith('/HEAD') and ref.commit.hexsha == child.hexsha]
|
||||
branches = [b[len('origin/'):] if b.startswith('origin/') else b for b in branches]
|
||||
|
||||
if len(branches) > 0:
|
||||
commitsSet = set()
|
||||
|
||||
@@ -42,8 +42,11 @@ class SharedStateHolderInMemory(AbstractStep[SharedState], SharedStateHolder):
|
||||
def progress(self) -> SharedState:
|
||||
if len(self.state.branches) == 0:
|
||||
if self.unmerge:
|
||||
unmerge = self.unmerge.progress()
|
||||
self.state = replace(self.state, branches=unmerge.branches, token=uuid.uuid4().hex)
|
||||
try:
|
||||
unmerge = self.unmerge.progress()
|
||||
self.state = replace(self.state, branches=unmerge.branches, token=uuid.uuid4().hex)
|
||||
except BaseException as e:
|
||||
logger.error("Unmerge failed: %s", e)
|
||||
return self.state
|
||||
|
||||
|
||||
|
||||
+15
-7
@@ -107,12 +107,12 @@ class WebApp:
|
||||
disconnected = set()
|
||||
message = custom_json_dumps({event: data})
|
||||
|
||||
for websocket in self.ws_connections.keys():
|
||||
if self.ws_connections[websocket].get(event) == message:
|
||||
for websocket, state in self.ws_connections.items():
|
||||
if state.get(event) == message:
|
||||
continue
|
||||
try:
|
||||
await websocket.send_text(message)
|
||||
self.ws_connections[websocket][event] = message
|
||||
state[event] = message
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending to websocket: {e}")
|
||||
disconnected.add(websocket)
|
||||
@@ -129,6 +129,7 @@ class WebApp:
|
||||
|
||||
async def broadcast_environments(self, data: Any) -> None:
|
||||
await self.broadcast("environments", data)
|
||||
await self.broadcast("version", self.get_version())
|
||||
|
||||
async def broadcast_error(self, data: Any) -> None:
|
||||
await self.broadcast("error", data)
|
||||
@@ -140,11 +141,16 @@ class WebApp:
|
||||
|
||||
# --- Routes ---
|
||||
|
||||
def get_version(self) -> str:
|
||||
return os.getenv('VERSION', 'unknown') or 'unknown'
|
||||
|
||||
async def serve_index(self) -> FileResponse:
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
async def serve_state(self) -> Response:
|
||||
return Response(content=custom_json_dumps(self.get_global_envs_to_emit()), media_type="application/json")
|
||||
state = self.get_global_envs_to_emit()
|
||||
state['version'] = self.get_version()
|
||||
return Response(content=custom_json_dumps(state), media_type="application/json")
|
||||
|
||||
async def serve_branches_route(self) -> Response:
|
||||
return Response(content=custom_json_dumps(self.get_global_branches_to_emit()), media_type="application/json")
|
||||
@@ -157,16 +163,18 @@ class WebApp:
|
||||
|
||||
async def websocket_endpoint(self, websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
self.ws_connections[websocket] = {}
|
||||
state = self.ws_connections.setdefault(websocket, {})
|
||||
|
||||
try:
|
||||
# Send initial state on connect and record it so duplicate broadcasts are suppressed
|
||||
branches_payload = custom_json_dumps({"branches": self.get_global_branches_to_emit()})
|
||||
envs_payload = custom_json_dumps({"environments": self.get_global_envs_to_emit()})
|
||||
version_payload = custom_json_dumps({"version": self.get_version()})
|
||||
await websocket.send_text(version_payload)
|
||||
await websocket.send_text(branches_payload)
|
||||
self.ws_connections[websocket]["branches"] = branches_payload
|
||||
state["branches"] = branches_payload
|
||||
await websocket.send_text(envs_payload)
|
||||
self.ws_connections[websocket]["environments"] = envs_payload
|
||||
state["environments"] = envs_payload
|
||||
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
|
||||
+2
-1
@@ -16,7 +16,8 @@ services:
|
||||
- PYTHONUNBUFFERED=1
|
||||
- VIRTUAL_HOST=brencher.rudolf.keenetic.link
|
||||
- LETSENCRYPT_HOST=brencher.rudolf.keenetic.link
|
||||
- PROFILES=brencher2,torrserv_proxy,immich,registry,gmail_mcp
|
||||
- VERSION=${version}
|
||||
- PROFILES=brencher2,torrserv_proxy,immich,gmail_mcp,hermes,hermes_infra,gitea
|
||||
- SECONDARY_BRENCHER=http://host.docker.internal:5002
|
||||
deploy:
|
||||
replicas: 1
|
||||
|
||||
@@ -13,6 +13,7 @@ const closeStatus = document.getElementById('close-status');
|
||||
const refreshBranchesBtn = document.getElementById('refresh-branches');
|
||||
const applyChangesBtn = document.getElementById('apply-changes');
|
||||
const branchFilter = document.getElementById('branch-filter');
|
||||
const appVersion = document.getElementById('app-version');
|
||||
|
||||
// Branches list (flattened) for UI rendering: [{ envId, envName, branch, commits: [] }]
|
||||
let branches = [];
|
||||
@@ -477,6 +478,8 @@ function setupWebSockets() {
|
||||
filterBranches();
|
||||
renderJobs();
|
||||
showStatus('Environments updated.');
|
||||
} else if ('version' in message) {
|
||||
appVersion.textContent = `Version: ${message.version || 'unknown'}`;
|
||||
} else if ('error' in message) {
|
||||
console.error('Error from server:', message.error);
|
||||
if (message.error && message.error.code === 'BRANCH_STATE_CONFLICT' && message.error.envId) {
|
||||
@@ -529,6 +532,14 @@ function updateEnvironment() {
|
||||
showStatus('Changes applied (pending server confirmation).');
|
||||
}
|
||||
// Initial load
|
||||
(function applyUrlFilter() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const initialFilter = params.get('filter');
|
||||
if (initialFilter && branchFilter) {
|
||||
branchFilter.value = initialFilter;
|
||||
filterBranches();
|
||||
}
|
||||
})();
|
||||
setupWebSockets();
|
||||
showStatus('Loading branches...');
|
||||
renderBranches();
|
||||
|
||||
+4
-3
@@ -1,8 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Brencher - Branch Merger</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
@@ -10,6 +10,7 @@
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>Brencher - Branch Merger</h1>
|
||||
<p class="version" id="app-version">Version: loading...</p>
|
||||
<div id="status-bar" class="status-bar hidden">
|
||||
<span id="status-message"></span>
|
||||
<button id="close-status">×</button>
|
||||
|
||||
Generated
+2137
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "brencher-frontend",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"generate-types": "openapi-typescript ../openapi.yaml -o src/types/api.ts",
|
||||
"prebuild": "npm run generate-types",
|
||||
"build": "vite build",
|
||||
"dev": "vite"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.3.12",
|
||||
"@types/react-dom": "^18.3.1",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
"openapi-typescript": "^7.4.4",
|
||||
"typescript": "^5.7.2",
|
||||
"vite": "^6.0.5"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
/* Base */
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background: #f8f9fa;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 960px;
|
||||
margin: 40px auto;
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
|
||||
padding: 32px;
|
||||
}
|
||||
|
||||
/* Header */
|
||||
header h1 {
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
|
||||
/* Status bar */
|
||||
.status-bar {
|
||||
background: #e9ecef;
|
||||
padding: 8px 16px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 16px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.close-status-btn {
|
||||
margin-left: auto;
|
||||
background: none;
|
||||
border: none;
|
||||
font-size: 1.2rem;
|
||||
cursor: pointer;
|
||||
color: #555;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.close-status-btn:hover {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
/* Branches section header */
|
||||
.branches-header {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.branches-header h2 {
|
||||
margin: 0;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.branches-controls-left {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.branches-controls-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
.refresh-button,
|
||||
.apply-button,
|
||||
.toggle-button {
|
||||
background: #28a745;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
padding: 8px 16px;
|
||||
cursor: pointer;
|
||||
font-size: 0.95rem;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.refresh-button:hover,
|
||||
.apply-button:hover,
|
||||
.toggle-button:hover {
|
||||
background: #218838;
|
||||
}
|
||||
|
||||
/* Filter */
|
||||
.filter-input {
|
||||
flex: 1;
|
||||
min-width: 220px;
|
||||
padding: 8px 12px;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
font-size: 0.95rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.filter-input:focus {
|
||||
outline: none;
|
||||
border-color: #007bff;
|
||||
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
|
||||
}
|
||||
|
||||
/* Branches list */
|
||||
.branches-list {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
color: #888;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* Environment block */
|
||||
.env-block {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.env-title {
|
||||
margin: 0 0 8px;
|
||||
color: #495057;
|
||||
}
|
||||
|
||||
/* Branches table */
|
||||
.branches-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.branches-table th {
|
||||
background: #f8f9fa;
|
||||
padding: 10px 8px;
|
||||
text-align: left;
|
||||
font-weight: 600;
|
||||
color: #495057;
|
||||
border-bottom: 2px solid #dee2e6;
|
||||
}
|
||||
|
||||
.branches-table td {
|
||||
padding: 10px 8px;
|
||||
vertical-align: middle;
|
||||
border-bottom: 1px solid #dee2e6;
|
||||
}
|
||||
|
||||
.branch-row:hover {
|
||||
background: #f8f9fa;
|
||||
}
|
||||
|
||||
.branch-name {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.desired-commit {
|
||||
margin-right: 6px;
|
||||
padding: 4px 8px;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.commit-input {
|
||||
padding: 4px 8px;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9em;
|
||||
width: 200px;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
input[type='checkbox'] {
|
||||
margin: 0;
|
||||
transform: scale(1.2);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Jobs section */
|
||||
.jobs-section {
|
||||
margin-top: 32px;
|
||||
}
|
||||
|
||||
.jobs-section h2 {
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
|
||||
.jobs-list {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.env-jobs {
|
||||
margin-bottom: 20px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.env-jobs h4 {
|
||||
margin: 0 0 8px;
|
||||
color: #495057;
|
||||
}
|
||||
|
||||
.job-item {
|
||||
margin-bottom: 8px;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 4px;
|
||||
padding: 8px 12px;
|
||||
}
|
||||
|
||||
.job-header {
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.job-header:hover {
|
||||
color: #007bff;
|
||||
}
|
||||
|
||||
.job-spoiler pre {
|
||||
background: #f8f9fa;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 4px;
|
||||
padding: 8px 12px;
|
||||
overflow-x: auto;
|
||||
font-size: 0.85em;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.job-spoiler a {
|
||||
color: #007bff;
|
||||
word-break: break-all;
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import './App.css';
|
||||
import { BranchesList } from './components/BranchesList';
|
||||
import type { BranchItem, ColumnsByEnv, SelectedBranches } from './components/BranchesList';
|
||||
import { JobsList } from './components/JobsList';
|
||||
import { StatusBar } from './components/StatusBar';
|
||||
import { useWebSocket } from './hooks/useWebSocket';
|
||||
import type { BranchCommitPair, EnvironmentDto } from './types/index';
|
||||
|
||||
export function App() {
|
||||
const { sendMessage, lastBranchesMessage, lastEnvironmentsMessage, lastError, connectionStatus } =
|
||||
useWebSocket();
|
||||
|
||||
const [branches, setBranches] = useState<BranchItem[]>([]);
|
||||
const [environments, setEnvironments] = useState<EnvironmentDto[]>([]);
|
||||
const [selectedBranchesByEnv, setSelectedBranchesByEnv] = useState<SelectedBranches>({});
|
||||
const [serverSelectedBranchesByEnv, setServerSelectedBranchesByEnv] = useState<SelectedBranches>({});
|
||||
const [columnsByEnv, setColumnsByEnv] = useState<ColumnsByEnv>({});
|
||||
const [filterText, setFilterText] = useState('');
|
||||
const [showAll, setShowAll] = useState(false);
|
||||
const [statusMessage, setStatusMessage] = useState<string | null>('Loading branches...');
|
||||
const [isStatusError, setIsStatusError] = useState(false);
|
||||
|
||||
const showStatus = useCallback((message: string, isError = false) => {
|
||||
setStatusMessage(message);
|
||||
setIsStatusError(isError);
|
||||
}, []);
|
||||
|
||||
// Handle incoming branches
|
||||
useEffect(() => {
|
||||
if (!lastBranchesMessage) return;
|
||||
const data = lastBranchesMessage;
|
||||
setBranches(() => {
|
||||
const next: BranchItem[] = Object.entries(data).flatMap(([envId, branchMap]) =>
|
||||
Object.entries(branchMap).map(([branchName, commitList]) => ({
|
||||
envId,
|
||||
branch: branchName,
|
||||
commits: commitList,
|
||||
}))
|
||||
);
|
||||
// Replace all branches with the latest data from the server
|
||||
return next;
|
||||
});
|
||||
showStatus('Branches updated.');
|
||||
}, [lastBranchesMessage, showStatus]);
|
||||
|
||||
// Handle incoming environments
|
||||
useEffect(() => {
|
||||
if (!lastEnvironmentsMessage) return;
|
||||
const payload = lastEnvironmentsMessage;
|
||||
const envList = Object.values(payload);
|
||||
setEnvironments(envList);
|
||||
|
||||
setSelectedBranchesByEnv((prevSelected) => {
|
||||
// We need server state inline to check pending changes; use functional update
|
||||
let nextSelected = prevSelected;
|
||||
setServerSelectedBranchesByEnv((prevServer) => {
|
||||
// Check if changes are pending before overwriting selections
|
||||
const hasPending = Object.keys(prevSelected).some((envId) => {
|
||||
const localSel = [...(prevSelected[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
const serverSel = [...(prevServer[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
return JSON.stringify(localSel) !== JSON.stringify(serverSel);
|
||||
});
|
||||
|
||||
const nextServerState = { ...prevServer };
|
||||
const updatedSelected = { ...prevSelected };
|
||||
|
||||
envList.forEach((envObj) => {
|
||||
const envId = envObj.id;
|
||||
if (!hasPending) {
|
||||
if (Array.isArray(envObj.branches) && envObj.branches.length > 0) {
|
||||
const pairs: BranchCommitPair[] = Array.isArray(envObj.branches[0])
|
||||
? (envObj.branches as BranchCommitPair[])
|
||||
: envObj.branches.map((b) => [b as unknown as string, 'HEAD'] as BranchCommitPair);
|
||||
updatedSelected[envId] = [...pairs];
|
||||
nextServerState[envId] = [...pairs];
|
||||
} else if (!updatedSelected[envId]) {
|
||||
updatedSelected[envId] = [];
|
||||
nextServerState[envId] = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
nextSelected = updatedSelected;
|
||||
return nextServerState;
|
||||
});
|
||||
|
||||
return nextSelected;
|
||||
});
|
||||
|
||||
// Extract column data from pipeline steps
|
||||
setColumnsByEnv((prev) => {
|
||||
const next = { ...prev };
|
||||
envList.forEach((envObj) => {
|
||||
const envId = envObj.id;
|
||||
next[envId] = {};
|
||||
const jobsArr = envObj.pipeline ?? [];
|
||||
jobsArr.forEach((job) => {
|
||||
if (job.status == null || job.error) return;
|
||||
const status = job.status;
|
||||
if (
|
||||
typeof status === 'object' &&
|
||||
!Array.isArray(status) &&
|
||||
status !== null &&
|
||||
'columns' in status
|
||||
) {
|
||||
const cols = (status as Record<string, unknown>).columns;
|
||||
if (cols && typeof cols === 'object' && !Array.isArray(cols)) {
|
||||
Object.entries(cols as Record<string, unknown>).forEach(([colName, colData]) => {
|
||||
if (colData && typeof colData === 'object' && !Array.isArray(colData)) {
|
||||
next[envId][colName] = colData as Record<string, string>;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
return next;
|
||||
});
|
||||
|
||||
showStatus('Environments updated.');
|
||||
}, [lastEnvironmentsMessage, showStatus]);
|
||||
|
||||
// Handle incoming errors
|
||||
useEffect(() => {
|
||||
if (!lastError) return;
|
||||
showStatus(lastError.message || 'Unknown error', true);
|
||||
}, [lastError, showStatus]);
|
||||
|
||||
// Show connection status changes
|
||||
useEffect(() => {
|
||||
if (connectionStatus === 'disconnected') {
|
||||
showStatus('Disconnected from server.', true);
|
||||
}
|
||||
}, [connectionStatus, showStatus]);
|
||||
|
||||
const isChangesPending = useMemo(() => {
|
||||
return Object.keys(selectedBranchesByEnv).some((envId) => {
|
||||
const localSel = [...(selectedBranchesByEnv[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
const serverSel = [...(serverSelectedBranchesByEnv[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
return JSON.stringify(localSel) !== JSON.stringify(serverSel);
|
||||
});
|
||||
}, [selectedBranchesByEnv, serverSelectedBranchesByEnv]);
|
||||
|
||||
const filteredBranches = useMemo(() => {
|
||||
const text = filterText.toLowerCase().trim();
|
||||
return branches.filter(({ envId, branch, commits }) => {
|
||||
const selList = selectedBranchesByEnv[envId] ?? [];
|
||||
const isSelected = selList.some(([b]) => b === branch);
|
||||
|
||||
const envCols = columnsByEnv[envId] ?? {};
|
||||
const isInColumns = Object.values(envCols).some((colData) => branch in colData);
|
||||
|
||||
if (showAll) return true;
|
||||
if (isSelected || isInColumns) return true;
|
||||
if (!text) return false;
|
||||
|
||||
if (branch.toLowerCase().includes(text)) return true;
|
||||
return commits.some((c) => {
|
||||
const shortHash = c.hexsha ? c.hexsha.substring(0, 8).toLowerCase() : '';
|
||||
return (
|
||||
c.hexsha?.toLowerCase().includes(text) ||
|
||||
shortHash.includes(text) ||
|
||||
c.message?.toLowerCase().includes(text) ||
|
||||
c.author?.toLowerCase().includes(text)
|
||||
);
|
||||
});
|
||||
});
|
||||
}, [branches, filterText, showAll, selectedBranchesByEnv, columnsByEnv]);
|
||||
|
||||
const handleSelectionChange = useCallback((envId: string, updated: BranchCommitPair[]) => {
|
||||
setSelectedBranchesByEnv((prev) => ({ ...prev, [envId]: updated }));
|
||||
}, []);
|
||||
|
||||
const handleApplyChanges = useCallback(() => {
|
||||
Object.keys(selectedBranchesByEnv).forEach((envId) => {
|
||||
const localSel = [...(selectedBranchesByEnv[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
const serverSel = [...(serverSelectedBranchesByEnv[envId] ?? [])].sort((a, b) =>
|
||||
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
||||
);
|
||||
if (JSON.stringify(localSel) !== JSON.stringify(serverSel)) {
|
||||
sendMessage({ update: { id: envId, branches: selectedBranchesByEnv[envId] } });
|
||||
setServerSelectedBranchesByEnv((prev) => ({
|
||||
...prev,
|
||||
[envId]: [...(selectedBranchesByEnv[envId] ?? [])],
|
||||
}));
|
||||
}
|
||||
});
|
||||
setFilterText('');
|
||||
showStatus('Changes applied (pending server confirmation).');
|
||||
}, [selectedBranchesByEnv, serverSelectedBranchesByEnv, sendMessage, showStatus]);
|
||||
|
||||
const handleRefresh = useCallback(() => {
|
||||
sendMessage({ update: { id: '' } });
|
||||
showStatus('Refreshing...');
|
||||
}, [sendMessage, showStatus]);
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<header>
|
||||
<h1>Brencher - Branch Merger</h1>
|
||||
<StatusBar
|
||||
message={statusMessage}
|
||||
isError={isStatusError}
|
||||
onClose={() => setStatusMessage(null)}
|
||||
/>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<div className="branches-section">
|
||||
<div className="branches-header">
|
||||
<h2>Git Branches</h2>
|
||||
<div className="branches-controls-left">
|
||||
<input
|
||||
type="text"
|
||||
className="filter-input"
|
||||
placeholder="Filter branches by name, commit ID, or commit message..."
|
||||
value={filterText}
|
||||
onChange={(e) => setFilterText(e.target.value)}
|
||||
/>
|
||||
<button
|
||||
className="toggle-button"
|
||||
onClick={() => setShowAll((v) => !v)}
|
||||
>
|
||||
{showAll ? 'Show Filtered' : 'Show All'}
|
||||
</button>
|
||||
</div>
|
||||
<div className="branches-controls-right">
|
||||
{isChangesPending && (
|
||||
<button className="apply-button" onClick={handleApplyChanges}>
|
||||
Apply Changes
|
||||
</button>
|
||||
)}
|
||||
<button className="refresh-button" onClick={handleRefresh}>
|
||||
Refresh Branches
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="branches-list">
|
||||
<BranchesList
|
||||
branches={filteredBranches}
|
||||
environments={environments}
|
||||
selectedBranchesByEnv={selectedBranchesByEnv}
|
||||
columnsByEnv={columnsByEnv}
|
||||
onSelectionChange={handleSelectionChange}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="jobs-section">
|
||||
<h2>Jobs</h2>
|
||||
<JobsList environments={environments} />
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
import type { BranchCommitPair, CommitInfo, EnvironmentDto } from '../types/index';
|
||||
|
||||
export interface BranchItem {
|
||||
envId: string;
|
||||
branch: string;
|
||||
commits: CommitInfo[];
|
||||
}
|
||||
|
||||
export type SelectedBranches = Record<string, BranchCommitPair[]>;
|
||||
export type ColumnsByEnv = Record<string, Record<string, Record<string, string>>>;
|
||||
|
||||
interface BranchesListProps {
|
||||
branches: BranchItem[];
|
||||
environments: EnvironmentDto[];
|
||||
selectedBranchesByEnv: SelectedBranches;
|
||||
columnsByEnv: ColumnsByEnv;
|
||||
onSelectionChange: (envId: string, branches: BranchCommitPair[]) => void;
|
||||
}
|
||||
|
||||
function getCommitOptions(commits: CommitInfo[], desiredCommit: string): { value: string; label: string; selected: boolean }[] {
|
||||
const opts: { value: string; label: string; selected: boolean }[] = [];
|
||||
|
||||
if (commits.length > 0) {
|
||||
const head = commits[0];
|
||||
const headShort = head.hexsha ? head.hexsha.substring(0, 8) : '';
|
||||
const headAuthor = (head.author || '').substring(0, 25);
|
||||
const headMsg = head.message
|
||||
? head.message.substring(0, 50) + (head.message.length > 50 ? '...' : '')
|
||||
: '';
|
||||
opts.push({
|
||||
value: 'HEAD',
|
||||
label: `HEAD (${headShort} - ${headAuthor} - ${headMsg})`,
|
||||
selected: desiredCommit === 'HEAD',
|
||||
});
|
||||
for (const c of commits) {
|
||||
const full = c.hexsha;
|
||||
const shortHash = full ? full.substring(0, 8) : '';
|
||||
const author = (c.author || '').substring(0, 25);
|
||||
const msg = c.message
|
||||
? c.message.substring(0, 50) + (c.message.length > 50 ? '...' : '')
|
||||
: '';
|
||||
opts.push({
|
||||
value: full,
|
||||
label: `${shortHash} - ${author} - ${msg}`,
|
||||
selected: desiredCommit === full || desiredCommit === shortHash,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
opts.push({ value: 'HEAD', label: 'HEAD (no commits)', selected: desiredCommit === 'HEAD' });
|
||||
}
|
||||
|
||||
const hasMatch = commits.some(
|
||||
(c) => desiredCommit === c.hexsha || desiredCommit === c.hexsha?.substring(0, 8) || desiredCommit === 'HEAD'
|
||||
);
|
||||
const isCustom = desiredCommit && desiredCommit !== 'HEAD' && !hasMatch;
|
||||
opts.push({ value: 'custom', label: 'Custom Commit', selected: isCustom });
|
||||
return opts;
|
||||
}
|
||||
|
||||
function branchHasCommit(commits: CommitInfo[], commit: string): boolean {
|
||||
if (commit === 'HEAD') return true;
|
||||
return commits.some((c) => c.hexsha === commit || c.hexsha?.substring(0, 8) === commit);
|
||||
}
|
||||
|
||||
interface BranchRowProps {
|
||||
envId: string;
|
||||
branch: string;
|
||||
commits: CommitInfo[];
|
||||
selectedBranches: BranchCommitPair[];
|
||||
colNames: string[];
|
||||
colData: Record<string, Record<string, string>>;
|
||||
onSelectionChange: (branches: BranchCommitPair[]) => void;
|
||||
}
|
||||
|
||||
function BranchRow({ envId, branch, commits, selectedBranches, colNames, colData, onSelectionChange }: BranchRowProps) {
|
||||
const pair = selectedBranches.find(([b]) => b === branch);
|
||||
const isChecked = !!pair;
|
||||
const desiredCommit = pair ? pair[1] : 'HEAD';
|
||||
const isCustom = desiredCommit !== 'HEAD' && !branchHasCommit(commits, desiredCommit);
|
||||
|
||||
const updateCommit = (commit: string) => {
|
||||
const updated = selectedBranches.map(([b, c]) => (b === branch ? [b, commit] as BranchCommitPair : [b, c] as BranchCommitPair));
|
||||
onSelectionChange(updated);
|
||||
};
|
||||
|
||||
const handleCheckbox = (checked: boolean) => {
|
||||
if (checked) {
|
||||
if (!selectedBranches.some(([b]) => b === branch)) {
|
||||
onSelectionChange([...selectedBranches, [branch, 'HEAD']]);
|
||||
}
|
||||
} else {
|
||||
onSelectionChange(selectedBranches.filter(([b]) => b !== branch));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDropdownChange = (value: string) => {
|
||||
if (value === 'custom') {
|
||||
// show text input - handled via isCustom state derived from desiredCommit
|
||||
updateCommit('');
|
||||
} else if (value === 'HEAD') {
|
||||
updateCommit('HEAD');
|
||||
} else {
|
||||
updateCommit(value);
|
||||
}
|
||||
};
|
||||
|
||||
const dropdownValue = isCustom ? 'custom' : desiredCommit;
|
||||
const options = getCommitOptions(commits, desiredCommit);
|
||||
|
||||
return (
|
||||
<tr className="branch-row" data-env={envId} data-branch={branch}>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={isChecked}
|
||||
onChange={(e) => handleCheckbox(e.target.checked)}
|
||||
/>
|
||||
</td>
|
||||
<td className="branch-name">{branch}</td>
|
||||
<td>
|
||||
<select
|
||||
className="desired-commit"
|
||||
value={dropdownValue}
|
||||
onChange={(e) => handleDropdownChange(e.target.value)}
|
||||
>
|
||||
{options.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>{opt.label}</option>
|
||||
))}
|
||||
</select>
|
||||
{isCustom && (
|
||||
<input
|
||||
type="text"
|
||||
className="commit-input"
|
||||
value={desiredCommit}
|
||||
placeholder="Enter commit ID"
|
||||
onChange={(e) => updateCommit(e.target.value)}
|
||||
onBlur={(e) => {
|
||||
const val = e.target.value.trim();
|
||||
updateCommit(val || 'HEAD');
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</td>
|
||||
{colNames.map((colName) => (
|
||||
<td key={colName}>{colData[colName]?.[branch] ?? ''}</td>
|
||||
))}
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
export function BranchesList({
|
||||
branches,
|
||||
environments: _environments,
|
||||
selectedBranchesByEnv,
|
||||
columnsByEnv,
|
||||
onSelectionChange,
|
||||
}: BranchesListProps) {
|
||||
if (branches.length === 0) {
|
||||
return <p className="loading">No branches found.</p>;
|
||||
}
|
||||
|
||||
// Group branches by envId
|
||||
const groups = branches.reduce<Record<string, BranchItem[]>>((acc, b) => {
|
||||
acc[b.envId] = acc[b.envId] ?? [];
|
||||
acc[b.envId].push(b);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return (
|
||||
<>
|
||||
{Object.entries(groups).map(([envId, rows]) => {
|
||||
const envCols = columnsByEnv[envId] ?? {};
|
||||
const colNames = Object.keys(envCols);
|
||||
const selectedBranches = selectedBranchesByEnv[envId] ?? [];
|
||||
|
||||
return (
|
||||
<div key={envId} className="env-block">
|
||||
<h3 className="env-title">Environment: {envId}</h3>
|
||||
<table className="branches-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Deploy</th>
|
||||
<th>Branch</th>
|
||||
<th>Desired Commit</th>
|
||||
{colNames.map((n) => <th key={n}>{n}</th>)}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map(({ branch, commits }) => (
|
||||
<BranchRow
|
||||
key={`${envId}-${branch}`}
|
||||
envId={envId}
|
||||
branch={branch}
|
||||
commits={commits}
|
||||
selectedBranches={selectedBranches}
|
||||
colNames={colNames}
|
||||
colData={envCols}
|
||||
onSelectionChange={(updated) => onSelectionChange(envId, updated)}
|
||||
/>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
import { useRef, useState } from 'react';
|
||||
import type { EnvironmentDto, PipelineStep } from '../types/index';
|
||||
|
||||
interface JobsListProps {
|
||||
environments: EnvironmentDto[];
|
||||
}
|
||||
|
||||
function extractUserLinks(obj: unknown): [string, string][] {
|
||||
if (!obj || typeof obj !== 'object') return [];
|
||||
const result: [string, string][] = [];
|
||||
const rec = obj as Record<string, unknown>;
|
||||
if (rec.userLinks && typeof rec.userLinks === 'object' && !Array.isArray(rec.userLinks)) {
|
||||
const links = rec.userLinks as Record<string, unknown>;
|
||||
for (const [title, url] of Object.entries(links)) {
|
||||
if (typeof url === 'string') result.push([title, url]);
|
||||
}
|
||||
}
|
||||
for (const key of Object.keys(rec)) {
|
||||
if (typeof rec[key] === 'object') {
|
||||
result.push(...extractUserLinks(rec[key]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function escapeHtml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function isValidHttpUrl(url: string): boolean {
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function linkifyText(text: string): string {
|
||||
const escaped = escapeHtml(text);
|
||||
return escaped.replace(
|
||||
/(https?:\/\/[^\s&"'<>]+)/g,
|
||||
(url) => {
|
||||
// Decode HTML entities that may have been escaped (e.g. & → &)
|
||||
const rawUrl = url.replace(/&/g, '&');
|
||||
if (!isValidHttpUrl(rawUrl)) return url;
|
||||
const safeHref = escapeHtml(rawUrl);
|
||||
return `<a href="${safeHref}" target="_blank" rel="noopener noreferrer">${url}</a>`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
interface JobItemProps {
|
||||
envId: string;
|
||||
job: PipelineStep;
|
||||
defaultOpen: boolean;
|
||||
}
|
||||
|
||||
function JobItem({ envId, job, defaultOpen }: JobItemProps) {
|
||||
const [open, setOpen] = useState(defaultOpen);
|
||||
const initialOpenRef = useRef(defaultOpen);
|
||||
|
||||
// Keep error jobs open by default (tracked via ref so it doesn't reset on re-render)
|
||||
const isOpen = job.error ? (initialOpenRef.current ? true : open) : open;
|
||||
|
||||
const statusJson = JSON.stringify(job.status, null, 2);
|
||||
const statusHtml = linkifyText(statusJson);
|
||||
|
||||
return (
|
||||
<div className="job-item">
|
||||
<div
|
||||
className="job-header"
|
||||
style={{ cursor: 'pointer', fontWeight: 'bold' }}
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
>
|
||||
{job.error ? (
|
||||
<span style={{ color: '#dc3545', fontWeight: 'bold', marginRight: 6 }} title="Error">!</span>
|
||||
) : (
|
||||
<span style={{ color: '#28a745', fontWeight: 'bold', marginRight: 6 }} title="OK">✔</span>
|
||||
)}
|
||||
{envId} - {job.name}
|
||||
</div>
|
||||
{isOpen && (
|
||||
<div className="job-spoiler" style={{ marginTop: 8 }}>
|
||||
<pre dangerouslySetInnerHTML={{ __html: statusHtml }} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function JobsList({ environments }: JobsListProps) {
|
||||
if (environments.length === 0) {
|
||||
return (
|
||||
<div className="jobs-list">
|
||||
<p className="loading">No jobs found.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="jobs-list">
|
||||
{environments.map((envObj) => {
|
||||
const jobsArr = envObj.pipeline ?? [];
|
||||
|
||||
const allLinks: [string, string][] = jobsArr.flatMap((job) =>
|
||||
extractUserLinks(job.status)
|
||||
);
|
||||
|
||||
return (
|
||||
<div key={envObj.id} className="env-jobs">
|
||||
<h4>Environment: {envObj.id}</h4>
|
||||
{allLinks.length > 0 && (
|
||||
<div style={{ float: 'right' }}>
|
||||
{allLinks.map(([title, url], i) => (
|
||||
<span key={i}>
|
||||
{i > 0 && ' | '}
|
||||
<a href={url} target="_blank" rel="noopener noreferrer">{title}</a>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{jobsArr.length > 0 ? (
|
||||
jobsArr.map((job, idx) => (
|
||||
<JobItem
|
||||
key={`${envObj.id}::${job.name}::${idx}`}
|
||||
envId={envObj.id}
|
||||
job={job}
|
||||
defaultOpen={job.error === true}
|
||||
/>
|
||||
))
|
||||
) : (
|
||||
<div className="job-item">No jobs found.</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
interface StatusBarProps {
|
||||
message: string | null;
|
||||
isError?: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
export function StatusBar({ message, isError = false, onClose }: StatusBarProps) {
|
||||
if (message === null) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="status-bar"
|
||||
style={{ background: isError ? '#f8d7da' : '#e9ecef' }}
|
||||
>
|
||||
<span style={{ color: isError ? '#721c24' : '#333' }}>{message}</span>
|
||||
<button className="close-status-btn" onClick={onClose}>×</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import type { BranchesData, EnvironmentsData, WsOutgoingMessage } from '../types/index';
|
||||
|
||||
export type ConnectionStatus = 'connecting' | 'connected' | 'disconnected';
|
||||
|
||||
export interface UseWebSocketReturn {
|
||||
sendMessage: (msg: WsOutgoingMessage) => void;
|
||||
lastBranchesMessage: BranchesData | null;
|
||||
lastEnvironmentsMessage: EnvironmentsData | null;
|
||||
lastError: { message: string } | null;
|
||||
connectionStatus: ConnectionStatus;
|
||||
}
|
||||
|
||||
export function useWebSocket(): UseWebSocketReturn {
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
const reconnectTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
|
||||
const [lastBranchesMessage, setLastBranchesMessage] = useState<BranchesData | null>(null);
|
||||
const [lastEnvironmentsMessage, setLastEnvironmentsMessage] = useState<EnvironmentsData | null>(null);
|
||||
const [lastError, setLastError] = useState<{ message: string } | null>(null);
|
||||
const [connectionStatus, setConnectionStatus] = useState<ConnectionStatus>('connecting');
|
||||
|
||||
const connect = useCallback(() => {
|
||||
if (wsRef.current && wsRef.current.readyState !== WebSocket.CLOSED) {
|
||||
wsRef.current.onclose = null;
|
||||
wsRef.current.close();
|
||||
}
|
||||
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const host = window.location.host;
|
||||
const ws = new WebSocket(`${protocol}//${host}/ws`);
|
||||
wsRef.current = ws;
|
||||
setConnectionStatus('connecting');
|
||||
|
||||
ws.onopen = () => {
|
||||
setConnectionStatus('connected');
|
||||
};
|
||||
|
||||
ws.onmessage = (event: MessageEvent) => {
|
||||
try {
|
||||
const message: unknown = JSON.parse(event.data as string);
|
||||
if (message && typeof message === 'object') {
|
||||
const msg = message as Record<string, unknown>;
|
||||
if ('branches' in msg) {
|
||||
setLastBranchesMessage(msg.branches as BranchesData);
|
||||
} else if ('environments' in msg) {
|
||||
setLastEnvironmentsMessage(msg.environments as EnvironmentsData);
|
||||
} else if ('error' in msg) {
|
||||
setLastError(msg.error as { message: string });
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error processing WebSocket message:', e);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
setConnectionStatus('disconnected');
|
||||
reconnectTimerRef.current = setTimeout(() => connect(), 5000);
|
||||
};
|
||||
|
||||
ws.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
connect();
|
||||
return () => {
|
||||
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
|
||||
if (wsRef.current) {
|
||||
wsRef.current.onclose = null;
|
||||
wsRef.current.close();
|
||||
}
|
||||
};
|
||||
}, [connect]);
|
||||
|
||||
const sendMessage = useCallback((msg: WsOutgoingMessage) => {
|
||||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
||||
wsRef.current.send(JSON.stringify(msg));
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { sendMessage, lastBranchesMessage, lastEnvironmentsMessage, lastError, connectionStatus };
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { StrictMode } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { App } from './App';
|
||||
|
||||
const rootEl = document.getElementById('root');
|
||||
if (!rootEl) throw new Error('Root element not found');
|
||||
|
||||
createRoot(rootEl).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>
|
||||
);
|
||||
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* This file was auto-generated by openapi-typescript.
|
||||
* Do not make direct changes to the file.
|
||||
*/
|
||||
|
||||
export interface paths {
|
||||
"/state": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
/** Get current environment state */
|
||||
get: operations["getState"];
|
||||
put?: never;
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/branches": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
/** Get branches for all environments */
|
||||
get: operations["getBranches"];
|
||||
put?: never;
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
}
|
||||
export type webhooks = Record<string, never>;
|
||||
export interface components {
|
||||
schemas: {
|
||||
CommitInfo: {
|
||||
/** @description Full commit SHA */
|
||||
hexsha: string;
|
||||
/** @description Commit author name */
|
||||
author: string;
|
||||
/** @description Commit message */
|
||||
message: string;
|
||||
};
|
||||
/** @description Tuple of [branchName, commitHash] */
|
||||
BranchCommitPair: string[];
|
||||
PipelineStep: {
|
||||
/** @description Step name */
|
||||
name: string;
|
||||
/** @description Step status (any shape) */
|
||||
status: unknown;
|
||||
/** @description Whether this step has an error */
|
||||
error?: boolean;
|
||||
};
|
||||
EnvironmentDto: {
|
||||
/** @description Environment identifier */
|
||||
id: string;
|
||||
/** @description Array of [branchName, commitHash] pairs */
|
||||
branches: components["schemas"]["BranchCommitPair"][];
|
||||
/** @description Whether this is a dry-run environment */
|
||||
dry: boolean;
|
||||
/** @description Repository path or URL */
|
||||
repo: string;
|
||||
/** @description Pipeline steps with their statuses */
|
||||
pipeline: components["schemas"]["PipelineStep"][];
|
||||
};
|
||||
WsIncomingMessage: {
|
||||
branches?: components["schemas"]["BranchesData"];
|
||||
environments?: components["schemas"]["EnvironmentsData"];
|
||||
error?: {
|
||||
message?: string;
|
||||
};
|
||||
};
|
||||
WsUpdatePayload: {
|
||||
/** @description Environment ID (empty string to trigger refresh) */
|
||||
id: string;
|
||||
branches?: components["schemas"]["BranchCommitPair"][];
|
||||
};
|
||||
WsOutgoingMessage: {
|
||||
update: components["schemas"]["WsUpdatePayload"];
|
||||
};
|
||||
/** @description Dict[envId, Dict[branchName, CommitInfo[]]] */
|
||||
BranchesData: {
|
||||
[key: string]: {
|
||||
[key: string]: components["schemas"]["CommitInfo"][];
|
||||
};
|
||||
};
|
||||
/** @description Dict[envId, EnvironmentDto] */
|
||||
EnvironmentsData: {
|
||||
[key: string]: components["schemas"]["EnvironmentDto"];
|
||||
};
|
||||
StateResponse: components["schemas"]["EnvironmentsData"];
|
||||
BranchesResponse: components["schemas"]["BranchesData"];
|
||||
};
|
||||
responses: never;
|
||||
parameters: never;
|
||||
requestBodies: never;
|
||||
headers: never;
|
||||
pathItems: never;
|
||||
}
|
||||
export type $defs = Record<string, never>;
|
||||
export interface operations {
|
||||
getState: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description Current state of all environments */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["StateResponse"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
getBranches: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description Branches grouped by environment and branch name */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["BranchesResponse"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { components } from './api';
|
||||
|
||||
export type CommitInfo = components['schemas']['CommitInfo'];
|
||||
export type BranchCommitPair = [string, string];
|
||||
export type PipelineStep = components['schemas']['PipelineStep'];
|
||||
export type EnvironmentDto = components['schemas']['EnvironmentDto'];
|
||||
export type BranchesData = components['schemas']['BranchesData'];
|
||||
export type EnvironmentsData = components['schemas']['EnvironmentsData'];
|
||||
export type WsIncomingMessage = components['schemas']['WsIncomingMessage'];
|
||||
export type WsUpdatePayload = components['schemas']['WsUpdatePayload'];
|
||||
export type WsOutgoingMessage = components['schemas']['WsOutgoingMessage'];
|
||||
@@ -15,6 +15,13 @@ body {
|
||||
padding: 32px;
|
||||
}
|
||||
|
||||
.version {
|
||||
color: #6c757d;
|
||||
font-family: monospace;
|
||||
font-size: 0.85rem;
|
||||
margin-top: -12px;
|
||||
}
|
||||
|
||||
.primary-button {
|
||||
background: #007bff;
|
||||
color: #fff;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"isolatedModules": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "./tsconfig.node.json" }]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"lib": ["ES2023"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"isolatedModules": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { defineConfig } from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
|
||||
const backendUrl = process.env.VITE_BACKEND_URL ?? 'http://localhost:5001';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
sourcemap: true,
|
||||
},
|
||||
server: {
|
||||
proxy: {
|
||||
'/ws': {
|
||||
target: backendUrl,
|
||||
ws: true,
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/api': {
|
||||
target: backendUrl,
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: Brencher API
|
||||
version: 1.0.0
|
||||
description: |
|
||||
Brencher branch merger API.
|
||||
|
||||
## WebSocket /ws
|
||||
|
||||
Connect to `/ws` for real-time updates.
|
||||
|
||||
### Incoming messages (server → client)
|
||||
|
||||
**Branches update:**
|
||||
```json
|
||||
{ "branches": { "<envId>": { "<branchName>": [CommitInfo, ...] } } }
|
||||
```
|
||||
|
||||
**Environments update:**
|
||||
```json
|
||||
{ "environments": { "<envId>": EnvironmentDto } }
|
||||
```
|
||||
|
||||
**Error:**
|
||||
```json
|
||||
{ "error": { "message": "string" } }
|
||||
```
|
||||
|
||||
### Outgoing messages (client → server)
|
||||
|
||||
**Update environment branches:**
|
||||
```json
|
||||
{ "update": { "id": "<envId>", "branches": [["<branchName>", "<commitHash>"], ...] } }
|
||||
```
|
||||
|
||||
**Refresh (send empty id to trigger refresh):**
|
||||
```json
|
||||
{ "update": { "id": "" } }
|
||||
```
|
||||
|
||||
paths:
|
||||
/state:
|
||||
get:
|
||||
operationId: getState
|
||||
summary: Get current environment state
|
||||
responses:
|
||||
"200":
|
||||
description: Current state of all environments
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/StateResponse"
|
||||
|
||||
/branches:
|
||||
get:
|
||||
operationId: getBranches
|
||||
summary: Get branches for all environments
|
||||
responses:
|
||||
"200":
|
||||
description: Branches grouped by environment and branch name
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/BranchesResponse"
|
||||
|
||||
components:
|
||||
schemas:
|
||||
CommitInfo:
|
||||
type: object
|
||||
required:
|
||||
- hexsha
|
||||
- author
|
||||
- message
|
||||
properties:
|
||||
hexsha:
|
||||
type: string
|
||||
description: Full commit SHA
|
||||
author:
|
||||
type: string
|
||||
description: Commit author name
|
||||
message:
|
||||
type: string
|
||||
description: Commit message
|
||||
|
||||
BranchCommitPair:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
minItems: 2
|
||||
maxItems: 2
|
||||
description: Tuple of [branchName, commitHash]
|
||||
|
||||
PipelineStep:
|
||||
type: object
|
||||
required:
|
||||
- name
|
||||
- status
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
description: Step name
|
||||
status:
|
||||
description: Step status (any shape)
|
||||
error:
|
||||
type: boolean
|
||||
description: Whether this step has an error
|
||||
|
||||
EnvironmentDto:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- branches
|
||||
- dry
|
||||
- repo
|
||||
- pipeline
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
description: Environment identifier
|
||||
branches:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/BranchCommitPair"
|
||||
description: Array of [branchName, commitHash] pairs
|
||||
dry:
|
||||
type: boolean
|
||||
description: Whether this is a dry-run environment
|
||||
repo:
|
||||
type: string
|
||||
description: Repository path or URL
|
||||
pipeline:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/PipelineStep"
|
||||
description: Pipeline steps with their statuses
|
||||
|
||||
WsIncomingMessage:
|
||||
type: object
|
||||
properties:
|
||||
branches:
|
||||
$ref: "#/components/schemas/BranchesData"
|
||||
environments:
|
||||
$ref: "#/components/schemas/EnvironmentsData"
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
message:
|
||||
type: string
|
||||
|
||||
WsUpdatePayload:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
description: Environment ID (empty string to trigger refresh)
|
||||
branches:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/BranchCommitPair"
|
||||
|
||||
WsOutgoingMessage:
|
||||
type: object
|
||||
required:
|
||||
- update
|
||||
properties:
|
||||
update:
|
||||
$ref: "#/components/schemas/WsUpdatePayload"
|
||||
|
||||
BranchesData:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/CommitInfo"
|
||||
description: "Dict[envId, Dict[branchName, CommitInfo[]]]"
|
||||
|
||||
EnvironmentsData:
|
||||
type: object
|
||||
additionalProperties:
|
||||
$ref: "#/components/schemas/EnvironmentDto"
|
||||
description: "Dict[envId, EnvironmentDto]"
|
||||
|
||||
StateResponse:
|
||||
$ref: "#/components/schemas/EnvironmentsData"
|
||||
|
||||
BranchesResponse:
|
||||
$ref: "#/components/schemas/BranchesData"
|
||||
@@ -11,3 +11,7 @@ types-PyYAML
|
||||
types-requests
|
||||
types-docker
|
||||
testcontainers
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ These tests run in a Docker-like isolated environment to avoid harming the host
|
||||
They simulate remote and local git repositories and test various merge scenarios.
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
import git
|
||||
import pytest
|
||||
from steps.git import CheckoutAndMergeResult, GitUnmergeResult
|
||||
from steps.git import CheckoutAndMergeResult, GitClone, GitUnmergeResult
|
||||
|
||||
from .test_remote_repo import RemoteRepoHelper
|
||||
|
||||
@@ -16,6 +18,187 @@ from .test_remote_repo import RemoteRepoHelper
|
||||
class TestGitIntegration:
|
||||
"""Integration tests for git operations"""
|
||||
|
||||
def test_git_clone_multiple_urls(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
repo_helper.create_commit(repo_helper.repo, "master", "master", "README.md", "content", "Initial commit")
|
||||
second_url = f"file://{repo_helper.remote_dir}"
|
||||
clone = GitClone(url=[repo_helper.remote_dir, second_url])
|
||||
clone.env = repo_helper.env
|
||||
|
||||
repo_path = clone.progress()
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
assert {remote.name for remote in repo.remotes} == {"origin", "repo1"}
|
||||
assert repo.remotes.origin.url == repo_helper.remote_dir
|
||||
assert repo.remotes.repo1.url == second_url
|
||||
assert set(clone.get_branches()) == {"master", "repo1/master"}
|
||||
|
||||
def test_git_clone_named_remotes(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Two real local repos served as separate "remotes". The user controls
|
||||
# their names explicitly via the `remotes=[{name,url}]` API.
|
||||
def fresh_repo(prefix: str, message: str) -> str:
|
||||
path = tempfile.mkdtemp(prefix=prefix)
|
||||
repo = git.Repo.init(path, bare=False)
|
||||
with repo.config_writer() as cw:
|
||||
cw.set_value("user", "email", "test@example.com")
|
||||
cw.set_value("user", "name", "Test User")
|
||||
repo_helper.create_commit(repo, "master", "master", "README.md", "content", message)
|
||||
return path
|
||||
|
||||
github_dir = fresh_repo("test_named_github_", "github initial")
|
||||
gitea_dir = fresh_repo("test_named_gitea_", "gitea initial")
|
||||
try:
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": github_dir},
|
||||
{"name": "gitea_admin", "url": gitea_dir},
|
||||
])
|
||||
clone.env = repo_helper.env
|
||||
|
||||
repo_path = clone.progress()
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
# User-chosen names appear exactly; the auto-generated "origin" /
|
||||
# "repo1" names from the positional-list form do NOT leak in.
|
||||
assert {r.name for r in repo.remotes} == {"github", "gitea_admin"}
|
||||
assert "origin" not in repo.remotes
|
||||
assert "repo1" not in repo.remotes
|
||||
assert repo.remotes.github.url == github_dir
|
||||
assert repo.remotes.gitea_admin.url == gitea_dir
|
||||
|
||||
# `get_branches` keeps the <remote>/<branch> shape for non-origin
|
||||
# names; the user gets the full provenance in the key.
|
||||
assert set(clone.get_branches()) == {"github/master", "gitea_admin/master"}
|
||||
finally:
|
||||
shutil.rmtree(github_dir, ignore_errors=True)
|
||||
shutil.rmtree(gitea_dir, ignore_errors=True)
|
||||
|
||||
def test_git_clone_remotes_validation(self) -> None:
|
||||
# Empty list: rejected (would produce an unusable clone).
|
||||
with pytest.raises(ValueError, match="At least one repository URL is required"):
|
||||
GitClone(remotes=[])
|
||||
|
||||
# Missing / empty / non-string name or url: rejected.
|
||||
with pytest.raises(ValueError, match="non-empty 'name'"):
|
||||
GitClone(remotes=[{"url": "git@example.com:foo/bar.git"}])
|
||||
with pytest.raises(ValueError, match="non-empty 'name'"):
|
||||
GitClone(remotes=[{"name": "", "url": "git@example.com:foo/bar.git"}])
|
||||
with pytest.raises(ValueError, match="non-empty 'url'"):
|
||||
GitClone(remotes=[{"name": "github"}])
|
||||
with pytest.raises(ValueError, match="non-empty 'url'"):
|
||||
GitClone(remotes=[{"name": "github", "url": ""}])
|
||||
|
||||
# Duplicate name: rejected.
|
||||
with pytest.raises(ValueError, match="Duplicate remote name"):
|
||||
GitClone(remotes=[
|
||||
{"name": "github", "url": "git@example.com:a/b.git"},
|
||||
{"name": "github", "url": "git@example.com:c/d.git"},
|
||||
])
|
||||
|
||||
# Passing both `url` and `remotes`: rejected.
|
||||
with pytest.raises(ValueError, match="not both"):
|
||||
GitClone(url="git@example.com:a/b.git", remotes=[{"name": "github", "url": "git@example.com:a/b.git"}])
|
||||
|
||||
# Passing neither: rejected.
|
||||
with pytest.raises(ValueError, match="requires either 'url' or 'remotes'"):
|
||||
GitClone()
|
||||
|
||||
def test_git_clone_remotes_per_remote_prefix(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Per-remote `prefix` filters fetch refspec so only matching branches are visible.
|
||||
# Two repos, two branches each (one matching the prefix, one not). Only the
|
||||
# matching branch should appear in get_branches() for each remote.
|
||||
def fresh_repo(prefix: str) -> str:
|
||||
path = tempfile.mkdtemp(prefix=prefix)
|
||||
repo = git.Repo.init(path, bare=False)
|
||||
with repo.config_writer() as cw:
|
||||
cw.set_value("user", "email", "test@example.com")
|
||||
cw.set_value("user", "name", "Test User")
|
||||
# Branches literally named `ansible/...` to match the prefix filter.
|
||||
# create_commit creates an initial master, then branches off it.
|
||||
repo_helper.create_commit(repo, "master", "master", "README.md", "content", f"{prefix} init")
|
||||
repo_helper.create_commit(repo, "master", "ansible/master", "AN.md", "ansible", f"{prefix} ansible")
|
||||
repo_helper.create_commit(repo, "master", "ansible/fix", "FIX.md", "fix", f"{prefix} fix")
|
||||
return path
|
||||
|
||||
github_dir = fresh_repo("test_prefix_github_")
|
||||
gitea_dir = fresh_repo("test_prefix_gitea_")
|
||||
try:
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": github_dir, "prefix": "ansible"},
|
||||
{"name": "gitea_admin", "url": gitea_dir, "prefix": "ansible"},
|
||||
])
|
||||
clone.env = repo_helper.env
|
||||
|
||||
repo_path = clone.progress()
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
branches = set(clone.get_branches())
|
||||
# Per-remote refspec was applied: only ansible/* branches fetched.
|
||||
assert "github/ansible/master" in branches, f"missing: {branches}"
|
||||
assert "github/ansible/fix" in branches, f"missing: {branches}"
|
||||
assert "gitea_admin/ansible/master" in branches, f"missing: {branches}"
|
||||
assert "gitea_admin/ansible/fix" in branches, f"missing: {branches}"
|
||||
|
||||
# `master` exists upstream but is NOT visible (filtered by prefix).
|
||||
assert "github/master" not in branches, f"unwanted: {branches}"
|
||||
assert "gitea_admin/master" not in branches, f"unwanted: {branches}"
|
||||
finally:
|
||||
shutil.rmtree(github_dir, ignore_errors=True)
|
||||
shutil.rmtree(gitea_dir, ignore_errors=True)
|
||||
|
||||
def test_git_clone_remotes_mixed_prefix(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Some remotes with prefix, others without. Each behaves independently:
|
||||
# prefixed remotes only see matching branches, unprefixed see all.
|
||||
def fresh_repo(prefix: str) -> str:
|
||||
path = tempfile.mkdtemp(prefix=prefix)
|
||||
repo = git.Repo.init(path, bare=False)
|
||||
with repo.config_writer() as cw:
|
||||
cw.set_value("user", "email", "test@example.com")
|
||||
cw.set_value("user", "name", "Test User")
|
||||
repo_helper.create_commit(repo, "master", "master", "README.md", "content", f"{prefix} init")
|
||||
repo_helper.create_commit(repo, "master", "ansible/master", "AN.md", "ansible", f"{prefix} ansible")
|
||||
repo_helper.create_commit(repo, "master", "feature_y", "OTHER.md", "other", f"{prefix} feature")
|
||||
return path
|
||||
|
||||
github_dir = fresh_repo("test_mixed_github_")
|
||||
gitea_dir = fresh_repo("test_mixed_gitea_")
|
||||
try:
|
||||
clone = GitClone(remotes=[
|
||||
{"name": "github", "url": github_dir, "prefix": "ansible"},
|
||||
{"name": "gitea_admin", "url": gitea_dir},
|
||||
])
|
||||
clone.env = repo_helper.env
|
||||
|
||||
repo_path = clone.progress()
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
branches = set(clone.get_branches())
|
||||
# Prefixed: only ansible/master.
|
||||
assert "github/ansible/master" in branches, f"missing: {branches}"
|
||||
assert "github/feature_y" not in branches, f"unwanted: {branches}"
|
||||
assert "github/master" not in branches, f"unwanted: {branches}"
|
||||
# Unprefixed: all branches visible.
|
||||
assert "gitea_admin/ansible/master" in branches, f"missing: {branches}"
|
||||
assert "gitea_admin/feature_y" in branches, f"missing: {branches}"
|
||||
assert "gitea_admin/master" in branches, f"missing: {branches}"
|
||||
finally:
|
||||
shutil.rmtree(github_dir, ignore_errors=True)
|
||||
shutil.rmtree(gitea_dir, ignore_errors=True)
|
||||
|
||||
def test_git_clone_remotes_prefix_validation(self) -> None:
|
||||
# `prefix` must be a string when set; non-string raises ValueError.
|
||||
with pytest.raises(ValueError, match="'prefix' must be a string"):
|
||||
GitClone(remotes=[
|
||||
{"name": "github", "url": "git@example.com:a/b.git", "prefix": 123}, # type: ignore[dict-item]
|
||||
])
|
||||
with pytest.raises(ValueError, match="'prefix' must be a string"):
|
||||
GitClone(remotes=[
|
||||
{"name": "github", "url": "git@example.com:a/b.git", "prefix": ["ansible"]}, # type: ignore[dict-item]
|
||||
])
|
||||
# Empty string / None: accepted, treated as no prefix.
|
||||
c1 = GitClone(remotes=[{"name": "github", "url": "git@example.com:a/b.git", "prefix": ""}])
|
||||
assert c1.remotes[0]["prefix"] == ""
|
||||
c2 = GitClone(remotes=[{"name": "github", "url": "git@example.com:a/b.git"}])
|
||||
assert c2.remotes[0]["prefix"] == ""
|
||||
|
||||
def test_checkout_merged_one_branch(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Create main branch with initial commit
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1",
|
||||
|
||||
Reference in New Issue
Block a user