2012-10-24 16:32:52 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Humbug trac plugin -- sends humbugs when tickets change.
|
|
|
|
#
|
|
|
|
# Install by placing in the plugins/ subdirectory and then adding
|
|
|
|
# "humbug_trac" to the [components] section of the conf/trac.ini file,
|
|
|
|
# like so:
|
|
|
|
#
|
|
|
|
# [components]
|
|
|
|
# humbug_trac = enabled
|
2012-10-24 18:04:05 -04:00
|
|
|
#
|
|
|
|
# You may then need to restart trac (or restart Apache) for the bot
|
|
|
|
# (or changes to the bot) to actually be loaded by trac.
|
|
|
|
#
|
|
|
|
# Our install is trac.humbughq.com:/home/humbug/trac/
|
2012-10-24 16:32:52 -04:00
|
|
|
|
2012-10-24 18:19:50 -04:00
|
|
|
from trac.core import Component, implements
|
2012-10-24 16:32:52 -04:00
|
|
|
from trac.ticket import ITicketChangeListener
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.append("/home/humbug/humbug")
|
|
|
|
import api.common
|
|
|
|
client = api.common.HumbugAPI(email="humbug+trac@humbughq.com",
|
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
|
|
|
|
|
|
|
def markdown_ticket_url(ticket, heading="ticket"):
|
|
|
|
return "[%s #%s](https://trac.humbughq.com/ticket/%s)" % (heading, ticket.id, ticket.id)
|
|
|
|
|
|
|
|
def markdown_block(desc):
|
2012-10-24 18:20:34 -04:00
|
|
|
return "\n\n>" + "\n> ".join(desc.split("\n")) + "\n"
|
2012-10-24 16:32:52 -04:00
|
|
|
|
2012-10-31 11:49:17 -04:00
|
|
|
def truncate(string, length):
|
|
|
|
if len(string) <= length:
|
|
|
|
return string
|
|
|
|
return string[:length - 3] + "..."
|
|
|
|
|
2012-10-24 16:32:52 -04:00
|
|
|
def trac_subject(ticket):
|
2012-10-31 12:08:41 -04:00
|
|
|
return truncate("#%s: %s" % (ticket.id, ticket.values.get("summary")), 60)
|
2012-10-24 16:32:52 -04:00
|
|
|
|
|
|
|
def send_update(ticket, content):
|
|
|
|
client.send_message({
|
|
|
|
"type": "stream",
|
2012-10-30 16:10:13 -04:00
|
|
|
"stream": "trac",
|
2012-10-24 16:32:52 -04:00
|
|
|
"content": content,
|
|
|
|
"subject": trac_subject(ticket)
|
|
|
|
})
|
|
|
|
|
2012-10-24 18:19:50 -04:00
|
|
|
class HumbugPlugin(Component):
|
2012-10-24 16:32:52 -04:00
|
|
|
implements(ITicketChangeListener)
|
|
|
|
|
|
|
|
def ticket_created(self, ticket):
|
|
|
|
"""Called when a ticket is created."""
|
2012-10-31 11:49:42 -04:00
|
|
|
content = "%s created %s in component **%s**, priority **%s**:\n" % \
|
|
|
|
(ticket.values.get("reporter"), markdown_ticket_url(ticket),
|
|
|
|
ticket.values.get("component"), ticket.values.get("priority"))
|
2012-10-24 16:32:52 -04:00
|
|
|
if ticket.values.get("description") != "":
|
2012-10-31 11:49:42 -04:00
|
|
|
content += "%s" % markdown_block(ticket.values.get("description"))
|
2012-10-24 16:32:52 -04:00
|
|
|
send_update(ticket, content)
|
|
|
|
|
|
|
|
def ticket_changed(self, ticket, comment, author, old_values):
|
|
|
|
"""Called when a ticket is modified.
|
2012-10-24 18:18:44 -04:00
|
|
|
|
2012-10-24 16:32:52 -04:00
|
|
|
`old_values` is a dictionary containing the previous values of the
|
|
|
|
fields that have changed.
|
|
|
|
"""
|
2012-11-01 18:04:09 -04:00
|
|
|
if not comment and set(old_values.keys()) <= set(["priority", "milestone",
|
|
|
|
"cc", "keywords",
|
|
|
|
"component"]):
|
|
|
|
# This is probably someone going through trac and updating
|
|
|
|
# the priorities; this can result in a lot of messages
|
|
|
|
# nobody wants to read, so don't send them without a comment.
|
|
|
|
return
|
|
|
|
|
2012-10-31 11:49:42 -04:00
|
|
|
content = "%s updated %s" % (author, markdown_ticket_url(ticket))
|
|
|
|
if comment:
|
|
|
|
content += ' with comment: %s\n\n' % (markdown_block(comment,))
|
|
|
|
else:
|
|
|
|
content += ":\n\n"
|
|
|
|
field_changes = []
|
2012-10-24 16:32:52 -04:00
|
|
|
for key in old_values.keys():
|
2012-10-24 18:18:44 -04:00
|
|
|
if key == "description":
|
|
|
|
content += '- Changed %s from %s to %s' % (key, markdown_block(old_values.get(key)),
|
2012-10-24 16:32:52 -04:00
|
|
|
markdown_block(ticket.values.get(key)))
|
2012-10-24 18:18:44 -04:00
|
|
|
elif old_values.get(key) == "":
|
2012-10-31 11:49:42 -04:00
|
|
|
field_changes.append('%s: => **%s**' % (key, ticket.values.get(key)))
|
2012-10-24 18:18:44 -04:00
|
|
|
elif ticket.values.get(key) == "":
|
2012-10-31 11:49:42 -04:00
|
|
|
field_changes.append('%s: **%s** => ""' % (key, old_values.get(key)))
|
2012-10-24 18:18:44 -04:00
|
|
|
else:
|
2012-10-31 11:49:42 -04:00
|
|
|
field_changes.append('%s: **%s** => **%s**' % (key, old_values.get(key),
|
|
|
|
ticket.values.get(key)))
|
|
|
|
content += ", ".join(field_changes)
|
2012-10-24 16:32:52 -04:00
|
|
|
|
|
|
|
send_update(ticket, content)
|
|
|
|
|
|
|
|
def ticket_deleted(self, ticket):
|
|
|
|
"""Called when a ticket is deleted."""
|
|
|
|
content = "%s was deleted." % markdown_ticket_url(ticket, heading="Ticket")
|
|
|
|
send_update(ticket, content)
|