Пример #1
0
 def update_build_status(self, state, description=None):
     try:
         self.build_status.update(state, description=description)
     except BitbucketAPIError:
         logger.exception('Error calling Bitbucket API')
         sentry.captureException()
Пример #2
0
def trigger_slack_webhook(webhooks, context, provider, succeed):
    actor = context.actor
    if succeed:
        title = '{} deploy succeed'.format(provider.name)
        color = 'good'
    else:
        title = '{} deploy failed'.format(provider.name)
        color = 'warning'
    fields = []
    fields.append({
        'title':
        'Repository',
        'value':
        '<https://bitbucket.org/{repo}|{repo}>'.format(
            repo=context.repository),
        'short':
        True,
    })
    if context.type == 'tag':
        fields.append({
            'title':
            'Tag',
            'value':
            '<https://bitbucket.org/{repo}/commits/tag/{tag}|{tag}>'.format(
                repo=context.repository, tag=context.source['branch']['name']),
            'short':
            True,
        })
    else:
        fields.append({
            'title':
            'Branch',
            'value':
            '<https://bitbucket.org/{repo}/src?at={branch}|{branch}>'.format(
                repo=context.repository,
                branch=context.source['branch']['name']),
            'short':
            True,
        })
    if context.type in {'branch', 'tag'}:
        fields.append({
            'title':
            'Commit',
            'value':
            '<https://bitbucket.org/{repo}/commits/{sha}|{sha}>'.format(
                repo=context.repository,
                sha=context.source['commit']['hash'],
            ),
            'short':
            False
        })
    attachment = {
        'fallback': title,
        'title': title,
        'color': color,
        'fields': fields,
        'footer': context.repo_name,
        'ts': int(time.time()),
        'author_name': actor['display_name'],
        'author_link': actor['links']['html']['href'],
        'author_icon': actor['links']['avatar']['href'],
    }
    if context.type in {'branch', 'tag'}:
        attachment['text'] = context.message
    payload = {'attachments': [attachment]}
    session = requests.Session()
    for webhook in webhooks:
        logger.info('Triggering Slack webhook %s', webhook)
        res = session.post(webhook, json=payload, timeout=10)
        try:
            res.raise_for_status()
        except requests.RequestException:
            logger.exception('Error triggering Slack webhook %s', webhook)
            sentry.captureException()
Пример #3
0
def handle_repo_push(payload):
    changes = payload['push']['changes']
    if not changes:
        return

    repo = payload['repository']
    scm = repo['scm']
    if scm.lower() != 'git':
        logger.info('Unsupported version system: %s', scm)
        return

    for change in changes:
        if not change['new']:
            logger.info('No new changes found')
            continue

        repo_name = repo['full_name']
        push_type = change['new']['type']
        rebuild = False
        if push_type == 'tag':
            commit_hash = change['new']['target']['hash']
            commit_message = change['new']['target']['message']
        elif push_type == 'branch':
            if not change['commits']:
                logger.warning('Can not find any commits')
                continue
            commit_hash = change['commits'][0]['hash']
            commit_message = change['commits'][0]['message']
            if 'ci skip' in commit_message.lower():
                logger.info('ci skip found, ignore tests.')
                continue
            if 'ci rebuild' in commit_message.lower():
                rebuild = True
        else:
            logger.error('Unsupported push type: %s', push_type)
            continue

        source = {
            'repository': {
                'full_name': repo_name
            },
            'branch': {
                'name': change['new']['name']
            },
            'commit': {
                'hash': commit_hash
            }
        }
        context = Context(
            repo_name,
            payload['actor'],
            push_type,
            commit_message,
            source,
            rebuild=rebuild,
        )
        try:
            _cancel_outdated_pipelines(context)
        except Exception:
            sentry.captureException()
        future = start_pipeline.delay(context)
        future.add_done_callback(
            lambda fut: _RUNNING_PIPELINES.pop(context.task_id, None))
        _RUNNING_PIPELINES[context.task_id] = future
        if push_type == 'branch':
            check_pr_mergeable.delay(context)
Пример #4
0
def trigger_slack_webhook(webhooks, context):
    if context['exit_code'] == 0:
        color = 'good'
        title = ':sparkles: <{}|Test succeed for repository {}>'.format(
            context['build_log_url'],
            context['context'].repository,
        )
    else:
        color = 'warning'
        title = ':broken_heart: <{}|Test failed for repository {}>'.format(
            context['build_log_url'],
            context['context'].repository,
        )
    fields = []
    fields.append({
        'title':
        'Repository',
        'value':
        '<https://bitbucket.org/{repo}|{repo}>'.format(
            repo=context['context'].repository),
        'short':
        True,
    })
    if context['context'].type == 'tag':
        fields.append({
            'title':
            'Tag',
            'value':
            '<https://bitbucket.org/{repo}/commits/tag/{tag}|{tag}>'.format(
                repo=context['context'].repository, tag=context['branch']),
            'short':
            True,
        })
    elif context['context'].type != 'commit':
        fields.append({
            'title':
            'Branch',
            'value':
            '<https://bitbucket.org/{repo}/src?at={branch}|{branch}>'.format(
                repo=context['context'].repository,
                branch=context['branch'],
            ),
            'short':
            True,
        })
    if context['context'].type in {'branch', 'tag', 'commit'}:
        fields.append({
            'title':
            'Commit',
            'value':
            '<https://bitbucket.org/{repo}/commits/{sha}|{sha}>'.format(
                repo=context['context'].repository,
                sha=context['context'].source['commit']['hash'],
            ),
            'short':
            False
        })
    elif context['context'].type == 'pullrequest':
        fields.append({
            'title':
            'Pull Request',
            'value':
            '<https://bitbucket.org/{repo}/pull-requests/{pr_id}|{title}>'.
            format(
                repo=context['context'].repository,
                pr_id=context['context'].pr_id,
                title=context['context'].message,
            ),
            'short':
            False
        })

    actor = context['context'].actor
    attachment = {
        'fallback': title,
        'title': title,
        'color': color,
        'title_link': context['build_log_url'],
        'fields': fields,
        'footer': context['context'].repo_name,
        'ts': int(time.time()),
        'author_name': actor['display_name'],
        'author_link': actor['links']['html']['href'],
        'author_icon': actor['links']['avatar']['href'],
    }
    if context['context'].type in {'branch', 'tag'}:
        attachment['text'] = context['context'].message
    payload = {'attachments': [attachment]}
    session = requests.Session()
    for webhook in webhooks:
        logger.info('Triggering Slack webhook %s', webhook)
        res = session.post(webhook, json=payload, timeout=10)
        try:
            res.raise_for_status()
        except requests.RequestException:
            logger.exception('Error triggering Slack webhook %s', webhook)
            sentry.captureException()