Added sub-path support to MatterMost Notifications

pull/123/head
Chris Caron 2019-06-06 12:40:14 -04:00 committed by GitHub
parent f373e41a91
commit 3b28823b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 20 deletions

View File

@ -24,6 +24,7 @@
# THE SOFTWARE. # THE SOFTWARE.
import re import re
import six
import requests import requests
from json import dumps from json import dumps
@ -79,7 +80,11 @@ class NotifyMatterMost(NotifyBase):
'{schema}://{host}/{authtoken}', '{schema}://{host}/{authtoken}',
'{schema}://{host}/{authtoken}:{port}', '{schema}://{host}/{authtoken}:{port}',
'{schema}://{botname}@{host}/{authtoken}', '{schema}://{botname}@{host}/{authtoken}',
'{schema}://{botname}@{host}/{authtoken}:{port}', '{schema}://{botname}@{host}:{port}/{authtoken}',
'{schema}://{host}/{fullpath}/{authtoken}',
'{schema}://{host}/{fullpath}{authtoken}:{port}',
'{schema}://{botname}@{host}/{fullpath}/{authtoken}',
'{schema}://{botname}@{host}:{port}/{fullpath}/{authtoken}',
) )
# Define our template tokens # Define our template tokens
@ -96,6 +101,10 @@ class NotifyMatterMost(NotifyBase):
'private': True, 'private': True,
'required': True, 'required': True,
}, },
'fullpath': {
'name': _('Path'),
'type': 'string',
},
'botname': { 'botname': {
'name': _('Bot Name'), 'name': _('Bot Name'),
'type': 'string', 'type': 'string',
@ -126,8 +135,8 @@ class NotifyMatterMost(NotifyBase):
}, },
}) })
def __init__(self, authtoken, channels=None, include_image=False, def __init__(self, authtoken, fullpath=None, channels=None,
**kwargs): include_image=False, **kwargs):
""" """
Initialize MatterMost Object Initialize MatterMost Object
""" """
@ -139,6 +148,10 @@ class NotifyMatterMost(NotifyBase):
else: else:
self.schema = 'http' self.schema = 'http'
# our full path
self.fullpath = '' if not isinstance(
fullpath, six.string_types) else fullpath.strip()
# Our Authorization Token # Our Authorization Token
self.authtoken = authtoken self.authtoken = authtoken
@ -204,8 +217,9 @@ class NotifyMatterMost(NotifyBase):
if channel: if channel:
payload['channel'] = channel payload['channel'] = channel
url = '%s://%s:%d' % (self.schema, self.host, self.port) url = '{}://{}:{}{}/hooks/{}'.format(
url += '/hooks/%s' % self.authtoken self.schema, self.host, self.port, self.fullpath,
self.authtoken)
self.logger.debug('MatterMost POST URL: %s (cert_verify=%r)' % ( self.logger.debug('MatterMost POST URL: %s (cert_verify=%r)' % (
url, self.verify_certificate, url, self.verify_certificate,
@ -288,14 +302,17 @@ class NotifyMatterMost(NotifyBase):
default_port = 443 if self.secure else self.default_port default_port = 443 if self.secure else self.default_port
default_schema = self.secure_protocol if self.secure else self.protocol default_schema = self.secure_protocol if self.secure else self.protocol
return '{schema}://{hostname}{port}/{authtoken}/?{args}'.format( return \
schema=default_schema, '{schema}://{hostname}{port}{fullpath}{authtoken}/?{args}'.format(
hostname=NotifyMatterMost.quote(self.host, safe=''), schema=default_schema,
port='' if not self.port or self.port == default_port hostname=NotifyMatterMost.quote(self.host, safe=''),
else ':{}'.format(self.port), port='' if not self.port or self.port == default_port
authtoken=NotifyMatterMost.quote(self.authtoken, safe=''), else ':{}'.format(self.port),
args=NotifyMatterMost.urlencode(args), fullpath='/' if not self.fullpath else '{}/'.format(
) NotifyMatterMost.quote(self.fullpath, safe='/')),
authtoken=NotifyMatterMost.quote(self.authtoken, safe=''),
args=NotifyMatterMost.urlencode(args),
)
@staticmethod @staticmethod
def parse_url(url): def parse_url(url):
@ -310,14 +327,16 @@ class NotifyMatterMost(NotifyBase):
# We're done early as we couldn't load the results # We're done early as we couldn't load the results
return results return results
try: # Acquire our tokens; the last one will always be our authtoken
# Apply our settings now # all entries before it will be our path
results['authtoken'] = \ tokens = NotifyMatterMost.split_path(results['fullpath'])
NotifyMatterMost.split_path(results['fullpath'])[0]
except IndexError: # Apply our settings now
# There was no Authorization Token specified results['authtoken'] = None if not tokens else tokens.pop()
results['authtoken'] = None
# Store our path
results['fullpath'] = '' if not tokens \
else '/{}'.format('/'.join(tokens))
# Define our optional list of channels to notify # Define our optional list of channels to notify
results['channels'] = list() results['channels'] = list()

View File

@ -999,6 +999,13 @@ TEST_URLS = (
('mmosts://localhost/3ccdd113474722377935511fc85d3dd4', { ('mmosts://localhost/3ccdd113474722377935511fc85d3dd4', {
'instance': plugins.NotifyMatterMost, 'instance': plugins.NotifyMatterMost,
}), }),
# Test our paths
('mmosts://localhost/a/path/3ccdd113474722377935511fc85d3dd4', {
'instance': plugins.NotifyMatterMost,
}),
('mmosts://localhost/////3ccdd113474722377935511fc85d3dd4///', {
'instance': plugins.NotifyMatterMost,
}),
('mmosts://localhost', { ('mmosts://localhost', {
# Thrown because there was no webhook id specified # Thrown because there was no webhook id specified
'instance': TypeError, 'instance': TypeError,