예제 #1
0
    def publish_custom_message(self, message, channel=None, user='******', icon=None, emoji=None,
                               attachment_color=None, slack_url=None, button_text=None, button_link=None):
        method = 'publish_custom_message'
        commons.print_msg(Slack.clazz, method, 'begin')

        slack_message = Object()

        if slack_url is not None:
            Slack.slack_url = slack_url

        if slack_url is None and Slack.slack_url is None and BuildConfig.settings.has_section('slack') and \
                BuildConfig.settings.has_option('slack', 'generic_message_slack_url'):
            Slack.slack_url = BuildConfig.settings.get('slack', 'generic_message_slack_url')
        elif slack_url is None and Slack.slack_url is None:
            commons.print_msg(Slack.clazz, method, 'No Slack URL was found in the environment or settings.ini.  Failed to send message', 'ERROR')
            exit(1)

        if emoji is None and 'slack' in BuildConfig.json_config and 'emoji' in BuildConfig.json_config['slack']:
            emoji = BuildConfig.json_config['slack']['emoji']
            slack_message.icon_emoji = emoji
        elif emoji is None and BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'emoji'):
            emoji = BuildConfig.settings.get('slack', 'emoji')
            slack_message.icon_emoji = emoji

        if icon is None and 'slack' in BuildConfig.json_config and 'icon' in BuildConfig.json_config['slack']:
            icon = BuildConfig.json_config['slack']['icon']
            slack_message.icon_url = icon
        elif icon is None and BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'icon'):
            icon = BuildConfig.settings.get('slack', 'icon')
            slack_message.icon_url = icon

        if channel is None and 'slack' in BuildConfig.json_config and 'channel' in BuildConfig.json_config['slack']:
            slack_channel = BuildConfig.json_config['slack']['channel']
            slack_message.channel = slack_channel
        elif channel is None and BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'channel'):
            slack_channel = BuildConfig.settings.get('slack', 'channel')
            slack_message.channel = slack_channel

        if user is None and 'slack' in BuildConfig.json_config and 'botName' in BuildConfig.json_config['slack']:
            user = BuildConfig.json_config['slack']['botName']
        elif user is None and BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'bot_name'):
            user = BuildConfig.settings.get('slack', 'bot_name')

        app_version = BuildConfig.version_number
        environment = BuildConfig.build_env
        app_name = BuildConfig.json_config['projectInfo']['name']

        slack_message.username = user
        slack_message.attachments = []

        # Application information
        attachment = Object()
        attachment.pretext = attachment.fallback = message

        if attachment_color is not None:
            attachment.color = attachment_color
        elif (BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack',
                                                                                  'release_note_attachment_color')):
            attachment.color = BuildConfig.settings.get('slack', 'release_note_attachment_color')
        else:
            attachment.color = '#0000ff'

        attachment.author_name = app_name + ' ' + environment + " " + (str(app_version) if str(app_version) is not
                                                                                           None else 'Uknown Version')
        if 'github' in self.config.json_config and 'org' in self.config.json_config['github']:
            attachment.author_name = "{msg} \n org: {org} \n repo: {repo}".format(msg=attachment.author_name,
                                                                    org=self.config.json_config['github']['org'],
                                                              repo=self.config.json_config['github']['repo'])
        # attachment.title = app_name
        attachment.title_link = (os.environ.get('BUILD_URL') if os.environ.get('BUILD_URL') is not None else '')
        attachment.footer = 'Flow'
        # attachment.text = message

        slack_message.attachments.append(attachment)

        commons.print_msg(Slack.clazz, method, slack_message.to_JSON())

        headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

        commons.print_msg(Slack.clazz, method, Slack.slack_url)

        try:
            resp = requests.post(Slack.slack_url, slack_message.to_JSON(), headers=headers,
                                 timeout=Slack.http_timeout)
            if resp.status_code == 200:
                commons.print_msg(Slack.clazz, method, "Successfully sent to slack. \r\n resp: {}".format(resp.text),
                                 "DEBUG")
            else:
                commons.print_msg(Slack.clazz, method, "Failed sending slack message to {url} \r\n Resp: {resp} \r\n "
                                                      "Status: {stat}".format(url=Slack.slack_url, resp=resp.text,
                                                                              stat=resp.status_code), "WARN")

        except requests.ConnectionError:
            commons.print_msg(Slack.clazz, method, "Request to Slack timed out.", "ERROR")
        except Exception as e:
            commons.print_msg(Slack.clazz, method, "Failed sending slack message to {url} with exception {ex}"
                              .format(url=Slack.slack_url,
                                     ex=e))

        commons.print_msg(Slack.clazz, method, 'end')
