diff --git a/jiramail/change.py b/jiramail/change.py index fca9931..d661410 100644 --- a/jiramail/change.py +++ b/jiramail/change.py @@ -73,6 +73,8 @@ def command_issue_comment(issue: jira.resources.Issue, try: if not dry_run: jiramail.jserv.jira.add_comment(issue, text) + else: + logger.critical("add comment to %s: %s", issue.key, text.encode()) except jira.exceptions.JIRAError as e: return jiramail.Error(f"unable to add comment to issue {issue.key}: {e}") @@ -522,6 +524,31 @@ def process_mail(mail: email.message.Message, return rc +def comment_mail(mail: email.message.Message) -> bool: + key = find_issue_key(mail) + if not key: + logger.critical("issue number not found. Maybe it's because you don't reply to the generated email.") + return False + + try: + issue = jiramail.jserv.jira.issue(key) + except jira.exceptions.JIRAError as e: + logger.critical("unable to get %s issue: %s", key, e.text) + return False + + for part in mail.walk(): + if part.get_content_type() != "text/plain": + continue + + res = command_issue_comment(issue, part.get_payload()) + + if isinstance(res, jiramail.Error): + logger.critical("%s", res.message) + return False + + return True + + def main(cmdargs: argparse.Namespace) -> int: global dry_run, no_reply diff --git a/jiramail/smtp.py b/jiramail/smtp.py index 459bcfd..2d70f3c 100644 --- a/jiramail/smtp.py +++ b/jiramail/smtp.py @@ -7,6 +7,7 @@ import argparse import email import email.message +import email.utils import re import socket @@ -170,6 +171,14 @@ def command_rcpt(state: Dict[str, Any], args: str) -> SMTPAnswer: return SMTPAnswer("250 2.1.5 Ok\r\n") +def get_mode(mail: email.message.Message) -> str: + for e in mail.get_all("To", []): + (_, addr) = email.utils.parseaddr(e) + if addr == "comment@jira": + return "comment" + return "change" + + def command_data(state: Dict[str, Any]) -> SMTPAnswer: if "user" in state and not state["authorized"]: return SMTPAnswer("530 Authentication required\r\n") @@ -203,9 +212,15 @@ def command_data(state: Dict[str, Any]) -> SMTPAnswer: replies: List[email.message.EmailMessage] = [] - if not jiramail.change.process_mail(mail, replies): - logger.critical("error: mail processing failed") - return SMTPAnswer("451 4.3.0 Requested action aborted: local error in processing\r\n") + match get_mode(mail): + case "comment": + if not jiramail.change.comment_mail(mail): + logger.critical("error: mail processing failed") + return SMTPAnswer("451 4.3.0 Requested action aborted: local error in processing\r\n") + case _: + if not jiramail.change.process_mail(mail, replies): + logger.critical("error: mail processing failed") + return SMTPAnswer("451 4.3.0 Requested action aborted: local error in processing\r\n") if "mbox" in state: for reply in replies: