Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Support register BitBucket webhook by command
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 29, 2016
1 parent d76c7d5 commit 7e67a15
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
24 changes: 24 additions & 0 deletions badwolf/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,30 @@ def comment(self, node, content, line_from=None, line_to=None,
return self.client.post(endpoint, data=data)


class Hooks(object):
def __init__(self, client, repo):
self.client = client
self.repo = repo

def add(self, name, url, events=None):
endpoint = '2.0/repositories/{repo}/hooks'.format(
repo=self.repo,
)
data = optionaldict(
url=url,
description=name,
events=events,
active=True
)
return self.client.post(endpoint, json=data)

def list(self):
endpoint = '2.0/repositories/{repo}/hooks'.format(
repo=self.repo,
)
return self.client.get(endpoint)


class FlaskBitbucket(object):
def __init__(self, app=None):
self.app = app
Expand Down
29 changes: 29 additions & 0 deletions badwolf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import signal

import click
from flask import url_for

import badwolf

Expand Down Expand Up @@ -90,3 +91,31 @@ def shell():
import code

code.interact(local=context)


@manage.command()
@click.argument('repo', required=True)
def register_webhook(repo):
'''Register badwolf webhook on BitBucket for REPO'''
from badwolf.wsgi import app
from badwolf.extensions import bitbucket
from badwolf.bitbucket import Hooks

with app.app_context():
webhook_url = url_for('webhook.webhook_push', _external=True)
hooks = Hooks(bitbucket, repo)
existing_hooks = hooks.list()
existing_urls = [hook['url'] for hook in existing_hooks['values']]
if webhook_url in existing_urls:
click.echo('Webhook {} already registered'.format(webhook_url))
return

hooks.add('badwolf', webhook_url, events=(
'repo:push',
'repo:commit_comment_created',
'pullrequest:created',
'pullrequest:updated',
'pullrequest:approved',
'pullrequest:comment_updated',
))
click.echo('Webhook {} registered'.format(webhook_url))
8 changes: 7 additions & 1 deletion docs/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ privileged boolean 使用特权模式启动 Doc
比如:`FROM messense/badwolf-test-runner:python`

然后,在 BitBucket 项目设置中配置 webhook,假设部署机器的可访问地址为:http://badwolf.example.com:8000,
则 webhook 地址应配置为:`http://badwolf.example.com:8000/webhook/push`
则 webhook 地址应配置为:`http://badwolf.example.com:8000/webhook/push`。

也可以使用 `badwolf register_webhook REPO` 命令自动配置 webhook,如:

.. code-block:: bash
badwolf register_webhook deepanalyzer/badwolf
Tips
-----------
Expand Down

0 comments on commit 7e67a15

Please sign in to comment.