예제 #2
0
    def publish_error(sender, message, class_name, method_name):
        method = 'publish_error'
        commons.print_msg(Slack.clazz, method, 'begin')

        if Slack.slack_url is None:
            commons.print_msg(Slack.clazz, method, 'No Slack URL was found in the environment.  Did you set SLACK_WEBHOOK_URL in your pipeline?', 'WARN')
        else:
            icon = None
            emoji = None
            slack_channel = None
            user_name = None

            if 'slack' in BuildConfig.json_config and 'emoji' in BuildConfig.json_config['slack']:
                emoji = BuildConfig.json_config['slack']['emoji']
            elif BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'emoji'):
                emoji = BuildConfig.settings.get('slack', 'emoji')

            if 'slack' in BuildConfig.json_config and 'icon' in BuildConfig.json_config['slack']:
                icon = BuildConfig.json_config['slack']['icon']
            elif BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'icon'):
                icon = BuildConfig.settings.get('slack', 'icon')

            if 'slack' in BuildConfig.json_config and 'channel' in BuildConfig.json_config['slack']:
                slack_channel = BuildConfig.json_config['slack']['channel']
            elif BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'channel'):
                slack_channel = BuildConfig.settings.get('slack', 'channel')

            if 'slack' in BuildConfig.json_config and 'botName' in BuildConfig.json_config['slack']:
                user_name = BuildConfig.json_config['slack']['botName']
            elif BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack', 'bot_name'):
                user_name = BuildConfig.settings.get('slack', 'bot_name')

            app_version = BuildConfig.version_number
            environment = BuildConfig.build_env
            app_name = BuildConfig.json_config['projectInfo']['name']

            slack_message = Object()
            if icon:
                slack_message.icon_url = icon
            else:
                slack_message.icon_emoji = emoji
            if slack_channel:
                slack_message.channel = slack_channel

            slack_message.username = user_name
            slack_message.attachments = []

            # Application information
            attachment = Object()
            attachment.pretext = attachment.fallback = "CI/CD for {} has failed".format(app_name)

            if (BuildConfig.settings.has_section('slack') and BuildConfig.settings.has_option('slack',
                                                                                              'error_attachment_color')):
                attachment.color = BuildConfig.settings.get('slack', 'error_attachment_color')
            else:
                attachment.color = '#ff0000'

            attachment.author_name = environment + " " + (str(app_version) if str(app_version) is not None else 'Uknown Version')
            attachment.title = "Build " + (os.environ.get('BUILD_ID') if os.environ.get('BUILD_ID') is not None else 'Uknown')
            attachment.title_link = (os.environ.get('BUILD_URL') if os.environ.get('BUILD_URL') is not None else '')
            attachment.footer = 'Flow'
            attachment.text = message

            attachment.fields = []

            attachment_field = Object()
            attachment_field.value = class_name
            attachment_field.title = 'Class'
            attachment.fields.append(attachment_field)

            attachment_field = Object()
            attachment_field.value = method_name
            attachment_field.title = 'Method'
            attachment.fields.append(attachment_field)

            slack_message.attachments.append(attachment)

            commons.print_msg(Slack.clazz, method, slack_message.to_JSON())

            headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

            commons.print_msg(Slack.clazz, method, Slack.slack_url)

            try:
                resp = requests.post(Slack.slack_url, slack_message.to_JSON(), headers=headers,
                                     timeout=Slack.http_timeout)

                if resp.status_code == 200:
                    commons.print_msg(Slack.clazz, method,
                                      "Successfully sent to slack. \r\n resp: {}".format(resp.text),
                                      "DEBUG")
                else:
                    commons.print_msg(Slack.clazz, method,
                                      "Failed sending slack message to {url} \r\n Resp: {resp} \r\n "
                                      "Status: {stat}".format(url=Slack.slack_url, resp=resp.text,
                                                              stat=resp.status_code), "WARN")

            except requests.ConnectionError:
                commons.print_msg(Slack.clazz, method, "Request to Slack timed out.", "ERROR")
            except Exception as e:
                commons.print_msg(Slack.clazz, method, "Failed sending slack message to {url} with exception {ex}"
                                  .format(url=Slack.slack_url,
                                          ex=e))

        commons.print_msg(Slack.clazz, method, 'end')