Skip to content

Commit

Permalink
mbox: add attachment with diff
Browse files Browse the repository at this point in the history
If the description changes, a file with a diff of the changes is
attached to the email.

Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Oct 14, 2023
1 parent c376187 commit e7d5201
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions jiramail/mbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
__author__ = 'Alexey Gladkov <[email protected]>'

import argparse
import collections
import difflib
import email
import email.utils
import re
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7d5201

Please sign in to comment.