Commit 7a3e5930 authored by Michal Čihař's avatar Michal Čihař

Added support for Bitbucket webhooks.

Fixes #771
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent ddcbc556
......@@ -11,6 +11,7 @@ Released on ? 2015.
* Improved configurability of import_project command.
* Added CSV dump of history.
* Avoid copy/paste errors with whitespace chars.
* Added support for Bitbucket webhooks.
weblate 2.3
-----------
......
......@@ -209,6 +209,86 @@ BITBUCKET_PAYLOAD_HG_NO_COMMIT = '''
}
'''
BITBUCKET_PAYLOAD_WEBHOOK = r'''
{
"actor": {
"username": "emmap1",
"display_name": "Emma",
"uuid": "{a54f16da-24e9-4d7f-a3a7-b1ba2cd98aa3}",
"links": {
"self": {
"href": "https://api.bitbucket.org/api/2.0/users/emmap1"
},
"html": {
"href": "https://api.bitbucket.org/emmap1"
},
"avatar": {
"href": "https://bitbucket-api-assetroot/emmap1-avatar_avatar.png"
}
}
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/api/2.0/repositories/bitbucket/bit"
},
"html": {
"href": "https://api.bitbucket.org/bitbucket/bitbucket"
},
"avatar": {
"href": "https://api-staging-assetroot/2629490769-3_avatar.png"
}
},
"uuid": "{673a6070-3421-46c9-9d48-90745f7bfe8e}",
"full_name": "team_name/repo_name",
"name": "repo_name"
},
"push": {
"changes": [
{
"new": {
"type": "branch",
"name": "name-of-branch",
"target": {
"type": "commit",
"hash": "709d658dc5b6d6afcd46049c2f332ee3f515a67d",
"author": {},
"message": "new commit message\n",
"date": "2015-06-09T03:34:49+00:00",
"parents": [
{
"hash": "1e65c05c1d5171631d92438a13901ca7dae9618c",
"type": "commit"
}
]
}
},
"old": {
"type": "branch",
"name": "name-of-branch",
"target": {
"type": "commit",
"hash": "1e65c05c1d5171631d92438a13901ca7dae9618c",
"author": {},
"message": "old commit message\n",
"date": "2015-06-08T21:34:56+00:00",
"parents": [
{
"hash": "e0d0c2041e09746be5ce4b55067d5a8e3098c843",
"type": "commit"
}
]
}
},
"created": false,
"forced": false,
"closed": false
}
]
}
}
'''
class HooksViewTest(ViewTestCase):
@OverrideSettings(ENABLE_HOOKS=True)
......@@ -277,6 +357,15 @@ class HooksViewTest(ViewTestCase):
)
self.assertContains(response, 'update triggered')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_bitbucket_webhook(self):
response = self.client.post(
reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_WEBHOOK}
)
self.assertContains(response, 'update triggered')
@OverrideSettings(ENABLE_HOOKS=False)
def test_disabled(self):
'''
......
......@@ -47,6 +47,16 @@ BITBUCKET_HG_REPOS = (
'hg::https://bitbucket.org/%(owner)s/%(slug)s',
)
BITBUCKET_REPOS = (
'ssh://git@bitbucket.org/%(full_name)s.git',
'git@bitbucket.org:%(full_name)s.git',
'https://bitbucket.org/%(full_name)s.git',
'https://bitbucket.org/%(full_name)s',
'ssh://hg@bitbucket.org/%(full_name)s',
'hg::ssh://hg@bitbucket.org/%(full_name)s',
'hg::https://bitbucket.org/%(full_name)s',
)
GITHUB_REPOS = (
'git://github.com/%(owner)s/%(slug)s.git',
'https://github.com/%(owner)s/%(slug)s.git',
......@@ -144,6 +154,7 @@ def vcs_service_hook(request, service):
try:
service_data = hook_helper(data)
except KeyError:
weblate.logger.error('failed to parse service %s data', service)
return HttpResponseBadRequest('Invalid data in json payload!')
# Log data
......@@ -175,11 +186,28 @@ def vcs_service_hook(request, service):
return HttpResponse('update triggered')
def bitbucket_webhook_helper(data):
"""API to handle webhooks from Bitbucket"""
repos = [
repo % {'full_name': data['repository']['full_name']}
for repo in BITBUCKET_REPOS
]
return {
'service_long_name': 'Bitbucket',
'repos': repos,
'branch': data['push']['changes'][-1]['new']['name']
}
@register_hook
def bitbucket_hook_helper(data):
'''
API to handle commit hooks from Bitbucket.
API to handle service hooks from Bitbucket.
'''
if 'push' in data:
return bitbucket_webhook_helper(data)
# Parse owner, branch and repository name
owner = data['repository']['owner']
slug = data['repository']['slug']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment