def parse_webhook_data()

in services/github-bots/LabelBotFullFunctionality/LabelBot.py [0:0]


    def parse_webhook_data(self, event):
        """
        This method triggers the label bot when the appropriate
        GitHub event is recognized by use of a webhook
        :param event: The event data that is received whenever a github issue, issue comment, etc. is made
        :return: Log statements which we can track in lambda
        """
        try:
            github_event = ast.literal_eval(event["Records"][0]['body'])['headers']["X-GitHub-Event"]
        except KeyError:
            raise Exception("Not a GitHub Event")

        if not self._secure_webhook(event):
            raise Exception("Failed to validate WebHook security")

        try:
            payload = json.loads(ast.literal_eval(event["Records"][0]['body'])['body'])
        except ValueError:
            raise Exception("Decoding JSON for payload failed")

        # Grabs actual payload data of the appropriate GitHub event needed for labelling
        if github_event == "issue_comment":

            # Acquiring labels specific to this repo
            labels = []
            actions = {}

            # Looks for and reads phrase referencing @mxnet-label-bot, and trims extra whitespace to single space
            if "@mxnet-label-bot" in payload["comment"]["body"]:
                phrase = payload["comment"]["body"][payload["comment"]["body"].find("@mxnet-label-bot"):payload["comment"]["body"].find("]")+1]
                phrase = ' '.join(phrase.split())

                labels += self._tokenize(phrase)
                if not labels:
                    logging.error(f'Message typed by user: {phrase}')
                    raise Exception("Unable to gather labels from issue comments")

                self._find_all_labels()
                if not self.all_labels:
                    raise Exception("Unable to gather labels from the repo")

                if not set(labels).intersection(set(self.all_labels)):
                    logging.error(f'Labels entered by user: {set(labels)}')
                    logging.error(f'Repo labels: {set(self.all_labels)}')
                    raise Exception("Provided labels don't match labels from the repo")

                # Case so that ( add[label1] ) and ( add [label1] ) are treated the same way
                if phrase.split(" ")[1].find('[') != -1:
                    action = phrase.split(" ")[1][:phrase.split(" ")[1].find('[')].lower()
                else:
                    action = phrase.split(" ")[1].lower()

                issue_num = payload["issue"]["number"]
                actions[action] = issue_num, labels
                if not self.label_action(actions):
                    logging.error(f'Unsupported actions: {actions}')
                    raise Exception("Unrecognized/Infeasible label action for the mxnet-label-bot")

        # On creation of a new issue, automatically trigger the bot to recommend labels
        if github_event == "issues" and payload["action"] == "opened":
            self._find_all_labels()
            return self.predict_label(payload["issue"]["number"])

        else:
            logging.info(f'GitHub Event unsupported by Label Bot: {github_event} {payload["action"]}')