Example #1
0
 def help(self, prefix=""):
     help_text = ""
     for command in self.available_methods():
         help_text += " • {0}\n".format(command)
     attachments = API_Slack_Attachment(help_text, 'good')
     text = prefix + "*Here are the `{0}` commands available:*".format(
         self.target.__name__)
     return text, attachments.render()
Example #2
0
    def test__create_button_to_test_dialog(self):
        self.test_lambda_update()
        self.api_attach = API_Slack_Attachment()
        self.api_attach.set_text       ('Click on button below to test dialog'          ) \
            .set_callback_id("gs_detect_slack"                            ) \
            .add_button     ("button 1", "gs detect test", "open 1", "primary"   )
        attachments = self.api_attach.render()

        slack_message("one message", attachments, 'DJYDAS51B')
Example #3
0
    def show_buttons_with_examples(self):

        attachments = API_Slack_Attachment()                                                                 \
                        .set_callback_id("jira-dialog-action"                                             )  \
                        .set_text       ('What would you like to do?'                                     )  \
                        .add_button     ("graph-type" , "Search for issues"   , "search-for-issues"       )  \
                        .add_button     ("graph-type" , "Fact-47 story"      , "show-fact-47-story"     )
                        #.add_button     ("graph-type" , "GS OKR example"      , "show-gs-okr-example"     )

        return {"text": "" , "attachments": attachments.render()}
Example #4
0
    def cmd_help(self):
        commands = [
            func for func in dir(GS_Bot_Jira)
            if callable(getattr(GS_Bot_Jira, func)) and func.startswith("cmd")
        ]

        help_text = ""
        for command in commands:
            help_text += " • {0}\n".format(command.replace('cmd_', ''))
        attachments = API_Slack_Attachment(help_text, 'good').render()
        text = "*Here are the `jira` commands available:*"
        return {"text": text, "attachments": attachments}
    def help(team_id, channel, params, data):
        commands = [
            func for func in dir(Lambda_Graph_Commands)
            if callable(getattr(Lambda_Graph_Commands, func))
            and not func.startswith("__")
        ]

        help_text = ""
        for command in commands:
            help_text += " • {0}\n".format(command)
        attachments = API_Slack_Attachment(help_text, 'good').render()
        text = "*Here are the `graph` commands available:*"
        if channel:
            slack_message(text, attachments, channel, team_id)
        else:
            return text, attachments
    def last(team_id, channel, params, data=None):
        n = 10
        if len(params) == 1:
            n = int(params.pop())

        graphs = Lambda_Graph().get_last_n_graphs_of_type('lambda_graph', n)
        row_separator = '|{0}|\n'.format("-" * 72)
        row_format = '| {0:2} | {1:9} | {2:5} | {3:5} | {4:24} | {5:10} |\n'
        graphs_text = '```'
        graphs_text += row_format.format('#', '   who   ', 'nodes', 'edges',
                                         '            type', ' name')
        graphs_text += row_separator
        count = 1
        for graph in graphs:
            graph_value = graph.get('value')
            graph_name = graph_value.get('doc_data').get('name')
            graph_type = graph_value.get('doc_data').get('type')
            extra_data = graph_value.get('doc_data').get('extra_data')
            user = extra_data.get('user')
            nodes = extra_data.get('stats').get('count_nodes')
            edges = extra_data.get('stats').get('count_edges')
            if user is None: user = "******"
            if graph_type is None: graph_type = "...."
            if graph_name is None: graph_name = ""
            graphs_text += row_format.format(count, user, nodes, edges,
                                             graph_type[0:24],
                                             graph_name[0:10])
            count += 1
        graphs_text += '```'
        attachments = API_Slack_Attachment(graphs_text, 'good').render()
        if channel:
            slack_message(
                '*Here are the last {0} graphs generated* (use `graph last n` to see more results)'
                .format(n), attachments, channel, team_id)
        else:
            return attachments
