Skip to content

Commit

Permalink
Merge branch 'feature/idretrieval' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
DrLuke committed Sep 25, 2018
2 parents ebed121 + 48c90ef commit dfea125
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
13 changes: 7 additions & 6 deletions jicket/jicket/jiraintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ def processMail(self) -> Tuple[bool, bool]:

def findIssue(self) -> List[jira.Issue]:
"""Check if issue for ticketid exists already"""
issues = self.jira.search_issues("project = %s AND summary~'\\\\[\\\\#%s\\\\]'" % (self.config.project, self.mail.tickethash))
issues = self.jira.search_issues("project = %s AND summary~'\\\\[\\\\#%s\\\\]'" % (self.config.project, self.mail.prefixedhash))

return issues


def newIssue(self):
"""Create a new issue from Mail"""
log.info("Creating new Issue for #%s in project %s" % (self.mail.tickethash, self.config.project))
log.info("Creating new Issue for #%s in project %s" % (self.mail.prefixedhash, self.config.project))

# Construct string for description
description = ""
description += "Imported by Jicket (SequentialID: %i)\n\n\n" % self.mail.ticketid
description += "Imported by Jicket (SequentialID: %i)" % self.mail.ticketid
description += "From: %s\n\n\n" % self.mail.parsed["From"]
description += re.sub("(\n.*?)\n", "\g<1>", html2text.html2text(self.mail.body)) # Remove every second newline which is added to distinguish between paragraphs in Markdown, but makes the jira ticket hard to read.

issuedict = {
"project": {"key": self.config.project},
"summary": "[#%s] %s" % (self.mail.tickethash, self.mail.subject),
"summary": "[#%s] %s" % (self.mail.prefixedhash, self.mail.subject),
"description": description,
"issuetype": {"name": "Task"}
}
Expand All @@ -68,10 +69,10 @@ def newIssue(self):

def updateIssue(self, issue: jira.Issue):
"""Update issue from mail"""
log.info("Updating Issue for #%s in project %s" % (self.mail.tickethash, self.config.project))
log.info("Updating Issue for #%s in project %s" % (self.mail.prefixedhash, self.config.project))

commenttext = ""
commenttext += "Imported by Jicket (SequentialID: %i)\n\n\n" % self.mail.ticketid
commenttext += "From: %s\n\n\n" % self.mail.parsed["From"]
commenttext += re.sub("(\n.*?)\n", "\g<1>", html2text.html2text(self.mail.body))

comment = self.jira.add_comment(issue, commenttext) # TODO: error checking
38 changes: 28 additions & 10 deletions jicket/jicket/mailhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self):

self.ticketAddress = None # type: str # Address of jicket mailbox

self.idPrefix = "JI" # type: str
self.idPrefix = "JI-" # type: str
self.idSalt = "JicketSalt" # type: str
self.idAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" # type: str
self.idMinLength = 6 # type: int
Expand All @@ -53,6 +53,9 @@ def __init__(self, uid: int, rawmailcontent: bytes, config: MailConfig):
self.parsed = None # type: email.message.Message
self.ticketid = None # type: int # ID of ticket
self.tickethash = None # type: str # Hashed ticket ID
self.prefixedhash = None # type: str # Hashed ticket ID with prefix

self.threadstarter = False

self.process()
self.determineTicketID()
Expand All @@ -72,6 +75,10 @@ def process(self) -> None:

self.subject = self.parsed["subject"]
# TODO: Get all attachments

if self.parsed["X-Jicket-Initial-ReplyID"] is not None and self.parsed["X-Jicket-Initial-ReplyID"] == self.parsed["In-Reply-To"]:
self.threadstarter = True

self.rawmailcontent = None # No need to store after processing

def determineTicketID(self):
Expand All @@ -81,14 +88,21 @@ def determineTicketID(self):
"""
hashid = hashids.Hashids(salt=self.config.idSalt, alphabet=self.config.idAlphabet, min_length=self.config.idMinLength)

idregex = "\[%s-([%s]{%i,}?)\]" % (self.config.idPrefix, self.config.idAlphabet, self.config.idMinLength)
match = re.match(idregex, self.subject)
if match:
self.tickethash = match.group(1)
self.ticketid = hashid.decode(self.tickethash)
# See if hashid is set in headers
if self.parsed["X-Jicket-HashID"] is not None:
self.tickethash = self.parsed["X-Jicket-HashID"]
self.ticketid = hashid.decode(self.parsed["X-Jicket-HashID"])
else:
self.tickethash = hashid.encode(self.uid)
self.ticketid = self.uid
idregex = "\\[#%s([%s]{%i,}?)\\]" % (re.escape(self.config.idPrefix), re.escape(self.config.idAlphabet), self.config.idMinLength)
match = re.search(idregex, self.subject)
if match:
self.tickethash = match.group(1)
self.ticketid = hashid.decode(self.tickethash)
else:
self.tickethash = hashid.encode(self.uid)
self.ticketid = self.uid

self.prefixedhash = self.config.idPrefix + self.tickethash


class MailImporter():
Expand Down Expand Up @@ -156,7 +170,8 @@ def fetchMails(self) -> List[ProcessedMail]:
def moveImported(self, mail):
"""Move successfully imported mails to success folder"""
self.IMAP.uid("copy", str(mail.uid).encode(), self.mailconfig.folderSuccess)
self.IMAP.uid("store", str(mail.uid).encode(), "+FLAGS", "(\Deleted)")
self.IMAP.uid("store", str(mail.uid).encode(), "+flags", "(\Deleted)")
self.IMAP.expunge()


class MailExporter():
Expand Down Expand Up @@ -195,12 +210,15 @@ def sendTicketStart(self, mail: ProcessedMail):

# Add Jicket headers
threadstarter["X-Jicket-HashID"] = mail.tickethash
# Initial ID this is a reply to. It is used to identify if this is a threadstarter email or regular mail.
# Treadstarter mails should be ignored on import, as they're only of informative nature.
threadstarter["X-Jicket-Initial-ReplyID"] = mail.parsed["Message-ID"].rstrip()

# Set other headers
threadstarter["To"] = "%s, %s" % (mail.parsed["From"], self.mailconfig.ticketAddress)
threadstarter["From"] = self.mailconfig.ticketAddress
threadstarter["In-Reply-To"] = mail.parsed["Message-ID"].rstrip()
threadstarter["Subject"] = "[#%s] %s" % (mail.tickethash, mail.subject)
threadstarter["Subject"] = "[#%s%s] %s" % (self.mailconfig.idPrefix, mail.tickethash, mail.subject)

# Send mail
self.sendmail(threadstarter)

0 comments on commit dfea125

Please sign in to comment.