Ticket #188: email2trac-by-subject.patch

File email2trac-by-subject.patch, 2.4 KB (added by icon@…, 14 years ago)

Attempted solution

  • (a) work/svn/email2trac/email2trac.py.in vs. (b) email2trac-modified

    a b  
    197197                else:
    198198                        self.TICKET_UPDATE = 0
    199199
     200                if parameters.has_key('ticket_update_by_subject'):
     201                        self.TICKET_UPDATE_BY_SUBJECT = int(parameters['ticket_update_by_subject'])
     202                else:
     203                        self.TICKET_UPDATE_BY_SUBJECT = 0
     204
    200205                if parameters.has_key('drop_spam'):
    201206                        self.DROP_SPAM = int(parameters['drop_spam'])
    202207                else:
     
    12111223                        elif result.group('new_fields'):
    12121224                                self.new_ticket(m, subject[:result.start('new_fields')], spam_msg, result.group('new_fields'))
    12131225
    1214                 # Create ticket
    1215                 #
    12161226                else:
    1217                         self.new_ticket(m, subject, spam_msg)
     1227                        if self.TICKET_UPDATE and self.TICKET_UPDATE_BY_SUBJECT:
     1228                                if self.DEBUG:
     1229                                        print 'TD: Will try to figure out ticket by subject'
     1230
     1231                                # This list of Re: prefixes is probably incomplete. Taken from
     1232                                # wikipedia.
     1233                                re_regex = re.compile(r'^(RE|AW|VS|SV):\s*(.*)', re.IGNORECASE)
     1234                                result = re_regex.search(subject)
     1235
     1236                                if result:
     1237                                        # This is a reply
     1238                                        orig_subject = result.group(2)
     1239                                        # query for any open tickets that match the subject
     1240                                        cursor = self.db.cursor()
     1241
     1242                                        summaries = [orig_subject, subject]
     1243
     1244                                        # hard-code the time to 30 days for now. Can easily be
     1245                                        # made configurable
     1246                                        lookback = int(time.mktime(time.gmtime())) - 2592000
     1247
     1248                                        matched_id = 0
     1249                                        for summary in summaries:
     1250                                                if self.DEBUG:
     1251                                                        print 'TD: Looking for summary matching: "%s"' % summary
     1252                                                sql = """SELECT id FROM ticket
     1253                                                                                  WHERE changetime >= %s
     1254                                                                                        AND summary = %s
     1255                                                                                        ORDER BY changetime DESC"""
     1256                                                cursor.execute(sql, [lookback, summary.strip()])
     1257
     1258                                                for row in cursor:
     1259                                                        (matched_id,) = row
     1260                                                        if self.DEBUG:
     1261                                                                print 'TD: Found matching ticket id: %d' % matched_id
     1262                                                        break
     1263
     1264                                                if matched_id:
     1265                                                        break
     1266
     1267                                        if matched_id:
     1268                                                # Change the id to string, as expected by ticket_update
     1269                                                matched_id = '#%d:' % matched_id
     1270                                                self.ticket_update(m, matched_id, spam_msg)
     1271                                                return
     1272
     1273                                # Not a reply or no matches found, create a new ticket
     1274                                self.new_ticket(m, subject, spam_msg)
     1275
     1276                        else:
     1277                                # No update by subject, so just create a new ticket
     1278                                self.new_ticket(m, subject, spam_msg)
    12181279
    12191280########## BODY TEXT functions  ###########################################################
    12201281