#!/usr/bin/env python3 """ Create one Gitea issue from one email (by UID), then mark the email as read. If --title and/or --body are provided (formalized by agent), use them; else use subject and body from the email. Usage: ./gitea-issues/mail-create-issue-from-email.sh --uid [--title "..." ] [--body "..." ] """ from __future__ import annotations import argparse import email import imaplib import ssl import sys from email.header import decode_header from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from mail_common import ( create_gitea_issue, load_gitea_config, load_imap_config, repo_root, sanitize_title, ) def decode_header_value(header: str | None) -> str: if not header: return "" from email.header import decode_header as dh parts = dh(header) result = [] for part, charset in parts: if isinstance(part, bytes): result.append(part.decode(charset or "utf-8", errors="replace")) else: result.append(part) return "".join(result) def get_text_body(msg: email.message.Message) -> str: if msg.is_multipart(): for part in msg.walk(): if part.get_content_type() == "text/plain": payload = part.get_payload(decode=True) if payload: return payload.decode(part.get_content_charset() or "utf-8", errors="replace") return "" payload = msg.get_payload(decode=True) if not payload: return "" return payload.decode(msg.get_content_charset() or "utf-8", errors="replace") def main() -> None: ap = argparse.ArgumentParser(description="Create one Gitea issue from one email by UID") ap.add_argument("--uid", required=True, help="IMAP message UID") ap.add_argument("--title", default="", help="Formalized issue title (else use subject)") ap.add_argument("--body", default="", help="Formalized issue body (else use email body + From)") args = ap.parse_args() cfg = load_imap_config() if not cfg["user"] or not cfg["password"]: root = repo_root() env_path = root / ".secrets" / "gitea-issues" / "imap-bridge.env" print("[gitea-issues] ERROR: IMAP_USER and IMAP_PASSWORD required.", file=sys.stderr) sys.exit(1) gitea = load_gitea_config() if not gitea["token"]: print("[gitea-issues] ERROR: GITEA_TOKEN not set.", file=sys.stderr) sys.exit(1) mail = imaplib.IMAP4(cfg["host"], int(cfg["port"])) if cfg["use_starttls"]: mail.starttls(ssl.create_default_context()) mail.login(cfg["user"], cfg["password"]) mail.select("INBOX") _, data = mail.fetch(args.uid, "(RFC822)") if not data or not data[0]: print("[gitea-issues] ERROR: Message UID not found.", file=sys.stderr) mail.logout() sys.exit(1) msg = email.message_from_bytes(data[0][1]) from_ = decode_header_value(msg.get("From")) subject = decode_header_value(msg.get("Subject")) body_text = get_text_body(msg) body_for_issue = f"**From:** {from_}\n\n{body_text}".strip() title = args.title.strip() if args.title else sanitize_title(subject) body = args.body.strip() if args.body else body_for_issue issue = create_gitea_issue(title, body) if not issue: print("[gitea-issues] ERROR: Failed to create issue.", file=sys.stderr) mail.logout() sys.exit(1) mail.store(args.uid, "+FLAGS", "\\Seen") mail.logout() num = issue.get("number", "?") print(f"[gitea-issues] Created issue #{num}: {title[:60]}") print(f"ISSUE_NUMBER={num}") if __name__ == "__main__": main()