Skip to content

Commit

Permalink
Use comment@jira to comment
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Nov 15, 2023
1 parent 07b2d00 commit a43fdc5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
27 changes: 27 additions & 0 deletions jiramail/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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

Expand Down
21 changes: 18 additions & 3 deletions jiramail/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import email
import email.message
import email.utils
import re
import socket

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a43fdc5

Please sign in to comment.