Example #7
0
class test_lambda_gsbot_gs_jira(Test_Helper):
    def setUp(self):
        super().setUp()
        self.aws_lambda = Lambda('osbot_jira.lambdas.slack_actions')

    def tearDown(self):
        if self.result is not None:
            Dev.pprint(self.result)

    def test_lambda_update(self):
        Deploy().deploy_lambda__jira('osbot_jira.lambdas.slack_actions')

    def test_invoke_directly_with_payload(self):
        payload = {
            'type':
            'block_actions',
            'team': {
                'id': 'TRQU3V52S',
                'domain': 'glasswall-solutions'
            },
            'user': {
                'id': 'UR9UENEAW',
                'username': '******',
                'name': 'dcruz',
                'team_id': 'TRQU3V52S'
            },
            'api_app_id':
            'ARQD319T7',
            'token':
            'grIGwKiaF1AYTrgpfyLYwsI2',
            'container': {
                'type': 'message',
                'message_ts': '1582152217.035800',
                'channel_id': 'DRE51D4EM',
                'is_ephemeral': False
            },
            'trigger_id':
            '947439811026.874955991094.568da2433c5af94ab19c4dbe3c885cdb',
            'channel': {
                'id': 'DRE51D4EM',
                'name': 'directmessage'
            },
            'message': {
                'type':
                'message',
                'subtype':
                'bot_message',
                'text':
                "This+content+can't+be+displayed.",
                'ts':
                '1582152217.035800',
                'username':
                '******',
                'bot_id':
                'BRCUGDR4K',
                'blocks': [{
                    'type': 'section',
                    'block_id': 'block_YT2N',
                    'text': {
                        'type': 'mrkdwn',
                        'text':
                        ':point_right:+*Issue*:+<bcf66c78613e430c99ee866a98d31262.eu-west-1.aws.found.io/browse/TASK-60|TASK-60+-+a+new+test>',
                        'verbatim': False
                    }
                }, {
                    'type': 'section',
                    'block_id': 'block_W0QX',
                    'text': {
                        'type': 'mrkdwn',
                        'text': '*Change+issue+status+to*:+(click+to+change)',
                        'verbatim': False
                    }
                }, {
                    'type':
                    'actions',
                    'block_id':
                    'block_OUEA',
                    'elements': [{
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::transition_to::21',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Ready+For+Review',
                            'emoji': True
                        },
                        'value': 'TASK-60::21::Ready+For+Review'
                    }, {
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::transition_to::111',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Stop+Progress',
                            'emoji': True
                        },
                        'value': 'TASK-60::111::Stop+Progress'
                    }, {
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::transition_to::151',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Done',
                            'emoji': True
                        },
                        'value': 'TASK-60::151::Done'
                    }]
                }, {
                    'type': 'section',
                    'block_id': 'block_GW9G',
                    'text': {
                        'type': 'mrkdwn',
                        'text': '*Edit+Issue+Field:*+(select+to+edit)',
                        'verbatim': False
                    }
                }, {
                    'type':
                    'actions',
                    'block_id':
                    'block_VJOA',
                    'elements': [{
                        'type':
                        'static_select',
                        'action_id':
                        'class_method::Jira_View_Issue::edit_field::TASK-60::field_to_edit',
                        'placeholder': {
                            'type': 'plain_text',
                            'text': 'Field+to+edit',
                            'emoji': True
                        },
                        'options': [{
                            'text': {
                                'type': 'plain_text',
                                'text': 'Assignee',
                                'emoji': True
                            },
                            'value': 'Assignee'
                        }, {
                            'text': {
                                'type': 'plain_text',
                                'text': 'Description',
                                'emoji': True
                            },
                            'value': 'Description'
                        }, {
                            'text': {
                                'type': 'plain_text',
                                'text': 'Labels',
                                'emoji': True
                            },
                            'value': 'Labels'
                        }, {
                            'text': {
                                'type': 'plain_text',
                                'text': 'Latest+Information',
                                'emoji': True
                            },
                            'value': 'Latest+Information'
                        }, {
                            'text': {
                                'type': 'plain_text',
                                'text': 'Summary',
                                'emoji': True
                            },
                            'value': 'Summary'
                        }]
                    }]
                }, {
                    'type': 'section',
                    'block_id': 'block_2B48',
                    'text': {
                        'type': 'mrkdwn',
                        'text': '*Actions*',
                        'verbatim': False
                    }
                }, {
                    'type':
                    'actions',
                    'block_id':
                    'block_C5Q7',
                    'elements': [{
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::view_links',
                        'text': {
                            'type': 'plain_text',
                            'text': 'View+Links',
                            'emoji': True
                        },
                        'value': 'TASK-60'
                    }, {
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::screenshot',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Screenshot',
                            'emoji': True
                        },
                        'value': 'TASK-60'
                    }, {
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::reload_issue',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Reload+Issue',
                            'emoji': True
                        },
                        'value': 'TASK-60'
                    }, {
                        'type': 'button',
                        'action_id':
                        'class_method::Jira_View_Issue::raw_issue_data',
                        'text': {
                            'type': 'plain_text',
                            'text': 'Raw+Issue+Data',
                            'emoji': True
                        },
                        'value': 'TASK-60'
                    }]
                }, {
                    'type': 'divider',
                    'block_id': 'ziFZ'
                }, {
                    'type':
                    'context',
                    'block_id':
                    'block_XKN0',
                    'elements': [{
                        'type': 'mrkdwn',
                        'text': 'Rating:+None',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Priority:+Medium',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Issue+Type:+Task',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Assignee:+None',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Labels:+None',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Creator:+None',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Created:+2020-02-19',
                        'verbatim': False
                    }, {
                        'type': 'mrkdwn',
                        'text': 'Updated:+2020-02-19',
                        'verbatim': False
                    }]
                }]
            },
            'response_url':
            'https://hooks.slack.com/actions/TRQU3V52S/962437095158/MQrgn7uTq1VIKcu4PJtqVEe4',
            'actions': [{
                'type': 'static_select',
                'action_id':
                'class_method::Jira_View_Issue::edit_field::TASK-60::field_to_edit',
                'block_id': 'block_VJOA',
                'selected_option': {
                    'text': {
                        'type': 'plain_text',
                        'text': 'Description',
                        'emoji': True
                    },
                    'value': 'Description'
                },
                'placeholder': {
                    'type': 'plain_text',
                    'text': 'Field+to+edit',
                    'emoji': True
                },
                'action_ts': '1582152400.262464'
            }]
        }
        payload = {
            'type': 'dialog_submission',
            'token': 'grIGwKiaF1AYTrgpfyLYwsI2',
            'action_ts': '1582153511.343061',
            'team': {
                'id': 'TRQU3V52S',
                'domain': 'glasswall-solutions'
            },
            'user': {
                'id': 'UR9UENEAW',
                'name': 'dcruz'
            },
            'channel': {
                'id': 'DRE51D4EM',
                'name': 'directmessage'
            },
            'submission': {
                'TASK-60::Summary': 'a+new+testAAAAAAAAAA'
            },
            'callback_id': 'jira_edit_issue',
            'response_url':
            'https://hooks.slack.com/app/TRQU3V52S/950128493681/PvzySbviprciuHfLJFFZN8jJ',
            'state': ''
        }
        payload = {
            'type':
            'interactive_message',
            'actions': [{
                'name': 'jira-actions',
                'type': 'button',
                'value': 'create_issue'
            }],
            'callback_id':
            'jira-slack-actions',
            'team': {
                'id': 'TRQU3V52S',
                'domain': 'glasswall-solutions'
            },
            'channel': {
                'id': 'DRE51D4EM',
                'name': 'directmessage'
            },
            'user': {
                'id': 'UR9UENEAW',
                'name': 'dcruz'
            },
            'action_ts':
            '1582155752.564879',
            'message_ts':
            '1582155697.059500',
            'attachment_id':
            '1',
            'token':
            'grIGwKiaF1AYTrgpfyLYwsI2',
            'is_app_unfurl':
            False,
            'original_message': {
                'type':
                'message',
                'subtype':
                'bot_message',
                'text':
                ':point_right:+Hello,+here+are+the+currently+supported+Jira+Actions',
                'ts':
                '1582155697.059500',
                'username':
                '******',
                'bot_id':
                'BRCUGDR4K',
                'attachments': [{
                    'callback_id':
                    'jira-slack-actions',
                    'id':
                    1,
                    'color':
                    '3AA3E3',
                    'actions': [{
                        'id': '1',
                        'name': 'jira-actions',
                        'text': 'Create+Issue',
                        'type': 'button',
                        'value': 'create_issue',
                        'style': 'primary'
                    }, {
                        'id': '2',
                        'name': 'jira-actions',
                        'text': 'Search',
                        'type': 'button',
                        'value': 'search',
                        'style': 'primary'
                    }],
                    'fallback':
                    '[no+preview+available]'
                }]
            },
            'response_url':
            'https://hooks.slack.com/actions/TRQU3V52S/950172142897/d5qEaWKzMBewqWy49jgxtHsJ',
            'trigger_id':
            '948828513811.874955991094.118063096d956be1d8f0dbef30b9b046'
        }
        self.result = run(payload)

    #     assert response[0] == '*Here are the `GS_Bot_Jira_Commands` commands available:*'

    def test_invoke(self):
        self.test_lambda_update()
        self.result = self.aws_lambda.invoke({})

    def test__create_button_to_test_dialog(self):
        self.test_lambda_update()
        self.api_attach = API_Slack_Attachment()
        self.api_attach.set_text       ('Click on button below to test dialog'          ) \
            .set_callback_id("gs_detect_slack"                            ) \
            .add_button     ("button 1", "gs detect test", "open 1", "primary"   )
        attachments = self.api_attach.render()

        slack_message("one message", attachments, 'DJYDAS51B')

    def test_invoke__with_test_button_click_event(self):
        self.test_lambda_update()
        payload = {
            'type':
            'interactive_message',
            'actions': [{
                'name': 'button+1',
                'type': 'button',
                'value': 'open+1'
            }],
            'callback_id':
            'button-dialog-test',
            'team': {
                'id': 'T7F3AUXGV',
                'domain': 'gs-cst'
            },
            'channel': {
                'id': 'DDKUZTK6X',
                'name': 'directmessage'
            },
            'user': {
                'id': 'U7ESE1XS7',
                'name': 'dinis.cruz'
            },
            'action_ts':
            '1560853130.127802',
            'message_ts':
            '1560853127.000500',
            'attachment_id':
            '1',
            'token':
            'a2J5DIEEJZtKOSZHZBqgDnVz',
            'is_app_unfurl':
            False,
            'original_message': {
                'type':
                'message',
                'subtype':
                'bot_message',
                'text':
                'one+message',
                'ts':
                '1560853127.000500',
                'username':
                '******',
                'bot_id':
                'BDKLUMX4K',
                'attachments': [{
                    'callback_id':
                    'button-dialog-test',
                    'fallback':
                    'Click+on+button+below+to+test+dialog',
                    'text':
                    'Click+on+button+below+to+test+dialog',
                    'id':
                    1,
                    'color':
                    '3AA3E3',
                    'actions': [{
                        'id': '1',
                        'name': 'button+1',
                        'text': 'click-me-1',
                        'type': 'button',
                        'value': 'open+1',
                        'style': 'primary'
                    }, {
                        'id': '2',
                        'name': 'button+2',
                        'text': 'click-me-2',
                        'type': 'button',
                        'value': 'open+2',
                        'style': 'primary'
                    }]
                }]
            },
            'response_url':
            'https://hooks.slack.com/actions/T7F3AUXGV/668389575413/fa9NGk6KvGPe8d9io9n2eTmc',
            'trigger_id':
            '656981015107.253112983573.a337fcceabba9cd21113700e39b41423'
        }
        self.result = self.aws_lambda.invoke(payload)

    def test_invoke__with_fialog_submission(self):
        self.test_lambda_update()
        payload = {
            'type': 'dialog_submission',
            'token': 'a2J5DIEEJZtKOSZHZBqgDnVz',
            'action_ts': '1560886997.880872',
            'team': {
                'id': 'T7F3AUXGV',
                'domain': 'gs-cst'
            },
            'user': {
                'id': 'U7ESE1XS7',
                'name': 'dinis.cruz'
            },
            'channel': {
                'id': 'DDKUZTK6X',
                'name': 'directmessage'
            },
            'submission': {
                'summary': 'an+summary+1'
            },
            'callback_id': 'issue-suggestion',
            'response_url':
            'https://hooks.slack.com/app/T7F3AUXGV/671229078598/xKLbB0wSefs2ZlVSqrdKftpB',
            'state': ''
        }
        self.result = self.aws_lambda.invoke(payload)

    def test_send_menu(self):
        text = 'an dynamic menu'
        attachments = [{
            "fallback":
            "Upgrade your Slack client to use messages like these.",
            "color":
            "3AA3E3",
            "attachment_type":
            "default",
            "callback_id":
            "select_remote_1234",
            "actions": [{
                "name": "bugs_list",
                "text": "Which random bug do you want to resolve?",
                "type": "select",
                "data_source": "external",
                "min_query_length": 1,
            }]
        }]
        self.test_lambda_update()
        slack_message("one message", attachments, 'DDKUZTK6X')
 def __init__(self):
     self.name = "jira-actions"
     self.style = 'primary'
     self.callback_id = 'jira-slack-actions'
     self.buttons_ui = API_Slack_Attachment()
