Files
reliquary/plugins/gitea/skills/gitea/scripts/start_ticket.py
T
g4borg f6dc43be19 ✨ export: 36 files (features)
M plugins/gitea/.claude-plugin/plugin.json
M plugins/gitea/.codex-plugin/plugin.json
M plugins/gitea/README.md
M plugins/gitea/skills/gitea/SKILL.md
M plugins/gitea/skills/gitea/scripts/fetch_ticket.py
M plugins/gitea/skills/gitea/scripts/start_ticket.py
A plugins/spire/UPGRADES.md
A plugins/spire/agents/designer.md
M plugins/spire/agents/planner.md
A plugins/spire/providers/README.md
A plugins/spire/providers/hosts/claude-desktop/spawning.md
A plugins/spire/providers/hosts/t3-code/spawning.md
A plugins/spire/providers/runtimes/codex/spawning.md
M plugins/spire/skills/gremlins/grm/call.py
M plugins/spire/skills/gremlins/grm/cli.py
A plugins/spire/skills/gremlins/grm/codex.py
A plugins/spire/skills/gremlins/grm/errors.py
A plugins/spire/skills/gremlins/grm/harness.py
M plugins/spire/skills/gremlins/grm/launch.py
A plugins/spire/skills/gremlins/grm/scrub.py
M plugins/spire/skills/gremlins/grm/stream.py
A plugins/spire/skills/gremlins/grm/title.py
A plugins/spire/skills/gremlins/tests/_pin.py
M plugins/spire/skills/gremlins/tests/fake_claude.py
A plugins/spire/skills/gremlins/tests/fake_codex.py
M plugins/spire/skills/gremlins/tests/test_cli.py
A plugins/spire/skills/gremlins/tests/test_cli_codex.py
A plugins/spire/skills/gremlins/tests/test_codex.py
A plugins/spire/skills/gremlins/tests/test_harness.py
A plugins/spire/skills/gremlins/tests/test_scrub.py
A plugins/spire/skills/gremlins/tests/test_title.py
A plugins/spire/skills/igor/references/upgrades.md
A plugins/spire/skills/igor/tests/test_upgrade_notes.py
A plugins/spire/skills/igor/upgrade-notes.sh
R plugins/spire/skills/implementation/references/spawning-claude-code.md -> plugins/spire/providers/runtimes/claude-code/spawning.md
M plugins/spire/skills/project/SKILL.md
2026-11-25 01:52:10 +01:00

132 lines
4.5 KiB
Python

#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11"
# dependencies = ["httpx", "pydantic>=2", "python-dotenv"]
# ///
"""Start working on a Gitea ticket: create a branch and fetch ticket context.
Usage:
uv run start_ticket.py <number> --quire <path> [--tickets-dir <path>] [--slug SLUG] [--no-checkout]
"""
from __future__ import annotations
import argparse
import io
import re
import subprocess
import sys
import unicodedata
from pathlib import Path
from client import GiteaClient, GiteaError
from config import load_config
from fetch_ticket import render_issue_markdown, resolve_tickets_dir
def slugify(text: str, max_length: int = 48) -> str:
text = unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode("ascii")
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
text = text.strip("-")
if len(text) <= max_length:
return text
truncated = text[:max_length]
last_hyphen = truncated.rfind("-")
if last_hyphen > max_length // 2:
truncated = truncated[:last_hyphen]
return truncated.rstrip("-")
def git(*args: str) -> tuple[int, str]:
result = subprocess.run(["git", *args], capture_output=True, text=True)
return result.returncode, result.stdout.strip()
def branch_exists_locally(name: str) -> bool:
code, _ = git("rev-parse", "--verify", name)
return code == 0
def branch_exists_remotely(name: str) -> bool:
code, output = git("ls-remote", "--heads", "origin", name)
return code == 0 and bool(output)
def current_branch() -> str:
_, name = git("rev-parse", "--abbrev-ref", "HEAD")
return name
def main() -> None:
parser = argparse.ArgumentParser(description="Start working on a Gitea ticket.")
parser.add_argument("ticket", type=int, help="Ticket number")
parser.add_argument("--quire", "--workpad", dest="quire", type=Path, default=None, help="Quire root; --workpad is the old name")
parser.add_argument("--tickets-dir", type=Path, default=None, help="Direct tickets directory (overrides quire)")
parser.add_argument("--slug", type=str, default=None, help="Custom branch slug")
parser.add_argument("--no-checkout", action="store_true", help="Create branch without switching to it")
args = parser.parse_args()
target = resolve_tickets_dir(args.quire, args.tickets_dir)
config = load_config()
try:
with GiteaClient(config) as client:
issue = client.get_issue(args.ticket)
comments = None
if issue.comments > 0:
comments = client.get_comments(args.ticket)
except GiteaError as e:
print(f"Error fetching ticket: {e}", file=sys.stderr)
sys.exit(1)
slug = args.slug or slugify(issue.title)
if "/" in slug:
print(f"Error: slug contains '/': {slug}", file=sys.stderr)
print("Branch names must have exactly one subpath (ticket/<number>-<slug>).", file=sys.stderr)
sys.exit(1)
branch_name = f"ticket/{args.ticket}-{slug}"
if branch_exists_locally(branch_name):
print(f"Error: branch '{branch_name}' already exists locally.", file=sys.stderr)
print(f"Resume with: git checkout {branch_name}", file=sys.stderr)
sys.exit(1)
if branch_exists_remotely(branch_name):
print(f"Error: branch '{branch_name}' already exists on remote.", file=sys.stderr)
print(f"Resume with: git checkout -b {branch_name} origin/{branch_name}", file=sys.stderr)
sys.exit(1)
cur = current_branch()
if cur not in ("main", "master"):
print(f"Warning: you are on '{cur}', not main. Branch will be created from here.", file=sys.stderr)
if args.no_checkout:
code, _ = git("branch", branch_name)
else:
code, _ = git("checkout", "-b", branch_name)
if code != 0:
print(f"Error: failed to create branch '{branch_name}'.", file=sys.stderr)
sys.exit(1)
ticket_dir = target / str(args.ticket)
ticket_dir.mkdir(parents=True, exist_ok=True)
md = render_issue_markdown(issue, comments)
context_file = ticket_dir / "context.md"
context_file.write_text(md, encoding="utf-8")
out = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
action = "Created" if args.no_checkout else "Checked out"
out.write(f"{action} branch: {branch_name}\n")
out.write(f"Ticket context: {context_file}\n")
out.write(f"Issue: #{issue.number} - {issue.title}\n")
if issue.labels:
out.write(f"Labels: {', '.join(issue.label_names())}\n")
out.flush()
if __name__ == "__main__":
main()