import boto3 from base64 import b64decode from urlparse import parse_qs import logging import json import urllib2 from urllib2 import Request, urlopen, URLError, HTTPError, build_opener ENCRYPTED_EXPECTED_TOKEN = "" # Enter the base-64 encoded, encrypted Slack command token (CiphertextBlob) GITHUB_TOKEN = "token " GITHUB_OWNER = "smyleeface" kms = boto3.client('kms') expected_token = kms.decrypt(CiphertextBlob = b64decode(ENCRYPTED_EXPECTED_TOKEN))['Plaintext'] logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): #get the body content req_body = event['body'] #split body content params = parse_qs(req_body) #validate the token request token = params['token'][0] if token != expected_token: logger.error("Request token (%s) does not match exptected", token) raise Exception("Invalid request token") #get the values from the slack payload user = params['user_name'][0] command = params['command'][0] channel = params['channel_name'][0] command_text = params['text'][0] #get the values from the command split_text = command_text.split() command = split_text[0] repo = split_text[1] pullnumber = split_text[2] #make the header info header = { "Authorization" : GITHUB_TOKEN, "Content-Type" : "application/json" } valid_command = False #make the variables for the merge command if (command == "merge"): github_api_url = "https://api.github.com/repos/%s/%s/pulls/%s/merge"%(GITHUB_OWNER,repo,pullnumber) data = {} req = urllib2.Request(github_api_url, json.dumps(data), header) req.get_method = lambda: 'PUT' valid_command = True #make the variables for the open or close command elif (command == "close" or command == "open"): github_api_url = "https://api.github.com/repos/%s/%s/pulls/%s"%(GITHUB_OWNER,repo,pullnumber) data = { "state" : command } req = urllib2.Request(github_api_url, json.dumps(data), header) req.get_method = lambda: 'PATCH' valid_command = True #run the url if it's a valid command if (valid_command): try: response = urlopen(req) return "%s invoked %s from slack channel %s with the following text: %s" % (user, command, channel, command_text) except HTTPError as e: print e return e else: return "%s is not a valid command"%(command)