File: ticket_event_listener.py

package info (click to toggle)
trac-codecomments 1.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 348 kB
  • ctags: 191
  • sloc: python: 593; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 2,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from trac.core import *
from trac.ticket.api import ITicketChangeListener

import re

from code_comments.comment_macro import CodeCommentLinkMacro

class UpdateTicketCodeComments(Component):
    """Automatically stores relations to CodeComments whenever a ticket is saved or created
    Note: This does not catch edits on replies right away but on the next change of the ticket or when adding a new reply
    """

    implements(ITicketChangeListener)

    def ticket_created(self, ticket):
        self.update_relations(ticket)

    def ticket_changed(self, ticket, comment, author, old_values):
        self.update_relations(ticket)

    def ticket_deleted(self, ticket):
        self.update_relations(ticket)

    def update_relations(self, ticket):
        comment_ids = []
        # (time, author, field, oldvalue, newvalue, permanent)
        changes = ticket.get_changelog()
        description = ticket['description']

        comment_ids += re.findall(CodeCommentLinkMacro.re, description)
        if changes:
            for change in changes:
                if change[2] == 'comment':
                    comment_ids += re.findall(CodeCommentLinkMacro.re, change[4])

        comment_ids = set(comment_ids)
        comment_ids_csv = ','.join(comment_ids)

        existing_comments_query = "SELECT * FROM ticket_custom WHERE ticket = %s AND name = 'code_comment_relation'"
        existing_comments = self.fetch(existing_comments_query, [ticket.id])

        if existing_comments:
            self.query("UPDATE ticket_custom SET value=%s WHERE ticket=%s AND name='code_comment_relation'", [comment_ids_csv, ticket.id])
        else:
            self.query("INSERT INTO ticket_custom (ticket, name, value) VALUES (%s, 'code_comment_relation', %s)", [ticket.id, comment_ids_csv])

    def query(self, query, args = [], result_callback=None):
        if result_callback is None:
            result_callback = lambda db, cursor: True
        result = {}
        @self.env.with_transaction()
        def insert_comment(db):
            cursor = db.cursor()
            cursor.execute(query, args)
            result['result'] = result_callback(db, cursor)
        return result['result']

    def fetch(self, query, args = []):
        return self.query(query, args, lambda db, cursor: cursor.fetchall())