bridge_service.py
python
sha256:fc04e4cae9e1774d6a21b65c45daeed0e6787eb581d13aa1b03bfe9384a34226
Merge branch 'fix/two-column-scroll-layout' into dev
Human
9 days ago
| 1 | """Bridge mirror service — persistence adapter for musehub_bridge_mirrors. |
| 2 | |
| 3 | This module is the sole point of DB access for the bridge mirror registry. |
| 4 | Route handlers delegate here; no business logic lives in routes. |
| 5 | |
| 6 | Boundary rules: |
| 7 | - Must NOT import state stores, SSE queues, or LLM clients. |
| 8 | - May import ORM models from musehub.db domain-specific modules. |
| 9 | - May import Pydantic models from musehub.models.bridge. |
| 10 | """ |
| 11 | |
| 12 | import logging |
| 13 | from datetime import datetime, timezone |
| 14 | |
| 15 | from sqlalchemy import select |
| 16 | from sqlalchemy.exc import IntegrityError |
| 17 | from sqlalchemy.ext.asyncio import AsyncSession |
| 18 | |
| 19 | from musehub.core.genesis import compute_bridge_mirror_id |
| 20 | from musehub.db.musehub_repo_models import MusehubBridgeMirror |
| 21 | from musehub.models.bridge import CreateMirrorRequest |
| 22 | |
| 23 | logger = logging.getLogger(__name__) |
| 24 | |
| 25 | def _utc_now() -> datetime: |
| 26 | return datetime.now(tz=timezone.utc) |
| 27 | |
| 28 | async def create_mirror( |
| 29 | session: AsyncSession, |
| 30 | repo_id: str, |
| 31 | request: CreateMirrorRequest, |
| 32 | created_by: str, |
| 33 | ) -> MusehubBridgeMirror: |
| 34 | """Create a new bridge mirror registration for a repo. |
| 35 | |
| 36 | Raises ``IntegrityError`` (409 at the route layer) when the same |
| 37 | ``git_remote_url`` is already registered for this ``repo_id``. |
| 38 | """ |
| 39 | mirror = MusehubBridgeMirror( |
| 40 | id=compute_bridge_mirror_id(repo_id, request.git_remote_url), |
| 41 | repo_id=repo_id, |
| 42 | git_remote_url=request.git_remote_url, |
| 43 | git_branch=request.git_branch, |
| 44 | direction=request.direction, |
| 45 | auto_export=request.auto_export, |
| 46 | created_by=created_by, |
| 47 | ) |
| 48 | session.add(mirror) |
| 49 | # Flush here so we propagate IntegrityError before the caller commits. |
| 50 | await session.flush() |
| 51 | return mirror |
| 52 | |
| 53 | async def list_mirrors( |
| 54 | session: AsyncSession, |
| 55 | repo_id: str, |
| 56 | ) -> list[MusehubBridgeMirror]: |
| 57 | """Return all bridge mirrors registered for *repo_id*, ordered by creation time.""" |
| 58 | result = await session.execute( |
| 59 | select(MusehubBridgeMirror) |
| 60 | .where(MusehubBridgeMirror.repo_id == repo_id) |
| 61 | .order_by(MusehubBridgeMirror.created_at) |
| 62 | ) |
| 63 | return list(result.scalars().all()) |
| 64 | |
| 65 | async def delete_mirror( |
| 66 | session: AsyncSession, |
| 67 | repo_id: str, |
| 68 | mirror_id: str, |
| 69 | ) -> bool: |
| 70 | """Delete the mirror with *mirror_id* that belongs to *repo_id*. |
| 71 | |
| 72 | Returns ``True`` when the row was deleted; ``False`` when it was not found. |
| 73 | """ |
| 74 | result = await session.execute( |
| 75 | select(MusehubBridgeMirror).where( |
| 76 | MusehubBridgeMirror.id == mirror_id, |
| 77 | MusehubBridgeMirror.repo_id == repo_id, |
| 78 | ) |
| 79 | ) |
| 80 | mirror = result.scalar_one_or_none() |
| 81 | if mirror is None: |
| 82 | return False |
| 83 | await session.delete(mirror) |
| 84 | await session.flush() |
| 85 | return True |
| 86 | |
| 87 | async def update_mirror_export_state( |
| 88 | session: AsyncSession, |
| 89 | mirror_id: str, |
| 90 | muse_commit_id: str, |
| 91 | git_sha: str, |
| 92 | ) -> None: |
| 93 | """Record a successful export operation on the given mirror.""" |
| 94 | result = await session.execute( |
| 95 | select(MusehubBridgeMirror).where(MusehubBridgeMirror.id == mirror_id) |
| 96 | ) |
| 97 | mirror = result.scalar_one_or_none() |
| 98 | if mirror is None: |
| 99 | logger.warning("update_mirror_export_state: mirror %s not found", mirror_id) |
| 100 | return |
| 101 | mirror.last_export_muse_commit_id = muse_commit_id |
| 102 | mirror.last_export_git_sha = git_sha |
| 103 | mirror.last_export_at = _utc_now() |
| 104 | await session.flush() |
File History
13 commits
sha256:fc04e4cae9e1774d6a21b65c45daeed0e6787eb581d13aa1b03bfe9384a34226
Merge branch 'fix/two-column-scroll-layout' into dev
Human
9 days ago
sha256:408916fc5973ba59c6e4eebaa80ebdcc801c0a63205651e25009d11548f79454
chore: bump version to 0.2.0.dev2 — nightly.2, matching muse
Sonnet 4.6
patch
12 days ago
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2
chore: bump version to 0.2.0rc15 for musehub#113 fix release
Sonnet 4.6
patch
15 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352
Merge branch 'task/version-tags-phase3-server' into dev
Human
17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53
merge: rescue snapshot-recovery hardening (c00aa21d) into d…
Opus 4.8
minor
⚠
30 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a
fix: remove false-positive proposal_comments index drop fro…
Sonnet 4.6
patch
34 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3
feat: render markdown mists as HTML with heading anchor links
Sonnet 4.6
patch
35 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
36 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
36 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c
Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo…
Human
36 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd
Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop…
Human
36 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f
fix: use wire_bytes not mpack_bytes_raw in compute_object_b…
Sonnet 4.6
patch
48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583
rename: delta_add → delta_upsert across wire format, models…
Sonnet 4.6
patch
50 days ago