-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
__author__ = 'Alexey Gladkov <[email protected]>' | ||
|
||
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 | ||
|