Skip to content

Instantly share code, notes, and snippets.

@joshwatson
Last active April 16, 2016 23:50
Show Gist options
  • Save joshwatson/1fa18a7f4887f5cd7bb8bd42c251852b to your computer and use it in GitHub Desktop.
Save joshwatson/1fa18a7f4887f5cd7bb8bd42c251852b to your computer and use it in GitHub Desktop.
Simple Slack slash command that generates jira ticket URLs from a ticket number
# Copyright (c) 2016 Josh Watson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import re
import urlparse
JIRA_URL = 'https://my.jira.project/browse/{}-{}'
DEFAULT_PROJECT = 'MYPROJECT'
TOKEN = 'eajZdcZv4yhm6mCKMemDQKfk'
def jirabot(event, context):
'''Returns a fully-formed JIRA url for a given ticket number.
Args:
event: A dictionary containing a single key (`formparams`).
`formparams` is an application/x-www-form-urlencoded string
representing the fields sent by the outgoing web hook from the
slack channel. The 'text' key will have the value of the /jira
command. The command fits the following regex:
/jira ((?P<project>[a-zA-Z][_a-zA-Z0-9]+)[ -])?(?P<ticket>\d+)
If `project` isn't specified, the default project will be used. The
project name is case insensitive, and will be converted to
uppercase to meet the key requirements of JIRA.
context: AWS Lambda uses this parameter to provide runtime information
to the handler. This parameter is of the LambdaContext type. It's
not actually used by jirabot.
Returns:
A dictionary of the form:
{
'response_type': 'in_channel',
'text': <jira_ticket_url_or_error>
}
Raises:
None
'''
# Create a default response. 'text' will be updated after the query
# is parsed.
jira_response = {
'response_type': 'in_channel',
'text': "I couldn't parse your request."
}
# turn the parameters sent from slack into a dictionary
try:
form_params = urlparse.parse_qs(event.get('formparams'))
except:
return jira_response
if form_params is None:
return jira_response
# return empty if the token doesn't match
try:
token = form_params.get('token')[0]
except TypeError:
token = ''
if token != TOKEN:
return {'text': 'Invalid token.'}
# get the ticket request
try:
ticket_request = form_params.get('text')[0]
except TypeError:
ticket_request = None
if ticket_request is None:
return jira_response
ticket_match = re.match(
r'((?P<project>[a-zA-Z][_a-zA-Z0-9]+)[ -])?(?P<ticket>\d+)',
ticket_request
)
if ticket_match is None:
return jira_response
# extract the project and ticket number from the query
project = ticket_match.group('project')
if project is None:
project = DEFAULT_PROJECT
ticket = ticket_match.group('ticket')
# format the URL response
jira_response['text'] = JIRA_URL.format(project.upper(), ticket)
return jira_response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment