Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 3
0
 def _report_error(self, content):
     content = sanitize_sensitive_data(content)
     if self.context.pr_id:
         pr = PullRequest(bitbucket, self.context.repository)
         pr.comment(self.context.pr_id, content)
     else:
         cs = Changesets(bitbucket, self.context.repository)
         cs.comment(self.commit_hash, content)
Exemplo n.º 4
0
 def _report_git_error(self, exc):
     self.build_status.update('FAILED',
                              description='Git clone repository failed')
     content = ':broken_heart: **Git error**: {}'.format(to_text(exc))
     content = sanitize_sensitive_data(content)
     if self.context.pr_id:
         pr = PullRequest(bitbucket, self.context.repository)
         pr.comment(self.context.pr_id, content)
     else:
         cs = Changesets(bitbucket, self.context.repository)
         cs.comment(self.commit_hash, content)
Exemplo n.º 5
0
def test_sanitize_basic_auth_urls_same_line():
    text = '''abc
    http://example.com -e git+https://user:[email protected]/

    def
    '''
    sanitized = sanitize_sensitive_data(text)
    assert 'user' not in sanitized
    assert 'pwd' not in sanitized
    assert 'http://example.com' in sanitized
    assert 'git+https://***:***@example.com' in sanitized
Exemplo n.º 6
0
def test_sanitize_basic_auth_urls():
    text = 'abc http://user:[email protected] def'
    sanitized = sanitize_sensitive_data(text)
    assert 'user' not in sanitized
    assert 'pwd' not in sanitized
    assert 'http://***:***@example.com' in sanitized

    text = '''abc
    http://user:[email protected]

    def
    '''
    sanitized = sanitize_sensitive_data(text)
    assert 'user' not in sanitized
    assert 'pwd' not in sanitized
    assert 'http://***:***@example.com' in sanitized

    text = '''abc
    http://example.com

    -e git+https://user:[email protected]/

    def
    '''
    sanitized = sanitize_sensitive_data(text)
    assert 'user' not in sanitized
    assert 'pwd' not in sanitized
    assert 'http://example.com' in sanitized
    assert 'git+https://***:***@example.com' in sanitized

    lots_of_urls = ['-e git+https://user:[email protected]  abcd'] * 1000
    lots_of_urls.extend(['abc http://example.com def'] * 1000)
    text = '\n'.join(lots_of_urls)
    sanitized = sanitize_sensitive_data(text)
    assert 'user' not in sanitized
    assert 'pwd' not in sanitized
    assert 'http://example.com' in sanitized
    assert 'git+https://***:***@example.com' in sanitized