2015年1月29日 星期四

[記事] Enable multiple/group owner of ticket in Trac

屬於阿宅世界的技術文章,想看的再點開,切莫自誤 !


感覺不是太難的功能,但這張 ticket (#2662) 在 trac issue 裡放了九年沒動靜,只好自己克難一下順便做點記錄,免得之後 trac 昇版後要再改出來又要再花時間 (在這個摩門, 1.0.3 又出來了.. = = )

Download the source code and decompress
shell> mkdir -p /home/trac/src
shell> cd /home/trac/src
shell> wget http://download.edgewall.org/trac/Trac-1.0.2.tar.gz
shell> tar zxvf  Trac-1.0.2.tar.gz
shell> cd  Trac-1.0.2

Update the filed format of owner filed in ticket
shell> vi trac/ticket/api.py
(in function : field)
line 304 :
    #add the format field as "list" into field of owner
    field = {'name': 'owner', 'label': N_('Owner')}
    field['type'] = 'text'
    field['format'] = 'list'

Add every owner to the the email recipient in order to bother notify everyone while ticket has been changed
shell> vi trac/ticket/notification.py
(in function get_ticket_notification_recipients)
line 537:
    # modify to_recipients.add(tkt['owner']) to
    owner_list = []
    tmp_owner_list = to_list(tkt['owner'])
    for o in tmp_owner_list:
        result = env.db_query("""
        SELECT username from PERMISSION where action=%s
        """, (o,) )
        if result:
            for user in result:
                owner_list.append(user[0])
            else:
        owner_list.append( o )
    to_recipients.update(owner_list)

Let the owner field render correctly in html
shell> vi trac/ticket/web_ui.py
(in function _process_ticket_request)
line 715:
    # add following lines before return 
    owner_fields_idx = data['fields_map']['owner']
    owner_field = data['fields'][owner_fields_idx]
    data['v_owner_link'] = owner_field['rendered']

shell> vi trac/ticket/templates/ticket_box.html
#replace $v_owner with $v_owner_link

Modify the report sql (My Ticket) in order to get those tickets belong to their true owner correctly , from
SELECT DISTINCT
    p.value AS __color__,
    (CASE
        WHEN owner like '%$USER%' AND status = 'accepted' THEN 'Accepted'
        WHEN owner like '%$USER%' THEN 'Owned'
        WHEN reporter = $USER THEN 'Reported'
        ELSE 'Commented' END) AS __group__,
    t.id AS ticket, summary, component, version, milestone,
    t.type AS type, priority, t.time AS created,
    t.changetime AS _changetime, description AS _description,
    reporter AS _reporter
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN ticket_change tc ON tc.ticket = t.id AND tc.author = $USER
AND tc.field = 'comment'
WHERE
    t.status <> 'closed'
    AND (owner like '%$USER%' OR reporter = $USER OR author = $USER)
ORDER BY
    (owner like '%$USER%' AND status = 'accepted') DESC,
    owner like '%$USER%' DESC, reporter = $USER DESC,
    CAST(p.value AS integer), milestone, t.type, t.time

to
SELECT DISTINCT
    p.value AS __color__,
    (CASE
      WHEN owner like '%$USER%' AND status = 'accepted' THEN 'Accepted'
      WHEN owner like '%$USER%' OR ss.username = '$USER' THEN 'Owned'
      WHEN reporter = 'ywhuang' THEN 'Reported'
      ELSE 'Commented' END) AS __group__,
    t.id AS ticket, summary, component, version, milestone,
    t.type AS type, priority, t.time AS created,
    t.changetime AS _changetime, description AS _description,
    reporter AS _reporter, t.owner, ss.username AS __grpusername__
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN ticket_change tc ON tc.ticket = t.id AND tc.author = 'ywhuang'
AND tc.field = 'comment'
LEFT JOIN permission ss on ss.username = '$USER' AND ss.action = t.owner
WHERE
    t.status <> 'closed'
    AND (owner like '%$USER%' OR reporter = '$USER' OR author = '$USER')
ORDER BY
    (owner like '%$USER%' AND status = 'accepted') DESC,
    (owner like '%$USER%' OR ss.username = '$USER') DESC,
    reporter = '$USER' DESC,
    CAST(p.value AS integer), milestone, t.type, t.time

Rebuild and deploy Trac
shell> python setup.py clean #(if u want)
shell> python setup.py build
shell> python setup.py install

p.s
u may start the trac daemon by
shell> /usr/bin/tracd -p -pidfile=/var/log/trac-debug.pid
and print directly in the code to get the debug message from console , much more convicent than using the log.info fuction (which output message into /log/trac.log)

know issue :
someone accept the ticket will set the owner of ticket to himself only

做完就會發現一張 ticket 有多個/群組的owner很多地方違反 trac 一開始設計 ticket 的流程與概念,甚至群組這東西在資料庫中連個正式的資料表都沒有,是依附在權限(PERMISSION)下面,難怪一直不想改

改完這個,可以開始動工加上 routine ticket and notification 的功能了

0 意見:

張貼留言