The migrator was fully synchronous — every HTTP round-trip and git op serialized over one blocking client, and a stalled git push could hang it forever. Overlap the independent phases and bound the slow / memory-heavy paths. - clients: ApiClient is now thread-safe (per-thread httpx.Client) so labels, milestones, Pass-B comments, and release assets overlap via parallel(); --concurrency sizes it (default 8). Pass A issue/PR creation stays serial — entities are created in source-number order so the number map resolves forward cross-references. - releases: stream assets through a spooled temp file instead of buffering each whole into memory; upload them concurrently. - reliability: per-op --git-timeout (was unbounded hangs), a circuit breaker that aborts on a degraded target, jittered retry backoff, and a bulk timeout for large asset transfers. - round-trips: set an issue's milestone in the create body; batch the deleted-branch PR-head git push and refs/pull/* pruning. - fix: raw_get_stream 4xx/5xx raised AttributeError (resp.read().text on bytes) instead of ApiError — now decodes. Wrap mirror git ops in try/finally so tokenized remote URLs are scrubbed even on timeout. - refactor: drop the typer dependency — the CLI is now stdlib argparse (same subcommands, flags, and env vars); remove dead code surfaced by a whole-repo audit (has_lfs, name_map, unused owner_repo param, stored-but-unread _base_url). - ci: enable the uv download cache. Tests cover parallel ordering, the circuit breaker, and the streamed 4xx path. CHANGELOG updated; the runtime dependency is now just httpx.
This commit is contained in:
1 parent
867d982786
commit
137734673d
14 files changed
+1372
-299
No files matched your search
@@ -14,6 +14,11 @@ Issue/PR numbers share one sequence on both forges, and source numbers are
|
||||
sparse (deleted issues leave gaps), so the rewrite pass is what keeps migrated
|
||||
cross-references honest.
|
||||
|
||||
Pass A is inherently serial (entities must be created in source-number order so
|
||||
the number map resolves forward references). Pass B, plus label/milestone
|
||||
creation, are independent across items and overlap through
|
||||
:func:`codeberg_to_gitea_migrator.clients.parallel`.
|
||||
|
||||
Known fidelity limits (the public Gitea API stamps these server-side): original
|
||||
author (attributed to the migration token user), original timestamps, and
|
||||
assignees/reactions.
|
||||
@@ -25,7 +30,7 @@ import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from .clients import ApiClient, ApiError
|
||||
from .clients import ApiClient, ApiError, _path, parallel
|
||||
from .refs import NumberMap, rewrite
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -46,18 +51,19 @@ class RefCtx:
|
||||
)
|
||||
|
||||
|
||||
def _path(owner: str, repo: str, suffix: str = "") -> str:
|
||||
return f"/repos/{owner}/{repo}{suffix}"
|
||||
|
||||
|
||||
# -- labels ---------------------------------------------------------------
|
||||
def migrate_labels(
|
||||
src: ApiClient, tgt: ApiClient, owner: str, repo: str
|
||||
) -> tuple[dict[int, int], dict[str, int]]:
|
||||
"""Recreate labels on target. Returns (old_id→new_id, name→new_id)."""
|
||||
id_map: dict[int, int] = {}
|
||||
name_map: dict[str, int] = {}
|
||||
for label in src.get_all(_path(owner, repo, "/labels")):
|
||||
src: ApiClient,
|
||||
tgt: ApiClient,
|
||||
owner: str,
|
||||
repo: str,
|
||||
*,
|
||||
concurrency: int = 1,
|
||||
) -> dict[int, int]:
|
||||
"""Recreate labels on target. Returns old_id→new_id."""
|
||||
labels = src.get_all(_path(owner, repo, "/labels"))
|
||||
|
||||
def make(label: dict) -> tuple[dict, dict]:
|
||||
body = {
|
||||
"name": label["name"],
|
||||
"color": label.get("color", "ededed"),
|
||||
@@ -66,18 +72,21 @@ def migrate_labels(
|
||||
"is_archived": bool(label.get("is_archived", False)),
|
||||
}
|
||||
created = tgt.post(_path(owner, repo, "/labels"), body=body)
|
||||
new_id = created["id"]
|
||||
id_map[label["id"]] = new_id
|
||||
name_map[label["name"]] = new_id
|
||||
log.info("label %s → #%s", label["name"], new_id)
|
||||
return id_map, name_map
|
||||
log.info("label %s → #%s", label["name"], created["id"])
|
||||
return label, created
|
||||
|
||||
results = parallel(labels, make, workers=concurrency)
|
||||
return {lab["id"]: created["id"] for lab, created in results}
|
||||
|
||||
|
||||
# -- milestones -----------------------------------------------------------
|
||||
def migrate_milestones(src: ApiClient, tgt: ApiClient, owner: str, repo: str) -> dict[int, int]:
|
||||
def migrate_milestones(
|
||||
src: ApiClient, tgt: ApiClient, owner: str, repo: str, *, concurrency: int = 1
|
||||
) -> dict[int, int]:
|
||||
"""Recreate milestones on target. Returns old_id→new_id."""
|
||||
id_map: dict[int, int] = {}
|
||||
for ms in src.get_all(_path(owner, repo, "/milestones"), state="all"):
|
||||
milestones = src.get_all(_path(owner, repo, "/milestones"), state="all")
|
||||
|
||||
def make(ms: dict) -> tuple[dict, dict]:
|
||||
body: dict[str, Any] = {
|
||||
"title": ms["title"],
|
||||
"description": ms.get("description") or "",
|
||||
@@ -86,9 +95,11 @@ def migrate_milestones(src: ApiClient, tgt: ApiClient, owner: str, repo: str) ->
|
||||
if ms.get("due_on"):
|
||||
body["due_on"] = ms["due_on"]
|
||||
created = tgt.post(_path(owner, repo, "/milestones"), body=body)
|
||||
id_map[ms["id"]] = created["id"]
|
||||
log.info("milestone %s → #%s", ms["title"], created["id"])
|
||||
return id_map
|
||||
return ms, created
|
||||
|
||||
results = parallel(milestones, make, workers=concurrency)
|
||||
return {ms["id"]: created["id"] for ms, created in results}
|
||||
|
||||
|
||||
# -- issues + PRs ---------------------------------------------------------
|
||||
@@ -144,13 +155,21 @@ def _create_entity(
|
||||
milestone_map: dict[int, int],
|
||||
) -> int:
|
||||
"""Create an issue or PR; return its new number. Raises on PR-create failure
|
||||
(e.g. head branch gone) so the caller can skip cleanly."""
|
||||
(e.g. head branch gone) so the caller can skip cleanly.
|
||||
|
||||
Issues carry their milestone in the create body (one request); PRs can't
|
||||
(the pull-create endpoint has no milestone field), so they still take a
|
||||
follow-up PATCH.
|
||||
"""
|
||||
kind = item["_kind"]
|
||||
ms = item.get("milestone")
|
||||
ms_id = milestone_map[ms["id"]] if ms and ms.get("id") in milestone_map else None
|
||||
|
||||
if kind == "issue":
|
||||
created = tgt.post(
|
||||
_path(owner, repo, "/issues"),
|
||||
body={"title": item["title"], "body": item.get("body") or ""},
|
||||
)
|
||||
body: dict[str, Any] = {"title": item["title"], "body": item.get("body") or ""}
|
||||
if ms_id is not None:
|
||||
body["milestone"] = ms_id
|
||||
created = tgt.post(_path(owner, repo, "/issues"), body=body)
|
||||
else:
|
||||
head = item.get("head", {}) or {}
|
||||
base = (item.get("base", {}) or {}).get("ref", "main")
|
||||
@@ -168,14 +187,9 @@ def _create_entity(
|
||||
"base": base,
|
||||
},
|
||||
)
|
||||
new_number = created["number"]
|
||||
ms = item.get("milestone")
|
||||
if ms and ms.get("id") in milestone_map:
|
||||
tgt.patch(
|
||||
_path(owner, repo, f"/issues/{new_number}"),
|
||||
body={"milestone": milestone_map[ms["id"]]},
|
||||
)
|
||||
return new_number
|
||||
if ms_id is not None:
|
||||
tgt.patch(_path(owner, repo, f"/issues/{created['number']}"), body={"milestone": ms_id})
|
||||
return created["number"]
|
||||
|
||||
|
||||
def migrate_issues_and_prs(
|
||||
@@ -186,6 +200,8 @@ def migrate_issues_and_prs(
|
||||
label_id_map: dict[int, int],
|
||||
milestone_map: dict[int, int],
|
||||
ref: RefCtx,
|
||||
*,
|
||||
concurrency: int = 1,
|
||||
) -> NumberMap:
|
||||
"""Migrate issues + PRs + comments, returning the source→target number map."""
|
||||
numbers = ref.numbers
|
||||
@@ -220,8 +236,11 @@ def migrate_issues_and_prs(
|
||||
created.append((item, new_number))
|
||||
log.info("%s #%s → #%s (%s)", kind, item["number"], new_number, item["title"])
|
||||
|
||||
# Pass B: rewrite refs in title+body, then stream comments.
|
||||
for item, new_number in created:
|
||||
# Pass B: rewrite refs in title+body, then stream comments. Items are
|
||||
# independent of each other (the number map is already complete), so they
|
||||
# overlap; each item's comments stay sequential to preserve their order.
|
||||
def pass_b(pair: tuple[dict, int]) -> None:
|
||||
item, new_number = pair
|
||||
new_title = ref(item.get("title"))
|
||||
new_body = ref(item.get("body"))
|
||||
tgt.patch(
|
||||
@@ -234,4 +253,5 @@ def migrate_issues_and_prs(
|
||||
body={"body": ref(comment.get("body"))},
|
||||
)
|
||||
|
||||
parallel(created, pass_b, workers=concurrency)
|
||||
return numbers
|
||||
Reference in new issue
Block a user