|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from ptq.job import get_job |
| 8 | +from ptq.ssh import backend_for_job |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from ptq.ssh import Backend |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class PRResult: |
| 16 | + url: str |
| 17 | + branch: str |
| 18 | + |
| 19 | + |
| 20 | +def _read_file(backend: Backend, path: str) -> str: |
| 21 | + result = backend.run(f"cat {path}", check=False) |
| 22 | + if result.returncode == 0: |
| 23 | + return result.stdout.strip() |
| 24 | + return "" |
| 25 | + |
| 26 | + |
| 27 | +def _build_pr_body(report: str, worklog: str, issue_number: int | None) -> str: |
| 28 | + parts: list[str] = [] |
| 29 | + if report: |
| 30 | + parts.append(report) |
| 31 | + if issue_number is not None: |
| 32 | + parts.append(f"\n\nFixes #{issue_number}") |
| 33 | + if worklog: |
| 34 | + parts.append( |
| 35 | + f"\n\n<details>\n<summary>Worklog</summary>\n\n{worklog}\n\n</details>" |
| 36 | + ) |
| 37 | + return "\n".join(parts) if parts else "Automated fix from ptq." |
| 38 | + |
| 39 | + |
| 40 | +_HTTPS_TO_SSH = { |
| 41 | + "https://github.com/": "git@github.com:", |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def _ensure_ssh_remote( |
| 46 | + backend: Backend, worktree: str, _log: Callable[[str], None] |
| 47 | +) -> None: |
| 48 | + result = backend.run(f"cd {worktree} && git remote get-url origin", check=False) |
| 49 | + url = result.stdout.strip() |
| 50 | + for https_prefix, ssh_prefix in _HTTPS_TO_SSH.items(): |
| 51 | + if url.startswith(https_prefix): |
| 52 | + ssh_url = url.replace(https_prefix, ssh_prefix) |
| 53 | + if not ssh_url.endswith(".git"): |
| 54 | + ssh_url += ".git" |
| 55 | + _log(f"Switching origin to SSH: {ssh_url}") |
| 56 | + backend.run(f"cd {worktree} && git remote set-url origin '{ssh_url}'") |
| 57 | + return |
| 58 | + |
| 59 | + |
| 60 | +def create_pr( |
| 61 | + job_id: str, |
| 62 | + *, |
| 63 | + title: str | None = None, |
| 64 | + draft: bool = False, |
| 65 | + log: Callable[[str], None] | None = None, |
| 66 | +) -> PRResult: |
| 67 | + _log = log or (lambda _: None) |
| 68 | + job = get_job(job_id) |
| 69 | + backend = backend_for_job(job_id) |
| 70 | + ws = backend.workspace |
| 71 | + job_dir = f"{ws}/jobs/{job_id}" |
| 72 | + worktree = f"{job_dir}/pytorch" |
| 73 | + issue_number = job.get("issue") |
| 74 | + |
| 75 | + branch = f"ptq/{issue_number}" if issue_number is not None else f"ptq/{job_id}" |
| 76 | + pr_title = title or ( |
| 77 | + f"Fix #{issue_number}" if issue_number is not None else f"Fix from {job_id}" |
| 78 | + ) |
| 79 | + |
| 80 | + _log(f"Branch: {branch}") |
| 81 | + _log(f"Title: {pr_title}") |
| 82 | + |
| 83 | + report = _read_file(backend, f"{job_dir}/report.md") |
| 84 | + worklog = _read_file(backend, f"{job_dir}/worklog.md") |
| 85 | + body = _build_pr_body(report, worklog, issue_number) |
| 86 | + _log( |
| 87 | + f"PR body: report.md {'found' if report else 'missing'}, worklog.md {'found' if worklog else 'missing'}" |
| 88 | + ) |
| 89 | + |
| 90 | + _SCRUB_PATHS = ".claude/ .cursorrules AGENTS.md" |
| 91 | + |
| 92 | + _log("Staging changes...") |
| 93 | + backend.run(f"cd {worktree} && git add -A") |
| 94 | + |
| 95 | + commit_msg = pr_title.replace("'", "'\\''") |
| 96 | + _log(f"Creating branch {branch} (single commit)...") |
| 97 | + base = backend.run( |
| 98 | + f"cd {worktree} && git merge-base HEAD origin/main", check=False |
| 99 | + ).stdout.strip() |
| 100 | + backend.run(f"cd {worktree} && git checkout -B '{branch}'") |
| 101 | + if base: |
| 102 | + backend.run(f"cd {worktree} && git reset --soft {base}") |
| 103 | + backend.run(f"cd {worktree} && git reset HEAD -- {_SCRUB_PATHS}", check=False) |
| 104 | + backend.run( |
| 105 | + f"cd {worktree} && git commit -m '{commit_msg}' --allow-empty", |
| 106 | + check=False, |
| 107 | + ) |
| 108 | + |
| 109 | + _ensure_ssh_remote(backend, worktree, _log) |
| 110 | + _log("Pushing and creating PR...") |
| 111 | + body_escaped = body.replace("'", "'\\''") |
| 112 | + result = backend.run( |
| 113 | + f"cd {worktree} && git push -u origin '{branch}' --force && " |
| 114 | + f"gh pr create " |
| 115 | + f"--title '{commit_msg}' " |
| 116 | + f"--body '{body_escaped}' " |
| 117 | + f"--head '{branch}'" |
| 118 | + f"{' --draft' if draft else ''}", |
| 119 | + check=False, |
| 120 | + ) |
| 121 | + |
| 122 | + url = "" |
| 123 | + for line in result.stdout.strip().splitlines(): |
| 124 | + if line.startswith("http"): |
| 125 | + url = line.strip() |
| 126 | + break |
| 127 | + |
| 128 | + if not url and result.returncode != 0: |
| 129 | + stderr = result.stderr.strip() if result.stderr else "" |
| 130 | + if "already exists" in stderr: |
| 131 | + list_result = backend.run( |
| 132 | + f"cd {worktree} && gh pr list --head '{branch}' --json url --jq '.[0].url'", |
| 133 | + check=False, |
| 134 | + ) |
| 135 | + url = list_result.stdout.strip() |
| 136 | + if url: |
| 137 | + return PRResult(url=url, branch=branch) |
| 138 | + raise SystemExit(f"gh pr create failed: {stderr or result.stdout}") |
| 139 | + |
| 140 | + return PRResult(url=url, branch=branch) |
0 commit comments