Skip to content

Pr recovery

pr_recovery

Shared recovery for an already-existing GitHub pull request.

When gh pr create fails because a PR already exists for the branch, both :class:~axm_git.tools.pr.GitPRTool and :class:~axm_git.hooks.create_pr.CreatePRHook recover the existing PR via gh pr view. This module factors that recovery into a single helper that returns a result-agnostic structure; each caller adapts it to its own result type (ToolResult / HookResult).

PRRecovery dataclass

Normalized result of recovering an existing pull request.

On success error is None and url/number are populated. On failure error carries the reason and url/number are empty.

Source code in packages/axm-git/src/axm_git/core/pr_recovery.py
Python
@dataclass(frozen=True)
class PRRecovery:
    """Normalized result of recovering an existing pull request.

    On success ``error`` is ``None`` and ``url``/``number`` are populated.
    On failure ``error`` carries the reason and ``url``/``number`` are empty.
    """

    url: str = ""
    number: str = ""
    already_existed: bool = False
    error: str | None = None

    @property
    def ok(self) -> bool:
        """Whether recovery succeeded."""
        return self.error is None
ok property

Whether recovery succeeded.

is_already_exists(stderr)

Return True when stderr signals an existing PR (case-insensitive).

Source code in packages/axm-git/src/axm_git/core/pr_recovery.py
Python
def is_already_exists(stderr: str) -> bool:
    """Return ``True`` when *stderr* signals an existing PR (case-insensitive)."""
    return "already exists" in stderr.lower()

recover_existing_pr(working_dir)

Resolve the existing PR via gh pr view after an 'already exists' error.

Parameters:

Name Type Description Default
working_dir Path

Repository working directory.

required

Returns:

Name Type Description
A PRRecovery

class:PRRecovery with url/number/already_existed on

PRRecovery

success, or with error set when the PR could not be retrieved.

Source code in packages/axm-git/src/axm_git/core/pr_recovery.py
Python
def recover_existing_pr(working_dir: Path) -> PRRecovery:
    """Resolve the existing PR via ``gh pr view`` after an 'already exists' error.

    Args:
        working_dir: Repository working directory.

    Returns:
        A :class:`PRRecovery` with ``url``/``number``/``already_existed`` on
        success, or with ``error`` set when the PR could not be retrieved.
    """
    view = run_gh(["pr", "view", "--json", "url,number"], working_dir)
    if view.returncode != 0:
        return PRRecovery(
            error=f"PR already exists but could not retrieve it: {view.stderr}"
        )
    data = json.loads(view.stdout)
    return PRRecovery(
        url=data["url"],
        number=str(data["number"]),
        already_existed=True,
    )