<commit_before>#!/usr/bin/python
from __future__ import print_function
from cli.utils import Utils
import os
import contextlib
import sys
from datetime import datetime
from cli.webhook import WebHook


@contextlib.contextmanager
def chdir(dirname):
    '''Withable chdir function that restores directory'''
    curdir = os.getcwd()
    try:
        os.chdir(dirname)
        yield
    finally:
        os.chdir(curdir)


class Hooks:

    def __init__(self):
        self.utils = Utils()
        self.whobj = WebHook()

    def run_hook(self, hookname, appdata, path, hook_input_metric):
        try:
            exit_code = 0
            function_execution_start_time = datetime.now()
            execution_result = 'SUCCESS'
            envr = 'NA'
            print (appdata)
            print (appdata.keys())
            #import pdb; pdb.set_trace()
            temp = hook_input_metric.split(',')
            action, app_name, config_name, envr, user = temp[0], temp[1], temp[3], temp[4], temp[5]
            # expecting 'roger-tools.pre_push_time' extraction of pre_push as string from here
            action = ''.join(action.split('.')[1])
            action = '-'.join(action.split('_')[0:2])
            if ('post' in action):
                slackMessage = "Completed *" +action.split('-')[1] +"* of *" +app_name.split('=')[1]+ "* on *"+envr.split('=')[1]+"* (triggered by *"+ user.split('=')[1] +"*)"
                #slackMessage = user.split('=')[1]+' is Performing action: '+ action + ' on app: ' + app_name + ' in environment: '+ envr.split('=')[1]
                self.whobj.api_call(slackMessage,'#testhook')
            abs_path = os.path.abspath(path)
            if "hooks" in appdata and hookname in appdata["hooks"]:
                command = appdata["hooks"][hookname]
                with chdir(abs_path):
                    print("About to run {} hook [{}] at path {}".format(
                        hookname, command, abs_path))
                    exit_code = os.system(command)
        except (Exception) as e:
            print("The following error occurred: %s" %
                  e, file=sys.stderr)
            execution_result = 'FAILURE'
            raise
        finally:
            try:
                if 'execution_result' not in globals() or 'execution_result' not in locals():
                    execution_result = 'FAILURE'
                if 'function_execution_start_time' not in globals() or 'function_execution_start_time' not in locals():<commit_msg>added changes for slack functionality implementation<commit_after>#!/usr/bin/python
from __future__ import print_function
import os
import contextlib
import sys
from sets import Set
from datetime import datetime
from cli.webhook import WebHook
from cli.utils import Utils


@contextlib.contextmanager
def chdir(dirname):
    '''Withable chdir function that restores directory'''
    curdir = os.getcwd()
    try:
        os.chdir(dirname)
        yield
    finally:
        os.chdir(curdir)


class Hooks:

    def __init__(self):
        self.utils = Utils()
        self.whobj = WebHook()

    def run_hook(self, hookname, appdata, path, hook_input_metric):
        try:
            exit_code = 0
            function_execution_start_time = datetime.now()

            execution_result = 'SUCCESS'
            envr = 'NA'
            print (appdata)
            temp = hook_input_metric.split(',')
            action, app_name, config_name, envr, user = temp[0], temp[1], temp[3], temp[4], temp[5]
            action = ''.join(action.split('.')[1])
            action = '-'.join(action.split('_')[0:2])


            try:
                channelsSet = Set(appdata['notifications']['channels'])
                envSet = Set(appdata['notifications']['envs'])
                commandsSet = Set(appdata['notifications']['commands'])
            except:
                self.whobj.api_call('setting default message','#testhook')
                channelsSet = ['rogerdefault']
                envSet = [envr]
                commandsSet = action
            #import pdb; pdb.set_trace()

            # expecting 'roger-tools.pre_push_time' extraction of pre_push as string from here
            print (action + ":" + ','.join(commandsSet))
            print (envr + ":" + ','.join(envSet))
            #import pdb; pdb.set_trace()
            if ('post' in action and envr.split('=')[1] in envSet and action.split('-')[1] in commandsSet):
                for channel in channelsSet:
                    print ('Present Sir!')
                    slackMessage = "Completed *" +action.split('-')[1] +"* of *" +app_name.split('=')[1]+ "* on *"+envr.split('=')[1]+"* (triggered by *"+ user.split('=')[1] +"*)"
                    self.whobj.api_call(slackMessage,'#'+channel)
            abs_path = os.path.abspath(path)
            if "hooks" in appdata and hookname in appdata["hooks"]:
                command = appdata["hooks"][hookname]
                with chdir(abs_path):
                    print("About to run {} hook [{}] at path {}".format(
                        hookname, command, abs_path))
                    exit_code = os.system(command)
        except (Exception) as e:
            print("The following error occurred : %s" %
                  e, file=sys.stderr)
            execution_result = 'FAILURE'
            self.whobj.api_call("The following error occurred : %s" %e ,'#testhook')
            raise
        finally:
            try:
                if 'execution_result' not in globals() or 'execution_result' not in locals():
                    execution_result = 'FAILURE'
                if 'function_execution_start_time' not in globals() or 'function_execution_start_time' not in locals():
{'fraction_non_alphanumeric': 0.12814685314685315, 'fraction_numerical': 0.005244755244755245, 'mean_word_length': 2.099133261105092, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '595166', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1638, 'n_words': 412}