Atomic issues and PRs
Contract
| Field | Bound contract | |---|---| | Trigger | User says "atomic PRs" or requests one issue/PR per logical change. | | Authority | Human-only. Requires explicit human invocation. Preview the target repo and consequence before creating branches, commits, issues, and pull requests on a remote repository. | | Side effect | Creates branches, commits, issues, and pull requests on a remote GitHub repository. Never force-pushes or pushes to protected branches without explicit user authorization. | | Done | One issue or PR per logical change on the correct base/head, links appended, URLs reported. |
Inputs
Required: a working tree containing the change-set to publish and a gh-authenticated GitHub account.
Optional: per-unit routing overrides (issue+PR vs PR-only) stated by the user; dependency order among units.
Procedure
- Run
gh auth status. If unauthenticated, stop and ask the user to rungh auth login. - Resolve the canonical (upstream) slug before any permission check. Read remotes with
git remote -v; pick the contribution target asupstreamif present, elseorigin. Detect a fork relationship withgh repo view <slug> --json nameWithOwner,parent,defaultBranchRef; a non-nullparentmeans the canonical slug isparent.nameWithOwner. If there is no clear single upstream (noupstreamremote and ≥2 plausible non-origin candidates, orgh's detectedparentdisagrees with theupstreamremote) ororiginhas genuinely diverged from upstream (no common merge-base, ororiginwas re-created/renamed/renewed), prompt the user for the target repo. Plain fork-behind, whereoriginis merely behindupstreamwith a shared merge-base, is not ambiguity and must never prompt. - Query permission on the canonical slug:
gh repo view <canonical-slug> --json viewerPermission.viewerPermission∈ {ADMIN, MAINTAIN, WRITE} ⇒ direct mode; otherwise ⇒ fork mode. Record the canonical default base branch fromdefaultBranchRef. - Group working-tree changes into atomic units by mechanism/file boundary: one concern per unit. Never bundle unrelated changes. Commit the whole set into N atomic commits first, running the repo-native type-checker and linter before each commit. Each unit becomes one self-contained commit, which allows per-unit branches to be created by cherry-picking. Never re-stage a dirty tree, which would let later units swallow earlier diffs. Present the unit→commit list to the user, then proceed.
- Route each unit by whether it changes observable behavior: behavior-affecting → Issue + linked PR, because a tracking issue gives the change a changelog/discussion anchor; mechanical → PR-only, because a self-explanatory change needs no separate tracking. Honor an explicit per-unit override if the user states one; otherwise route silently.
- Resolve the push URL once, by URL not remote name, to avoid undefined targets and remote-name collisions. Direct mode: push URL =
https://github.com/<canonical-slug>. Fork mode: fork owner =gh api user --jq .login; fork slug =<login>/<repo>. Iforiginalready points to a prior personal fork whose owner ≠ canonical owner and ≠ the authenticated login, print a non-blocking warning noting the account mismatch; do not stop the run. If the fork does not exist, create it withgh repo fork <canonical-slug> --clone=falseand note the fork slug in the output. Push URL =https://github.com/<fork-slug>. - For each unit, in dependency order:
- Create the branch and cherry-pick the unit commit. Independent unit:
git branch <branch> <canonical-default-base>thengit cherry-pick <unit-commit>; PR bases on<default>. Dependent unit in direct mode: parent-ref = the prerequisite unit's already-pushed branch; PR bases on that branch (a stacked PR); cherry-pick only this unit's commit so no prerequisite is lost. Dependent unit in fork mode: cross-fork stacking cannot be expressed (a fork PR's base must be a branch in the canonical repo), so flatten onto<canonical-default-base>and prefix the PR body with a warning line naming the prerequisite unit and noting the dependency was flattened; the compromise is never silent. git push <push-url> <branch>:refs/heads/<branch>(same form both modes; only the URL differs).gh pr create --repo <canonical-slug> --body-file <tmp>. Direct mode:--base <parent-ref> --head <branch>; fork mode:--base <default> --head <fork-owner>:<branch>; capture PR#M.- If the unit routed to Issue + linked PR:
gh issue create --repo <canonical-slug> --title "<summary>" --body-file <tmp>with a body that references PR#M; capture#N. - Amend the PR body by appending
Closes #Nto the PR's existing body. Reuse the body-file from step 3 (append the line and rewrite the file) or fetch the current body first withgh pr view <M> --json body; then write it withgh pr edit <M> --repo <canonical-slug> --body-file <amended-tmp>.gh pr edit --body-filereplaces the whole body, so never write a bareCloses #Nas the entirety of it. - Emit the issue/PR URLs; move to the next unit.
- Create the branch and cherry-pick the unit commit. Independent unit:
- For PR-only routed units (no issue filed in step 4), skip the issue-create and body-amend steps entirely; no dangling
Closes. Done when: every logical unit has the correct issue/PR routing, base and head, links, and reported URLs.
Failure and recovery
- Unauthenticated: stop before any mutation; ask the user to run
gh auth login. No partial result. - Ambiguous upstream: stop and ask the user for the target repo; do not guess. No mutation performed.
- Insufficient permission with no fork path: stop; report the canonical slug and the missing permission. No push or object created.
- Push rejected: report the rejection and the branch/URL; do not force-push. Leave prior successfully published units in place.
- Partial publish failure mid-loop: already-published units remain; report each unit's status (published URL or failed) so the user can retry the failed units. Never swallow the error or pretend the done predicate holds.
- Non-converged: if any unit could not be published, the terminal result names the failed units and the reason; the run is not done.
Output
A run report listing, per unit: the routing (Issue+PR or PR-only), the branch, the push URL, the PR number and URL, and (when filed) the issue number and URL. Classify the run as done only when one issue or PR per logical change exists on the correct base/head, links are appended, and URLs are reported.