From e0cddbbd27ea1c3e1dffe16a088c3398197ce7a1 Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sat, 16 Jun 2018 14:18:22 +0200 Subject: [PATCH] a bit more tidying after refactoring --- fih.py | 30 ++++++++++++++++-------------- incident.py | 0 2 files changed, 16 insertions(+), 14 deletions(-) delete mode 100644 incident.py diff --git a/fih.py b/fih.py index 5c86398..301995b 100644 --- a/fih.py +++ b/fih.py @@ -1,7 +1,6 @@ from flask import Flask, request import os -import incident import datetime import subprocess import re @@ -49,9 +48,7 @@ def is_incident_channel_name( channel_name ): return channel_name.startswith( app.config[ "MATTERMOST_INCIDENT_CHANNEL_PREFIX" ] ) -def mattermost_incident_command(): - cmd = request.form[ 'text' ].strip().split()[ 0 ].upper() - +def mattermost_incident_command( cmd ): app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ], 'message': "### Executing `" + cmd + "`...", } ) @@ -118,25 +115,24 @@ def mattermost_incident_command(): return "OK" -def mattermost_incident_start(): +def mattermost_incident_start( description, team_id, user_name, user_id, channel_id ): raw_incident_number = incident.generate_raw_incident_number() fi_number = raw_incident_number_to_public_incident_number( raw_incident_number ) channel_name = raw_incident_number_to_channel_name( raw_incident_number ) - purpose = request.form[ 'text' ] - channel = app.mm.channels.create_channel( options = { "team_id": request.form[ 'team_id' ], + channel = app.mm.channels.create_channel( options = { "team_id": team_id, "name": channel_name, "display_name": fi_number, - "purpose": purpose, + "purpose": description, "header": "", "type": "O", } ) # add user to the channel we just created - app.mm.client.make_request( 'post', '/channels/' + channel[ 'id' ] + '/members', options = { 'user_id': request.form[ 'user_id' ], + app.mm.client.make_request( 'post', '/channels/' + channel[ 'id' ] + '/members', options = { 'user_id': user_id, } ) app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], - 'message': "Incident discussion channel for [%s](%s) created by @%s." % ( fi_number, raw_incident_number_to_url( incident_number ), request.form[ 'user_name' ] ), + 'message': "Incident discussion channel for [%s](%s) created by @%s." % ( fi_number, raw_incident_number_to_url( incident_number ), user_name ), } ) now = datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M" ) @@ -175,12 +171,12 @@ Again, don't forget to **/incident PUBLISH** once making changes. Again, after setting the headline status you must: **/incident PUBLISH** ## Good luck, Incident Commander @%(commander_name)s!""" % { "incident_url": raw_incident_number_to_url( incident_number ), - "commander_name": request.form[ 'user_name' ], + "commander_name": user_name, "now": now, }, } ) - app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ], + app.mm.posts.create_post( options = { 'channel_id': channel_id, 'message': 'On behalf of @%s I have started incident ~%s.' % ( request.form[ 'user_name' ], channel_name, ), } ) @@ -200,9 +196,15 @@ def mattermost_incident(): if request.form[ 'token' ] in app.config[ 'MATTERMOST_COMMAND_TOKENS' ]: if is_incident_channel_name( request.form[ 'channel_name' ] ): - return mattermost_incident_command() + cmd = request.form[ 'text' ].strip().split()[ 0 ].upper() + return mattermost_incident_command( cmd ) else: - return mattermost_incident_start() + return mattermost_incident_start( description = request.form[ 'text' ], + team_id = request.form[ 'team_id' ], + user_name = request.form[ 'user_name' ], + user_id = request.form[ 'user_id' ], + channel_id = request.form[ 'channel_id' ], + ) else: return "Invalid command token." diff --git a/incident.py b/incident.py deleted file mode 100644 index e69de29..0000000