2013-08-07 12:01:49 -04:00
|
|
|
# Zulip trac plugin -- sends zulips when tickets change.
|
2013-02-06 11:14:00 -05:00
|
|
|
#
|
2013-08-07 11:55:22 -04:00
|
|
|
# Install by copying this file and zulip_trac_config.py to the trac
|
2013-02-13 13:47:23 -05:00
|
|
|
# plugins/ subdirectory, customizing the constants in
|
2013-08-07 12:01:49 -04:00
|
|
|
# zulip_trac_config.py, and then adding "zulip_trac" to the
|
2013-02-13 13:47:23 -05:00
|
|
|
# components section of the conf/trac.ini file, like so:
|
2013-02-06 11:14:00 -05:00
|
|
|
#
|
|
|
|
# [components]
|
2013-08-07 12:01:49 -04:00
|
|
|
# zulip_trac = enabled
|
2013-02-06 11:14:00 -05: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.
|
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
|
2013-02-06 11:14:00 -05:00
|
|
|
from trac.core import Component, implements
|
|
|
|
from trac.ticket import ITicketChangeListener
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2013-02-13 17:48:06 -05:00
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
2013-08-07 11:55:22 -04:00
|
|
|
import zulip_trac_config as config
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2013-12-05 17:42:33 -05:00
|
|
|
VERSION = "0.9"
|
2013-02-06 11:14:00 -05:00
|
|
|
|
2020-04-18 18:49:45 -04:00
|
|
|
from typing import Any, Dict
|
2016-12-29 13:34:15 -05:00
|
|
|
|
2013-08-07 12:01:49 -04:00
|
|
|
if config.ZULIP_API_PATH is not None:
|
|
|
|
sys.path.append(config.ZULIP_API_PATH)
|
2013-02-06 16:44:03 -05:00
|
|
|
|
2013-08-07 11:51:03 -04:00
|
|
|
import zulip
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2013-08-07 11:51:03 -04:00
|
|
|
client = zulip.Client(
|
2013-08-07 12:01:49 -04:00
|
|
|
email=config.ZULIP_USER,
|
|
|
|
site=config.ZULIP_SITE,
|
2013-12-05 17:42:33 -05:00
|
|
|
api_key=config.ZULIP_API_KEY,
|
2021-05-28 05:03:46 -04:00
|
|
|
client="ZulipTrac/" + VERSION,
|
|
|
|
)
|
|
|
|
|
2013-02-06 11:14:00 -05:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def markdown_ticket_url(ticket: Any, heading: str = "ticket") -> str:
|
2013-02-13 13:47:23 -05:00
|
|
|
return "[%s #%s](%s/%s)" % (heading, ticket.id, config.TRAC_BASE_TICKET_URL, ticket.id)
|
2013-02-06 11:14:00 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def markdown_block(desc: str) -> str:
|
2013-02-06 11:14:00 -05:00
|
|
|
return "\n\n>" + "\n> ".join(desc.split("\n")) + "\n"
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def truncate(string: str, length: int) -> str:
|
2013-02-06 11:14:00 -05:00
|
|
|
if len(string) <= length:
|
|
|
|
return string
|
2021-05-28 05:03:46 -04:00
|
|
|
return string[: length - 3] + "..."
|
|
|
|
|
2013-02-06 11:14:00 -05:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def trac_subject(ticket: Any) -> str:
|
2013-02-06 11:14:00 -05:00
|
|
|
return truncate("#%s: %s" % (ticket.id, ticket.values.get("summary")), 60)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def send_update(ticket: Any, content: str) -> None:
|
2021-05-28 05:03:46 -04:00
|
|
|
client.send_message(
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": config.STREAM_FOR_NOTIFICATIONS,
|
|
|
|
"content": content,
|
|
|
|
"subject": trac_subject(ticket),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-02-06 11:14:00 -05:00
|
|
|
|
2013-08-07 12:01:49 -04:00
|
|
|
class ZulipPlugin(Component):
|
2013-02-06 11:14:00 -05:00
|
|
|
implements(ITicketChangeListener)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def ticket_created(self, ticket: Any) -> None:
|
2013-02-06 11:14:00 -05:00
|
|
|
"""Called when a ticket is created."""
|
2021-05-28 05:03:46 -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"),
|
|
|
|
)
|
2013-10-01 14:30:49 -04:00
|
|
|
# Include the full subject if it will be truncated
|
|
|
|
if len(ticket.values.get("summary")) > 60:
|
|
|
|
content += "**%s**\n" % (ticket.values.get("summary"),)
|
2013-02-06 11:14:00 -05:00
|
|
|
if ticket.values.get("description") != "":
|
2013-10-01 14:30:49 -04:00
|
|
|
content += "%s" % (markdown_block(ticket.values.get("description")),)
|
2013-02-06 11:14:00 -05:00
|
|
|
send_update(ticket, content)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
def ticket_changed(
|
|
|
|
self, ticket: Any, comment: str, author: str, old_values: Dict[str, Any]
|
|
|
|
) -> None:
|
2013-02-06 11:14:00 -05:00
|
|
|
"""Called when a ticket is modified.
|
|
|
|
|
|
|
|
`old_values` is a dictionary containing the previous values of the
|
|
|
|
fields that have changed.
|
|
|
|
"""
|
2020-04-18 20:33:01 -04:00
|
|
|
if not (
|
|
|
|
set(old_values.keys()).intersection(set(config.TRAC_NOTIFY_FIELDS))
|
|
|
|
or (comment and "comment" in set(config.TRAC_NOTIFY_FIELDS))
|
|
|
|
):
|
2013-02-06 11:14:00 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
content = "%s updated %s" % (author, markdown_ticket_url(ticket))
|
|
|
|
if comment:
|
2021-05-28 05:05:11 -04:00
|
|
|
content += " with comment: %s\n\n" % (markdown_block(comment),)
|
2013-02-06 11:14:00 -05:00
|
|
|
else:
|
|
|
|
content += ":\n\n"
|
|
|
|
field_changes = []
|
2017-12-23 01:17:13 -05:00
|
|
|
for key, value in old_values.items():
|
2013-02-06 11:14:00 -05:00
|
|
|
if key == "description":
|
2021-05-28 05:05:11 -04:00
|
|
|
content += "- Changed %s from %s\n\nto %s" % (
|
2021-05-28 05:03:46 -04:00
|
|
|
key,
|
|
|
|
markdown_block(value),
|
|
|
|
markdown_block(ticket.values.get(key)),
|
|
|
|
)
|
2013-02-06 11:14:00 -05:00
|
|
|
elif old_values.get(key) == "":
|
2021-05-28 05:05:11 -04:00
|
|
|
field_changes.append("%s: => **%s**" % (key, ticket.values.get(key)))
|
2013-02-06 11:14:00 -05:00
|
|
|
elif ticket.values.get(key) == "":
|
|
|
|
field_changes.append('%s: **%s** => ""' % (key, old_values.get(key)))
|
|
|
|
else:
|
2021-05-28 05:03:46 -04:00
|
|
|
field_changes.append(
|
2021-05-28 05:05:11 -04:00
|
|
|
"%s: **%s** => **%s**" % (key, old_values.get(key), ticket.values.get(key))
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2013-02-06 11:14:00 -05:00
|
|
|
content += ", ".join(field_changes)
|
|
|
|
|
|
|
|
send_update(ticket, content)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def ticket_deleted(self, ticket: Any) -> None:
|
2013-02-06 11:14:00 -05:00
|
|
|
"""Called when a ticket is deleted."""
|
2021-03-04 18:02:39 -05:00
|
|
|
content = "%s was deleted." % (markdown_ticket_url(ticket, heading="Ticket"),)
|
2013-02-06 11:14:00 -05:00
|
|
|
send_update(ticket, content)
|