Commit d096ae50 authored by Michal Čihař's avatar Michal Čihař

Introduced special hook for GitHub

parent 52cb1746
......@@ -83,8 +83,15 @@ Interacting with others
-----------------------
You can trigger update of underlaying git repository for every subproject by
accessing URL :file:`/hooks/p/project/subproject/update/`. This can be used for
example as as Post-Receive URLs on Github.
accessing URL :file:`/hooks/p/project/subproject/update/`.
For GitHub, there is a special URL :file:`/hooks/github/`, which parses GitHub
notifications and updates related projects automatically.
.. note::
The GitHub notification relies on Git urls you use to be in form
git://github.com/owner/repo.git
.. _privileges:
......
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotAllowed, HttpResponseNotFound
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotAllowed, HttpResponseNotFound, HttpResponseBadRequest
from trans.models import Project, SubProject, Translation, Unit, Suggestion, Check
from django.shortcuts import get_object_or_404
import json
import logging
logger = logging.getLogger('weblate')
@csrf_exempt
def update_subproject(request, project, subproject):
'''
......@@ -15,3 +20,29 @@ def update_subproject(request, project, subproject):
obj.update_branch()
obj.create_translations()
return HttpResponse('updated')
@csrf_exempt
def github_hook(request):
'''
API to handle commit hooks from Github.
'''
if not settings.ENABLE_HOOKS:
return HttpResponseNotAllowed([])
if request.method != 'POST':
return HttpResponseNotAllowed(['POST'])
try:
data = json.loads(request.raw_post_data)
except ValueError:
return HttpResponseBadRequest('could not parse json!')
repo = 'git://github.com/%s/%s.git' % (
data['repository']['owner']['name'],
data['repository']['name'],
)
logger.info('received GitHub notification on repository %s', repo)
for s in SubProject.objects.filter(repo = repo):
logger.info('GitHub notification will update %s', s)
s.update_branch()
s.create_translations()
return HttpResponse('updated')
......@@ -31,6 +31,7 @@ urlpatterns = patterns('',
url(r'^languages/(?P<lang>[^/]*)/$', 'trans.views.show_language'),
url(r'^hooks/p/(?P<project>[^/]*)/(?P<subproject>[^/]*)/update/$', 'trans.api.update_subproject'),
url(r'^hooks/github/$', 'trans.api.github_hook'),
url(r'^js/get/(?P<checksum>[^/]*)/$', 'trans.views.get_string'),
url(r'^js/ignore-check/(?P<check_id>[0-9]*)/$', 'trans.views.ignore_check'),
......
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