From e7d520140e5a0c552d51323e34dc6d381849fc9b Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Sat, 14 Oct 2023 19:57:52 +0200 Subject: [PATCH] mbox: add attachment with diff If the description changes, a file with a diff of the changes is attached to the email. Signed-off-by: Alexey Gladkov --- jiramail/mbox.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/jiramail/mbox.py b/jiramail/mbox.py index 8d13daa..2ded91c 100644 --- a/jiramail/mbox.py +++ b/jiramail/mbox.py @@ -5,6 +5,8 @@ __author__ = 'Alexey Gladkov ' import argparse +import collections +import difflib import email import email.utils import re @@ -316,6 +318,43 @@ def comment_email(issue: jira.resources.Issue, comment: jira.resources.Comment, return mail +def attach_description_diff(mail: email.message.EmailMessage, + item: jira.resources.PropertyHolder) -> None: + if not hasattr(item, "fromString") or \ + not hasattr(item, "toString"): + return + + diff = difflib.ndiff( + item.fromString.splitlines(keepends=True), + item.toString.splitlines(keepends=True)) + + max_context = 3 + changes = [] + + context: collections.deque[str] = collections.deque(maxlen=max_context) + context_after: bool = False + + for line in diff: + if not line.startswith(" "): + while len(context) > 0: + changes.append(context.popleft()) + changes.append(line) + context_after = True + else: + if context_after and len(context) == max_context: + while len(context) > 0: + changes.append(context.popleft()) + context_after = False + context.append(line) + + while context_after and len(context) > 0: + changes.append(context.popleft()) + + mail.add_attachment("".join(changes).encode(), + filename="description.diff", + maintype="text", subtype="x-diff") + + def add_issue(issue: jira.resources.Issue, mbox: jiramail.Mailbox) -> None: logger.debug("processing issue %s ...", issue.key) # pprint.pprint(issue.raw) @@ -369,6 +408,7 @@ def add_issue(issue: jira.resources.Issue, mbox: jiramail.Mailbox) -> None: mail = issue_email(issue, date, User(prop.author), subject, item.fromString or "") + attach_description_diff(mail, item) mbox.append(mail) date = prop.created