110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# dependencies = ["httpx", "pydantic>=2", "python-dotenv"]
|
|
# ///
|
|
"""Fetch a Gitea ticket and write structured markdown.
|
|
|
|
Usage:
|
|
uv run fetch_ticket.py <number> --workpad <path> [--tickets-dir <path>] [--no-comments]
|
|
|
|
Output: <tickets-dir>/<number>/context.md
|
|
(default tickets-dir = <workpad>/tickets)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
from client import GiteaClient, GiteaError
|
|
from config import load_config
|
|
from models import GiteaComment, GiteaIssue
|
|
|
|
|
|
def render_issue_markdown(issue: GiteaIssue, comments: List[GiteaComment] | None = None) -> str:
|
|
lines: list[str] = []
|
|
lines.append(f"# #{issue.number}: {issue.title}")
|
|
lines.append("")
|
|
lines.append(f"**State:** {issue.state}")
|
|
lines.append(f"**Created:** {issue.created_at.strftime('%Y-%m-%d %H:%M')}")
|
|
lines.append(f"**Author:** {issue.user.login}")
|
|
if issue.labels:
|
|
lines.append(f"**Labels:** {', '.join(issue.label_names())}")
|
|
if issue.milestone:
|
|
lines.append(f"**Milestone:** {issue.milestone.title}")
|
|
if issue.assignees:
|
|
lines.append(f"**Assignees:** {', '.join(a.login for a in issue.assignees)}")
|
|
if issue.html_url:
|
|
lines.append(f"**URL:** {issue.html_url}")
|
|
lines.append("")
|
|
lines.append("---")
|
|
lines.append("")
|
|
lines.append("## Description")
|
|
lines.append("")
|
|
lines.append(issue.body if issue.body else "*No description provided.*")
|
|
lines.append("")
|
|
if comments:
|
|
lines.append("---")
|
|
lines.append("")
|
|
lines.append(f"## Comments ({len(comments)})")
|
|
lines.append("")
|
|
for comment in comments:
|
|
lines.append(f"### {comment.user.login} ({comment.created_at.strftime('%Y-%m-%d %H:%M')})")
|
|
lines.append("")
|
|
lines.append(comment.body)
|
|
lines.append("")
|
|
lines.append("---")
|
|
lines.append("")
|
|
lines.append(f"*Fetched at {datetime.now().strftime('%Y-%m-%d %H:%M')}*")
|
|
lines.append("")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def resolve_tickets_dir(workpad: Path | None, tickets_dir: Path | None) -> Path:
|
|
if tickets_dir is not None:
|
|
return tickets_dir
|
|
if workpad is None:
|
|
print("Error: either --workpad or --tickets-dir must be provided.", file=sys.stderr)
|
|
sys.exit(1)
|
|
return workpad / "tickets"
|
|
|
|
|
|
def fetch_and_write(ticket_number: int, target_dir: Path, include_comments: bool = True) -> Path:
|
|
config = load_config()
|
|
with GiteaClient(config) as client:
|
|
issue = client.get_issue(ticket_number)
|
|
comments = None
|
|
if include_comments and issue.comments > 0:
|
|
comments = client.get_comments(ticket_number)
|
|
md = render_issue_markdown(issue, comments)
|
|
out_dir = target_dir / str(ticket_number)
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
out_file = out_dir / "context.md"
|
|
out_file.write_text(md, encoding="utf-8")
|
|
return out_file
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="Fetch a Gitea ticket as markdown.")
|
|
parser.add_argument("ticket", type=int, help="Ticket number to fetch")
|
|
parser.add_argument("--workpad", type=Path, default=None, help="Workpad root (tickets go to <workpad>/tickets/<n>/)")
|
|
parser.add_argument("--tickets-dir", type=Path, default=None, help="Direct tickets directory (overrides workpad)")
|
|
parser.add_argument("--no-comments", action="store_true", help="Skip fetching comments")
|
|
args = parser.parse_args()
|
|
|
|
target = resolve_tickets_dir(args.workpad, args.tickets_dir)
|
|
try:
|
|
out = fetch_and_write(args.ticket, target, include_comments=not args.no_comments)
|
|
print(f"Wrote ticket #{args.ticket} to {out}")
|
|
except GiteaError as e:
|
|
print(f"Error: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|