コード例 #1
0
    def send_notifications(self, context):
        exit_code = context['exit_code']
        template = 'test_success' if exit_code == 0 else 'test_failure'
        html = render_template('mail/' + template + '.html', **context)
        html = sanitize_sensitive_data(html)

        # Save log html
        log_dir = os.path.join(current_app.config['BADWOLF_LOG_DIR'],
                               self.commit_hash)
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        log_file = os.path.join(log_dir, 'build.html')
        with open(log_file, 'wb') as f:
            f.write(to_binary(html))

        if exit_code == 0:
            subject = 'Test succeed for repository {}'.format(
                self.repo_full_name)
        else:
            subject = 'Test failed for repository {}'.format(
                self.repo_full_name)
        notification = self.spec.notification
        emails = notification['emails']
        if emails:
            send_mail(emails, subject, html)

        slack_webhooks = notification['slack_webhooks']
        if slack_webhooks:
            message = render_template('slack_webhook/' + template + '.md',
                                      **context)
            trigger_slack_webhook(slack_webhooks, message)
コード例 #2
0
    def send_notifications(self, context):
        exit_code = context['exit_code']
        template = 'test_success' if exit_code == 0 else 'test_failure'
        html = render_template('mail/' + template + '.html', **context)
        html = sanitize_sensitive_data(html)

        # Save log html
        log_dir = os.path.join(current_app.config['BADWOLF_LOG_DIR'],
                               self.commit_hash, self.context.task_id)
        os.makedirs(log_dir, exist_ok=True)
        log_file = os.path.join(log_dir, 'build.html')
        with open(log_file, 'wb') as f:
            f.write(to_binary(html))

        if exit_code == 137:
            logger.info('Build cancelled, will not sending notification')
            return

        test_status = 'succeed' if exit_code == 0 else 'failed'
        subject = 'Test {} for repository {}'.format(test_status,
                                                     self.context.repository)
        notification = self.spec.notification
        email = notification.email
        should_send_mail = email and email.recipients and (
            (exit_code == 0 and email.on_success == 'always') or
            (exit_code != 0 and email.on_failure == 'always'))
        if should_send_mail:
            send_mail(email.recipients, subject, html)

        slack_webhook = notification.slack_webhook
        if slack_webhook and slack_webhook.webhooks:
            if exit_code == 0 and slack_webhook.on_success == 'always':
                trigger_slack_webhook(slack_webhook.webhooks, context)
            if exit_code != 0 and slack_webhook.on_failure == 'always':
                trigger_slack_webhook(slack_webhook.webhooks, context)