[fix]. Restore PR-scoped review + remove fix cmd
This commit is contained in:
@@ -1,32 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shlex
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Any
|
||||
|
||||
from gitea_codex_bot.services.gitea import PullRequestContext
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
|
||||
|
||||
class ReviewError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _run_git(args: list[str], cwd: Path | None = None) -> str:
|
||||
completed = subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True)
|
||||
return completed.stdout
|
||||
|
||||
|
||||
def checkout_pr(tmpdir: Path, pr: PullRequestContext) -> Path:
|
||||
repo_dir = tmpdir / "repo"
|
||||
_run_git(["clone", "--no-tags", "--depth", "50", pr.clone_url, str(repo_dir)])
|
||||
_run_git(["fetch", "origin", pr.base_ref, pr.head_ref], cwd=repo_dir)
|
||||
_run_git(["checkout", pr.head_sha], cwd=repo_dir)
|
||||
return repo_dir
|
||||
|
||||
|
||||
def normalize_review_result(result: Any) -> dict[str, Any]:
|
||||
if not isinstance(result, dict):
|
||||
raise ReviewError(f"Invalid review result type: {type(result)!r}")
|
||||
@@ -39,44 +19,3 @@ def normalize_review_result(result: Any) -> dict[str, Any]:
|
||||
if "confidence" not in result:
|
||||
result["confidence"] = 0.5
|
||||
return result
|
||||
|
||||
|
||||
def summarize_command(command: ParsedCommand) -> str:
|
||||
return " ".join(["@codex", command.name, *command.arguments]).strip()
|
||||
|
||||
|
||||
def fix_branch_name(pr_number: int, arguments: list[str] | None = None) -> str:
|
||||
suffix = "fix"
|
||||
if arguments:
|
||||
words = [token.lower().strip() for token in arguments if token.strip() and not token.startswith("--")]
|
||||
if words:
|
||||
clean = "-".join(words[:4])
|
||||
cleaned = "".join(ch if ch.isalnum() or ch == "-" else "-" for ch in clean).strip("-")
|
||||
if cleaned:
|
||||
suffix = f"fix-{cleaned}"
|
||||
return f"codex/pr-{pr_number}-{suffix}"
|
||||
|
||||
|
||||
def create_fix_patch_note(command: ParsedCommand) -> str:
|
||||
details = shlex.join(command.arguments) if command.arguments else "latest findings"
|
||||
return f"Fix command requested for {details}."
|
||||
|
||||
|
||||
def create_fix_branch(
|
||||
pr: PullRequestContext,
|
||||
*,
|
||||
note: str,
|
||||
arguments: list[str] | None = None,
|
||||
) -> str:
|
||||
branch = fix_branch_name(pr.pr_number, arguments=arguments)
|
||||
with TemporaryDirectory(prefix="gitea-codex-fix-") as tmp:
|
||||
tmpdir = Path(tmp)
|
||||
repo_dir = checkout_pr(tmpdir, pr)
|
||||
_run_git(["checkout", "-b", branch], cwd=repo_dir)
|
||||
notes_dir = repo_dir / ".codex"
|
||||
notes_dir.mkdir(parents=True, exist_ok=True)
|
||||
(notes_dir / "fix-note.md").write_text(f"# Codex Fix Note\n\n{note}\n", encoding="utf-8")
|
||||
_run_git(["add", ".codex/fix-note.md"], cwd=repo_dir)
|
||||
_run_git(["-c", "user.name=codex-bot", "-c", "user.email=codex-bot@example.invalid", "commit", "-m", f"Codex fix note for PR {pr.pr_number}"], cwd=repo_dir)
|
||||
_run_git(["push", "origin", f"{branch}:{branch}", "--force"], cwd=repo_dir)
|
||||
return branch
|
||||
|
||||
Reference in New Issue
Block a user