class Jira_Slack_Actions:
    def __init__(self):
        self.name = "jira-actions"
        self.style = 'primary'
        self.callback_id = 'jira-slack-actions'
        self.buttons_ui = API_Slack_Attachment()

    def get_actions_ui(self):
        self.create()
        text = ':point_right: Hello, here are the currently supported Jira Actions'
        return text, self.buttons_ui.render()

    def add_button(self, text):
        #"jira-actions", "Create Issue", "create-issue", ""
        button_id = text.lower().replace(' ', '_')
        self.buttons_ui.add_button(self.name, text, button_id, self.style)

    def create(self):  # this is where the current buttons exist
        self.buttons_ui.set_callback_id(self.callback_id)
        self.add_button('Create Issue')
        #self.add_button('Search')
        #self.add_button('Search')

        return self

    def message_not_supported_action(self, action=''):
        return {
            "text":
            ':red_circle: Sorry, action not recognized (in Jira_Slack_Actions) : {0}'
            .format(action),
            "attachments": [],
            'replace_original':
            False
        }

    def message_execution_error(self, error):
        return {
            "text":
            ':red_circle: Sorry, there was an error executing the requested action: {0}'
            .format(error),
            "attachments": [],
            'replace_original':
            False
        }

    def handle_action(self, event):
        action = Misc.get_value(Misc.array_pop(event.get('actions'), 0),
                                'value')
        try:
            target = getattr(self, action)
        except:
            return self.message_not_supported_action(action)

        try:
            return target(event)
        except Exception as error:
            return self.message_execution_error(error)

    def create_issue(self, event):
        trigger_id = event.get('trigger_id')
        channel = event.get('channel').get('id')
        team = event.get('team').get('id')
        slack_dialog = Jira_Create_Issue().setup().render()
        API_Slack(channel, team).slack.dialog_open(trigger_id=trigger_id,
                                                   dialog=slack_dialog)

        return {
            "text":
            ":point_right: Opening `Create Issue` dialog".format(
                channel, team),
            "attachments": [],
            'replace_original':
            False
        }

    def search(self, event):
        #channel    = event.get('channel').get('id')
        #team       = event.get('team').get('id')
        (text, attachments) = Slack_Jira_Search().get_drop_box_ui()
        return {
            "text": text,
            "attachments": attachments,
            'replace_original': False
        }
