diff options
-rwxr-xr-x | import_issues.py | 112 |
1 files changed, 75 insertions, 37 deletions
diff --git a/import_issues.py b/import_issues.py index 642f9c6..83823a2 100755 --- a/import_issues.py +++ b/import_issues.py @@ -127,6 +127,7 @@ import logging import json import os import re +import subprocess import smtplib import sys import time @@ -232,6 +233,19 @@ def do_mail( raise RuntimeError(f"Unknown mode: {mode!r}") +def run_hut(cmds, tracker, msg, args=None): + if args is None: + args = [] + res = subprocess.run( + ["hut", "todo"] + cmds + ["-t", tracker] + args, + encoding="utf-8", + input=msg, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + return res + + def open_ticket( *, smtp, @@ -279,15 +293,21 @@ def open_ticket( lines.append("") lines.append(body) - do_mail( - smtp=smtp, - delay=delay, - mode=mode, - frm=frm, - to=f"{tracker}@todo.sr.ht", - subject=title, - body="\n".join(lines), - ) + if mode in ["send", "print"]: + do_mail( + smtp=smtp, + delay=delay, + mode=mode, + frm=frm, + to=f"{tracker}@todo.sr.ht", + subject=title, + body="\n".join(lines), + ) + elif mode == "hut": + msg = title + if len(lines): + msg += "\n" + "\n".join(lines) + run_hut(["ticket", "create", "--stdin"], tracker, msg) issue_count += 1 return issue_count @@ -304,15 +324,19 @@ def file_missing_ticket( ): global issue_count - do_mail( - smtp=smtp, - delay=delay, - mode=mode, - frm=frm, - to=f"{tracker}@todo.sr.ht", - subject="Missing issue", - body=f"Issue {issue_id} is not known.", - ) + if mode in ["send", "print"]: + do_mail( + smtp=smtp, + delay=delay, + mode=mode, + frm=frm, + to=f"{tracker}@todo.sr.ht", + subject="Missing issue", + body=f"Issue {issue_id} is not known.", + ) + elif mode == "hut": + msg = f"Missing issue\n\nIssue {issue_id} is not known." + run_hut(["ticket", "create"], tracker, msg, ["--stdin"]) issue_count += 1 @@ -369,14 +393,18 @@ def send_comment( lines.append("") lines.append(f"(Last edited at {last_edited_at}.)") - do_mail( - smtp=smtp, - delay=delay, - mode=mode, - frm=frm, - to=f"{tracker}/{issue_id}@todo.sr.ht", - body="\n".join(lines), - ) + body = "\n".join(lines) + + if mode in ["send", "print"]: + do_mail( + smtp=smtp, + delay=delay, + mode=mode, + frm=frm, + to=f"{tracker}/{issue_id}@todo.sr.ht", + ) + elif mode == "hut": + run_hut(["ticket", "comment"], tracker, None, ["--stdin", issue_id]) def close_ticket( @@ -398,17 +426,26 @@ def close_ticket( elif is_closed: lines.append("Ticket closed.") - lines.append("") - lines.append("!resolve fixed") + if mode in ["send", "print"]: + lines.append("") + lines.append("!resolve fixed") - do_mail( - smtp=smtp, - delay=delay, - mode=mode, - frm=frm, - to=f"{tracker}/{issue_id}@todo.sr.ht", - body="\n".join(lines), - ) + do_mail( + smtp=smtp, + delay=delay, + mode=mode, + frm=frm, + to=f"{tracker}/{issue_id}@todo.sr.ht", + body="\n".join(lines), + ) + + elif mode == "hut": + run_hut( + ["ticket", "update-status"], + tracker, + None, + ["--resolution", "fixed", "--status", "closed", issue_id], + ) def run( @@ -671,8 +708,9 @@ def main(): parser.add_argument( "--mode", + choices=["print", "send", "hut"], default="print", - help="Action to take, 'print' or 'send'.", + help="Action to take.", ) parser.add_argument( |