feat: implement Codeberg → Gitea repo migrator

Replay git (mirror + LFS), labels, milestones, issues & PRs (with
comments), and releases (with assets) entity-by-entity over the shared
Forgejo/Gitea REST API. Two-pass issue/PR creation rewrites #NNN and
URL cross-references to the target number space; deleted-branch PR
heads are recreated as refs/heads/_pr/<n>.

CLI: codeberg-to-gitea migrate owner/name  (CODEBERG_TOKEN/GITEA_TOKEN)
Verified end-to-end on AlexKucera/helm: 283 issues, 1 PR, 19 labels,
219 comments, git history, with cross-refs correctly rewritten.
This commit is contained in:
AlexKucera committed 2026-08-11 11:12:06 +02:00
1 parent 601cf4c41e
commit ab43b0311e
14 files changed
+1240 -23

No files matched your search

+5 -4
View File
@@ -2,8 +2,7 @@
## Purpose
Pytest suite for `codeberg_to_gitea_migrator`. Currently a smoke test asserting
the package imports and reports its version.
Pytest suite for `codeberg_to_gitea_migrator`.
## Ownership
@@ -16,8 +15,10 @@ Project maintainer (Alexander Kucera).
## Work Guidance
- Add focused tests alongside each migration module.
- Keep network calls behind a fake/recorded client — do not hit live Codeberg/Gitea from tests.
- `test_smoke.py` — package imports + version.
- `test_refs.py` — `#NNN`/URL cross-reference rewrite + `NumberMap`.
- `test_clients.py` — `ApiClient` pagination, param cleaning, 4xx handling, JSON POST, all via `httpx.MockTransport` (no network).
- Keep tests network-free: never hit live Codeberg/Gitea from the suite. The end-to-end run is a manual CLI invocation, not a test.
## Verification
+73
View File
@@ -0,0 +1,73 @@
"""Tests for the API client (pagination + helpers) using a mock transport.
No network: httpx.MockTransport answers all requests deterministically.
"""
import httpx
import pytest
from codeberg_to_gitea_migrator.clients import ApiClient, ApiError
def _client(handler) -> ApiClient:
transport = httpx.MockTransport(handler)
real = ApiClient("https://example.com", "tok")
# Swap in the mock transport but keep the auth header.
real._client = httpx.Client(
base_url=real.api_root,
headers={"Authorization": "token tok"},
transport=transport,
)
return real
def test_paged_follows_pages_until_short_batch() -> None:
pages = {
1: [{"id": i} for i in range(50)],
2: [{"id": i} for i in range(50, 70)], # < limit → stop
}
def handler(request: httpx.Request) -> httpx.Response:
page = int(request.url.params["page"])
return httpx.Response(200, json=pages.get(page, []))
with _client(handler) as c:
items = c.get_all("/repos/o/r/labels")
assert len(items) == 70
assert items[0]["id"] == 0
assert items[-1]["id"] == 69
def test_get_drops_none_params_and_stringifies_bools() -> None:
seen = {}
def handler(request: httpx.Request) -> httpx.Response:
seen["params"] = dict(request.url.params)
return httpx.Response(200, json={"ok": True})
with _client(handler) as c:
c.get("/repos/o/r", state=None, private=True, draft=False, name="x")
assert seen["params"] == {"private": "true", "draft": "false", "name": "x"}
def test_raises_on_4xx() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(404, text="not found")
with _client(handler) as c:
with pytest.raises(ApiError) as exc_info:
c.get("/repos/o/r/missing")
assert exc_info.value.status == 404
def test_post_sends_json_body() -> None:
seen = {}
def handler(request: httpx.Request) -> httpx.Response:
seen["body"] = request.read().decode()
return httpx.Response(201, json={"id": 7})
with _client(handler) as c:
result = c.post("/repos/o/r/issues", body={"title": "x"})
assert result == {"id": 7}
assert '"title":"x"' in seen["body"]
+59
View File
@@ -0,0 +1,59 @@
"""Tests for cross-reference rewriting (no network)."""
from codeberg_to_gitea_migrator.refs import NumberMap, rewrite
def _ref(numbers: dict[int, int], text: str) -> str:
nm = NumberMap()
for src, tgt in numbers.items():
nm.add(src, tgt)
return rewrite(
text,
nm,
source_web_root="https://codeberg.org",
target_web_root="https://git.babylondreams.de",
)
def test_hash_ref_rewritten_when_mapped() -> None:
assert _ref({289: 2}, "Fixes #289 and more") == "Fixes #2 and more"
def test_unknown_hash_ref_left_alone() -> None:
assert _ref({289: 2}, "see #999 here") == "see #999 here"
def test_self_mapping_left_unchanged() -> None:
# Numbers that map to themselves aren't rewritten (keeps diffs clean).
assert _ref({5: 5}, "ref #5") == "ref #5"
def test_markdown_heading_not_matched() -> None:
assert _ref({}, "## 2 Section") == "## 2 Section"
def test_anchor_not_matched() -> None:
# `C#123` — the '#' is glued to a word char, so not a ref.
assert _ref({123: 1}, "use C#123 framework") == "use C#123 framework"
def test_url_repointed_and_renumbered() -> None:
out = _ref({290: 3}, "see https://codeberg.org/AlexKucera/helm/issues/290")
assert out == "see https://git.babylondreams.de/AlexKucera/helm/issues/3"
def test_url_host_only_when_number_unknown() -> None:
out = _ref({}, "see https://codeberg.org/AlexKucera/helm/issues/42")
assert out == "see https://git.babylondreams.de/AlexKucera/helm/issues/42"
def test_number_map_any_mismatch() -> None:
nm = NumberMap()
assert nm.any_mismatch is False
nm.add(1, 1)
assert nm.any_mismatch is False
nm.add(2, 5)
assert nm.any_mismatch is True
assert len(nm) == 2
assert nm.get(2) == 5
assert nm.get(99) is None