Example #10
0
class ELK_to_Slack:
    def __init__(self):
        self.api_issues  = API_Issues()
        self.attachments = API_Slack_Attachment()
        self.default_max =  20

    def set_default_max(self, value): self.default_max = value ; return self

    def get_search_mappings(self):
        return {
            'assign'   : 'Assignee:',
            'summary'  : 'Summary:',
            #'asset'    : 'Issue\\ Type:"IT Asset" AND Summary:',
            #'entity'   : 'Issue\\ Type:"Business Entity"  AND Summary:',
            #'epic'     : 'Issue\\ Type:"Epic"  AND Summary:',
            #'incident' : 'Issue\\ Type:"Incident" AND Summary:',
            #'meeting'  : 'Issue\\ Type:Meeting AND Summary:',
            'person'   : 'Issue\ Type:Person AND Summary:',
            #'project'  : 'Issue\\ Type:"GS-Project" AND Summary:',
            #'programme': 'Issue\\ Type:"Programme" AND Summary:',
            #'risk'     : 'Issue\\ Type:Risk AND Summary:',
            #'sc'       : 'Issue\\ Type:"Security Controls AND Summary:',
            #'service'  : 'Issue\\ Type:"GS Service" AND Summary:',

            #'vuln'     : 'Issue\\ Type:Vulnerability AND Summary:',
            #'supplier': 'Project:"Supplier\\ Log" AND Summary:',
            'task'       : 'Issue\ Type:"Task"  AND Summary:',
            'squad'      : 'Issue\ Type:Squad AND Summary:',
            'outcomes'   : 'Issue\ Type:Outcome AND Summary:',
            'keyresult'  : 'Issue\ Type:Key Result AND Summary:',
            'sow'        : 'Issue\ Type:Sow AND Summary:',

            'label'   : 'Labels:',
            'high'    : 'Rating:High      AND Summary:',
            'low'     : 'Rating:Low      AND Summary:',
            'medium'  : 'Rating:Medium      AND Summary:',


        }

    def get_search_query(self,params):
        search_type  = Lists.first(params,strip=True)
        if search_type:
            query = self.get_search_mappings().get(search_type)
            if query:
                return query + ' '.join(params[1:])
            #if index_name.lower() in ['people']: return 'Issue\ Type:People AND Summary ' +' '.join[1:]

        return ' '.join(params)

    def get_text_with_issues_key_and_summary(self, results):
        issues_list = ""
        keys        = []
        for issue in results[0:self.default_max]:
            key         = issue['Key']
            summary     = issue['Summary']
            jira_link   = "https://glasswall.atlassian.net/browse/{0}".format(key)
            issues_list += "<{0}|{1}>  {2} \n".format(jira_link, key, summary)
            keys.append(key)
        return issues_list

    def save_issues_as_new_graph(self, issues):
        all_keys = []
        for issue in issues:
            all_keys.append(issue['Key'])
        graph = GS_Graph()  # save results in graph
        graph.add_nodes(all_keys)
        return Lambda_Graph().save_gs_graph(graph, graph_type='graph-search')

    def get_slack_message(self, issues, graph_name):
        text = ":point_right: Elk search had `{0}` matches (results saved to graph `{1}` ) \n" \
            .format(len(issues), graph_name)
        if len(issues) > self.default_max:
            text += ":point_right: showing first `{0}`".format(self.default_max)
        return text

    def cmd_search(self, params, user=None, team_id=None, channel=None):
        if Lists.empty(params):
            text = ':red_circle: for the `search` command, please provide the type of search you want to do. \nHere are the the options:'
            for name in sorted(list(set(self.get_search_mappings()))):
                text +=  '\n\t\t • `{0}` '.format(name)
            text += '\n\n:point_right: the syntax is: `jira search {type} {what to search}` (note search is done on the Summary field)'
            return slack_message(text, [], channel, team_id)

        query       = self.get_search_query(params)
        issues      = self.api_issues.search_using_lucene(query)
        if channel:
            if len(issues) > 0:
                issues_text = self.get_text_with_issues_key_and_summary(issues)
                graph_name  = self.save_issues_as_new_graph(issues)
                text        = self.get_slack_message(issues, graph_name)

                self.attachments.set_text(issues_text)
                self.attachments.set_callback_id("search-results")

                return slack_message(text, self.attachments.render(), channel, team_id)
            else:
                text = ":red_circle: Elk search for `{0}` had `{1}` matches".format(query, len(issues))
                return slack_message(text, self.attachments.render(), channel, team_id)

        else:
            return issues
    # def cmd_search_graph(self, params, user, team_id, channel):
    #     results = self.api_issues.search_using_lucene(' '.join(params))
    #     keys = [issue.get('Key') for issue in results]
    #     if len(keys) == 0:
    #         text = ':black_circle: ELK search returned 0 results'
    #     else:
    #         text =  'ELK search resulted in `{0}` keys'.format(len(keys))
    #
    #         slack_cmd = 'links {0} all 1'.format(','.join(keys))
    #         params    = slack_cmd.split(' ')
    #         result    = Lambda('gs.elastic_jira').invoke({"params": params,  "user": user, "channel": channel})
    #         #slack_message(result.get('text'), result.get('attachments'), channel)
    #         self.attachments.set_text(result.get('text'))
    #
    #     slack_message(text, self.attachments.render(), channel, team_id)

    def handle_lambda_event(self, event):

        channel = event.get('channel' )
        params  = event.get("params"  )
        user    = event.get('user'    )
        team_id  = event.get('team_id')
        log_to_elk('[Elk_to_Slack.handle_lambda_event]: {0}'.format(event))
        try:
            if params:
                command = params.pop(0)
                if command == 'search'      : return self.cmd_search      (params, user, team_id, channel)
                #if command == 'search-graph': return self.cmd_search_graph(params, user, team_id, channel)

                #return slack_message(":point_right: in handle_lambda_event with params: {0}".format(event), [], channel)
            slack_message(":red_circle: in ELK_to_Slack, un-supported command :`{0}`. Data received was: {1}".format(command,event), [], channel)
        except Exception as error:
            message = ":red_circle: error in ELK_to_Slack:`{0}`. Data received was: {1}".format(error,event)
            log_to_elk(message, level = 'error')
            slack_message(message , [], channel)

    # def save_search_in_elk(self, message):
    #     index   = 'slack_interaction'
    #     item    = { 'data': message,
    #                 'date': datetime.datetime.utcnow() }
    #     elastic = Log_To_Elk().setup(index)
    #     return elastic.add(item)
Example #11
0
 def __init__(self):
     self.api_issues  = API_Issues()
     self.attachments = API_Slack_Attachment()
     self.default_max =  20