a bit more tidying after refactoring

master
Marek Isalski 7 years ago
parent 27afe2229f
commit e0cddbbd27

@ -1,7 +1,6 @@
from flask import Flask, request from flask import Flask, request
import os import os
import incident
import datetime import datetime
import subprocess import subprocess
import re import re
@ -49,9 +48,7 @@ def is_incident_channel_name( channel_name ):
return channel_name.startswith( app.config[ "MATTERMOST_INCIDENT_CHANNEL_PREFIX" ] ) return channel_name.startswith( app.config[ "MATTERMOST_INCIDENT_CHANNEL_PREFIX" ] )
def mattermost_incident_command(): def mattermost_incident_command( cmd ):
cmd = request.form[ 'text' ].strip().split()[ 0 ].upper()
app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ], app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ],
'message': "### Executing `" + cmd + "`...", 'message': "### Executing `" + cmd + "`...",
} ) } )
@ -118,25 +115,24 @@ def mattermost_incident_command():
return "OK" 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() raw_incident_number = incident.generate_raw_incident_number()
fi_number = raw_incident_number_to_public_incident_number( 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 ) 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, "name": channel_name,
"display_name": fi_number, "display_name": fi_number,
"purpose": purpose, "purpose": description,
"header": "", "header": "",
"type": "O", "type": "O",
} ) } )
# add user to the channel we just created # 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' ], 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" ) 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** 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 ), ## 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, "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, ), '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 request.form[ 'token' ] in app.config[ 'MATTERMOST_COMMAND_TOKENS' ]:
if is_incident_channel_name( request.form[ 'channel_name' ] ): 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: 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: else:
return "Invalid command token." return "Invalid command token."

Loading…
Cancel
Save