Added sub-path support to MatterMost Notifications

pull/123/head
Chris Caron 6 years ago committed by GitHub
parent f373e41a91
commit 3b28823b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

Loading…
Cancel
Save