refactor(migrator): own _pr/N PR-head contract
CI / test (push) Canceled after 0s

Extract the duplicated `_pr/<n>` deleted-branch PR-head branch name
into a single pure helper `refs.pr_head_branch(n)`, now the only
definition. The git-mirror layer (mirror.py) and the REST/entity
layer (issues.py) both call it instead of spelling the pattern, so
the two can no longer diverge — editing one side previously broke
the other silently and late (404/500 on PR create for deleted-branch
PRs only).

Behavior-preserving string round-trip. refs.py widens from
"cross-reference rewriting" to "reference-namespace knowledge"
(branch-name patterns + cross-ref rewriting), still pure stdlib. No
new test — a shared symbol prevents divergence by construction
(issue #3 testing decision); the deleted-branch round-trip stays
covered by the existing phase test seam.

Closes #3
This commit is contained in:
AlexKucera committed 2026-08-11 17:26:46 +02:00
1 parent 9d585f601a
commit 8ec9ce49db
8 files changed
+107 -13

No files matched your search

@@ -0,0 +1,70 @@
# Own the _pr/<n> deleted-branch PR-head contract (shared pr_head_branch helper in refs.py)
> **Date:** 2026-08-11
> **Type:** issue
> **Reference:** [#3](https://git.babylondreams.de/AlexKucera/codeberg-to-gitea-migrator/issues/3)
## Goal
Eliminate the duplicated `_pr/<n>` branch-name literal that the deleted-branch
PR survival feature rested on. The git-transport layer (`mirror.py`) *created*
`refs/heads/_pr/<n>`; the REST/entity layer (`issues.py`) *referenced* the same
branch as a PR's head when the source branch was gone. Both spelled
`f"_pr/{n}"` with no shared symbol binding them — editing one side silently
broke the other, and the failure was narrow and late (404/500 on PR create for
deleted-branch PRs only). Own the contract as one shared, importable helper so
the two cannot diverge.
## What Was Done
- Added `pr_head_branch(n: int) -> str` to `refs.py` (returns `f"_pr/{n}"`);
pure, no I/O. The single source of truth for the `_pr/<n>` namespace.
- `mirror.py` (producer): added `from .refs import pr_head_branch`; refspec now
`f"{sha}:refs/heads/{pr_head_branch(n)}"` (refspec syntax stays local).
- `issues.py` (consumer): added `from .refs import pr_head_branch`; PR head now
`pr_head_branch(item["number"])` instead of the inline literal.
- Widened `refs.py`'s module docstring from "cross-reference rewriting" to
"reference-namespace knowledge" (branch-name patterns + cross-ref rewriting);
still pure stdlib (`re` only).
- Docs closeout: `src/.../AGENTS.md` module-map (`refs.py` + `issues.py`
entries) and `README.md` "Pull-request heads" bullet now name
`refs.pr_head_branch` as the single source of truth.
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| Free function, not a `Migration` method | The producer (`mirror.py`, git layer) holds no `Migration` handle, so both layers reach a neutral pure module instead of one depending on the other's internals. |
| House it in `refs.py` | A branch-name pattern *is* reference-namespace knowledge; broadens `refs.py` coherently. Pure stdlib, so the git layer importing it drags in no REST coupling. |
| Shared kernel = bare branch name only | The producer prefixes refspec syntax (`refs/heads/`, the `sha:` source) locally — those are refspec concerns, not part of the shared contract. |
| `n` stays `int` on both sides | The mirror parses `int(ref.split("/")[2])`; the consumer reads the source PR number — no int/str bridge needed. |
| No change-specific test | A shared symbol prevents divergence by construction; a test asserting "the two literals agree" tests the absence of duplication (issue #3 testing decision). The deleted-branch round-trip stays covered by the existing phase test seam. |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| PRD claimed `refs.py` was "already imported" in `issues.py` | It wasn't — `issues.py` imported `.clients` + `.migration` only (`m.rewrite` reaches refs via the `Migration` handle) | Added `from .refs import pr_head_branch` to `issues.py`. Minimal deviation from the PRD; intent unchanged. |
| First cut added a `__main__` self-check `assert pr_head_branch(42) == "_pr/42"` | That asserts "the literal agrees" — exactly the redundant test the spec's testing decision warns against, and a trivial one-liner under ponytail | Removed it after the fresh-context review fanout confirmed it was redundant. |
## Files Changed
| File | Change Summary |
|------|----------------|
| `src/codeberg_to_gitea_migrator/refs.py` | Add `pr_head_branch(n)`; widen module docstring to reference-namespace knowledge. |
| `src/codeberg_to_gitea_migrator/mirror.py` | Import + route the PR-head refspec through `pr_head_branch`. |
| `src/codeberg_to_gitea_migrator/issues.py` | Import + route the deleted-branch PR head through `pr_head_branch`. |
| `src/codeberg_to_gitea_migrator/AGENTS.md` | Module map: `refs.py` and `issues.py` entries note the shared symbol. |
| `README.md` | "Pull-request heads" bullet notes `refs.pr_head_branch` as the single source of truth. |
| `AGENTS.md` (root) | GitNexus symbol count auto-bump (434→444) from re-index. |
## Open Items & Next Steps
- [ ] **Untracked `CONTEXT.md`** — predates this session, not gitignored, but
referenced by `AGENTS.md` as the DOX "single-context" core doc. Track-vs-ignore
intent undecided; left out of this commit intentionally. Not in #3's scope.
- [ ] None for #3 itself — all 13 user stories met; behaviour unchanged.
---
*Log written by write-log skill*
+1
View File
@@ -5,5 +5,6 @@ Complete history of session logs in this directory, newest first. One-line summa
<!-- write-log: full-index -->
| Date | Type | File | Summary |
|------|------|------|---------|
| 2026-08-11 | issue | [20260811-issue3-pr-head-branch-helper-in-refs_log.md](20260811-issue3-pr-head-branch-helper-in-refs_log.md) | Extract the duplicated `_pr/<n>` deleted-branch PR-head name into one shared pure helper `refs.pr_head_branch`; `mirror.py` + `issues.py` route through it instead of spelling the literal. Closes #3. |
| 2026-08-11 | issue | [20260811-issue2-bind-migration-context-retire-refctx-path_log.md](20260811-issue2-bind-migration-context-retire-refctx-path_log.md) | Introduce frozen `Migration` bound context; rewrite the four entity phases to take the handle; retire `RefCtx`/`_path`; add the phase test seam (`test_phases.py`). Closes #2. |
| 2026-08-11 | issue | [20260811-issue1-gitrunner-sole-git-op-seam_log.md](20260811-issue1-gitrunner-sole-git-op-seam_log.md) | Introduce `git.GitRunner` as the sole git-op seam; rewrite mirror to drive it; retire URL-token helpers; closes audit HIGH A09 + MEDIUM A02 at the root. |