mirror of
https://github.com/getredash/redash.git
synced 2025-12-19 17:37:19 -05:00
Merge pull request #1102 from getredash/feature/hipchat_v2
Switch to HipChat V2 API
This commit is contained in:
@@ -43,10 +43,16 @@ if __name__ == '__main__':
|
|||||||
if settings.HIPCHAT_API_TOKEN:
|
if settings.HIPCHAT_API_TOKEN:
|
||||||
# Have all existing alerts send to HipChat if already configured
|
# Have all existing alerts send to HipChat if already configured
|
||||||
schema = get_configuration_schema_for_destination_type('hipchat')
|
schema = get_configuration_schema_for_destination_type('hipchat')
|
||||||
conf = {'token': settings.HIPCHAT_API_TOKEN,
|
|
||||||
'room_id': settings.HIPCHAT_ROOM_ID}
|
conf = {}
|
||||||
|
|
||||||
if settings.HIPCHAT_API_URL:
|
if settings.HIPCHAT_API_URL:
|
||||||
conf['url'] = settings.HIPCHAT_API_URL
|
conf['url'] = '{url}/room/{room_id}/notification?auth_token={token}'.format(
|
||||||
|
url=settings.HIPCHAT_API_URL, room_id=settings.HIPCHAT_ROOM_ID, token=settings.HIPCHAT_API_TOKEN)
|
||||||
|
else:
|
||||||
|
conf['url'] = 'https://hipchat.com/v2/room/{room_id}/notification?auth_token={token}'.format(
|
||||||
|
room_id=settings.HIPCHAT_ROOM_ID, token=settings.HIPCHAT_API_TOKEN)
|
||||||
|
|
||||||
options = ConfigurationContainer(conf, schema)
|
options = ConfigurationContainer(conf, schema)
|
||||||
|
|
||||||
hipchat = NotificationDestination.create(
|
hipchat = NotificationDestination.create(
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import hipchat
|
import requests
|
||||||
|
|
||||||
from redash import settings
|
|
||||||
from redash.destinations import *
|
from redash.destinations import *
|
||||||
|
from redash.models import Alert
|
||||||
|
|
||||||
|
|
||||||
|
colors = {
|
||||||
|
Alert.OK_STATE: 'green',
|
||||||
|
Alert.TRIGGERED_STATE: 'red',
|
||||||
|
Alert.UNKNOWN_STATE: 'yellow'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class HipChat(BaseDestination):
|
class HipChat(BaseDestination):
|
||||||
@@ -12,17 +20,11 @@ class HipChat(BaseDestination):
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"url": {
|
"url": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"title": "HipChat Notification URL (get it from the Integrations page)"
|
||||||
},
|
},
|
||||||
"token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
"room_id": {
|
"required": ["url"]
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["token", "room_id"],
|
|
||||||
"secret": ["token"]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -31,17 +33,25 @@ class HipChat(BaseDestination):
|
|||||||
|
|
||||||
def notify(self, alert, query, user, new_state, app, host, options):
|
def notify(self, alert, query, user, new_state, app, host, options):
|
||||||
try:
|
try:
|
||||||
if options.url:
|
alert_url = '{host}/alerts/{alert_id}'.format(host=host, alert_id=alert.id);
|
||||||
hipchat_client = hipchat.HipChat(token=options.token, url=options.url)
|
query_url = '{host}/queries/{query_id}'.format(host=host, query_id=query.id);
|
||||||
else:
|
|
||||||
hipchat_client = hipchat.HipChat(token=options.token)
|
message = '<a href="{alert_url}">{alert_name}</a> changed state to {new_state} (based on <a href="{query_url}">this query</a>).'.format(
|
||||||
html = """
|
alert_name=alert.name, new_state=new_state.upper(),
|
||||||
Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
|
alert_url=alert_url,
|
||||||
""".format(host=host, alert_id=alert.id, query_id=query.id)
|
query_url=query_url)
|
||||||
message = '[' + new_state.upper() + '] ' + alert.name + '<br />' + html
|
|
||||||
hipchat_client.message_room(options.room_id, settings.NAME, message.encode('utf-8', 'ignore'), message_format='html')
|
data = {
|
||||||
|
'message': message,
|
||||||
|
'color': colors.get(new_state, 'green')
|
||||||
|
}
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
response = requests.post(options['url'], data=json.dumps(data), headers=headers)
|
||||||
|
|
||||||
|
if response.status_code != 204:
|
||||||
|
logging.error('Bad status code received from HipChat: %d', response.status_code)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception("hipchat send ERROR.")
|
logging.exception("HipChat Send ERROR.")
|
||||||
|
|
||||||
|
|
||||||
register(HipChat)
|
register(HipChat)
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ pycrypto==2.6.1
|
|||||||
funcy==1.5
|
funcy==1.5
|
||||||
raven==5.9.2
|
raven==5.9.2
|
||||||
semver==2.2.1
|
semver==2.2.1
|
||||||
python-simple-hipchat==0.4.0
|
|
||||||
xlsxwriter==0.8.4
|
xlsxwriter==0.8.4
|
||||||
pystache==0.5.4
|
pystache==0.5.4
|
||||||
parsedatetime==2.1
|
parsedatetime==2.1
|
||||||
|
|||||||
Reference in New Issue
Block a user