Skip to content

Commit

Permalink
Allow filtering notifications based on ticket state changes
Browse files Browse the repository at this point in the history
This implements the a new setting which allows specifying a list of ticket
events which cause notifications to be sent:

  [webhook]
  ...
  notify = created,closed

When this setting is empty, all state changes are notified.

This implements part of the changes suggested in issue #2.
  • Loading branch information
aperezdc committed Sep 6, 2016
1 parent 1a90e30 commit d25f1b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -28,10 +28,13 @@ url = https://host/webhook/path
mucs = team@conference.domain.com,devel@conference.domain.com
jids = bob@domain.com
fields = type,component,resolution
notify = created,changed,closed
```

Some notes on the configuration:

* The list of ticket actions in `notify` can be empty. In that case, all
actions are notified.
* Multiple comma-separated JIDs can be specified both for `mucs` (chat rooms)
and `jids` (individuals).
* The `secret` must be a random string which is configured also in the
Expand Down
16 changes: 16 additions & 0 deletions webhook_notification/__init__.py
Expand Up @@ -31,6 +31,10 @@ def prepare_ticket_values(ticket, action=None):
values['changes'] = ''
return values

def split_option(value, separator=","):
return map(lambda s: s.strip(), value.split(separator))


class WebhookNotificationPlugin(Component):
implements(ITicketChangeListener)
urls = Option('webhook', 'url', '', doc="Incoming webhook")
Expand All @@ -39,6 +43,8 @@ class WebhookNotificationPlugin(Component):
mucs = Option("webhook", "mucs", "", doc="List of MUC rooms to notify")
jids = Option("webhook", "jids", "", doc="List of JIDs to notify")
secret = Option("webhook", "secret", "", doc="Secret used for signing requests")
notify_events = Option("webhook", "notify", "created,closed,changed",
doc="List of ticket events to notify")

def notify(self, type, values):
values['author'] = re.sub(r' <.*', '', values['author'])
Expand Down Expand Up @@ -96,6 +102,8 @@ def notify(self, type, values):
return True

def ticket_created(self, ticket):
if not self.should_notify_event("created"):
return
values = prepare_ticket_values(ticket, 'created')
values['author'] = values['reporter']
values['comment'] = u""
Expand All @@ -113,6 +121,10 @@ def ticket_changed(self, ticket, comment, author, old_values):
if 'status' in ticket.values:
if ticket.values['status'] != old_values['status']:
action = ticket.values['status']

if not self.should_notify_event(action):
return

values = prepare_ticket_values(ticket, action)
values.update({
'comment': comment or '',
Expand Down Expand Up @@ -147,6 +159,10 @@ def ticket_changed(self, ticket, comment, author, old_values):
def ticket_deleted(self, ticket):
pass

def should_notify_event(self, event_name):
enabled_events = set(split_option(self.notify_events))
return len(enabled_events) == 0 or event_name in enabled_events

#def wiki_page_added(self, page):
#def wiki_page_changed(self, page, version, t, comment, author, ipnr):
#def wiki_page_deleted(self, page):
Expand Down

0 comments on commit d25f1b8

Please sign in to comment.