Merge pull request #550 from Gluejar/travis_webhook

Travis webhook
pull/1/head
eshellman 2016-02-24 09:09:27 -05:00
commit 2256940bc7
3 changed files with 91 additions and 0 deletions

View File

@ -188,3 +188,45 @@ class AllowedRepoTests(TestCase):
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/master/metadata.yaml')[0]) self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/master/metadata.yaml')[0])
self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/master/metadata.json')[0]) self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/master/metadata.json')[0])
class WebHookTests(TestCase):
def test_travisci_webhook(self):
"""
test of api.views.travisci_webhook
"""
payload = json.dumps({
"repository":{
"id":4651401,
"name":"Adventures-of-Huckleberry-Finn_76",
"owner_name":"GITenberg",
"url":"http://GITenberg.github.com/"
},
"status_message": "Passed",
"type": "push"
})
invalid_payload = json.dumps({
"repository":{
"id":4651401,
"name":"",
"url":"http://GITenberg.github.com/"
},
"status_message": "Passed",
"type": "push"
})
url = "/api/travisci/webhook"
# 200 if a simple get
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
# 200 when we actually load a valid repo (should we use 201?)
r = self.client.post(url, data={'payload':payload}, headers={}, allow_redirects=True)
self.assertEqual(r.status_code, 200)
# 400 error if we get exception when trying to load a book
r = self.client.post(url, data={'payload':invalid_payload}, headers={}, allow_redirects=True)
self.assertEqual(r.status_code, 400)

View File

@ -27,5 +27,6 @@ urlpatterns = patterns('',
url(r"^onix/$", OnixView.as_view(), name="onix_all"), url(r"^onix/$", OnixView.as_view(), name="onix_all"),
url(r'^id/work/(?P<work_id>\w+)/$', 'regluit.api.views.negotiate_content', name="work_identifier"), url(r'^id/work/(?P<work_id>\w+)/$', 'regluit.api.views.negotiate_content', name="work_identifier"),
url(r'^loader/yaml$','regluit.api.views.load_yaml', name="load_yaml"), url(r'^loader/yaml$','regluit.api.views.load_yaml', name="load_yaml"),
url(r'^travisci/webhook$','regluit.api.views.travisci_webhook', name="travisci_webhook"),
(r'^', include(v1_api.urls)), (r'^', include(v1_api.urls)),
) )

View File

@ -1,19 +1,26 @@
from tastypie.models import ApiKey from tastypie.models import ApiKey
import json
import logging
from django.contrib import auth from django.contrib import auth
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, get_object_or_404 from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View, TemplateView from django.views.generic.base import View, TemplateView
from django.http import ( from django.http import (
HttpResponse, HttpResponse,
HttpResponseNotFound, HttpResponseNotFound,
HttpResponseBadRequest,
HttpResponseRedirect, HttpResponseRedirect,
Http404, Http404,
) )
import regluit.core.isbn import regluit.core.isbn
from regluit.core.bookloader import load_from_yaml from regluit.core.bookloader import load_from_yaml
from regluit.api import opds, onix from regluit.api import opds, onix
@ -21,6 +28,8 @@ from regluit.api.models import repo_allowed
from regluit.core import models from regluit.core import models
logger = logging.getLogger(__name__)
def editions(request): def editions(request):
editions = models.Edition.objects.all() editions = models.Edition.objects.all()
@ -76,6 +85,45 @@ def load_yaml(request):
return HttpResponseRedirect(reverse('work', args=[work_id])) return HttpResponseRedirect(reverse('work', args=[work_id]))
except: except:
return HttpResponse('unsuccessful') return HttpResponse('unsuccessful')
@csrf_exempt
def travisci_webhook(request):
"""
Respond to travis-ci webhooks from Project GITenberg repositories. If the webhook is successfully parsed,
the metdata.yaml for the repository is loaded using load_from_yaml.
https://docs.travis-ci.com/user/notifications/#Webhook-notification
"""
if request.method == "POST":
try:
data = json.loads(request.POST.get('payload'))
# example of URL to feed to yaml loader:
# https://github.com/GITenberg/Adventures-of-Huckleberry-Finn_76/raw/master/metadata.yaml
if data['status_message'] == 'Passed' and data['type'] == 'push':
# another way to get owner_name / name would be request.META.get('HTTP_TRAVIS_REPO_SLUG', '')
repo_url = "https://github.com/{}/{}/raw/master/metadata.yaml".format(data['repository']['owner_name'],
data['repository']['name'])
work_id = load_from_yaml(repo_url)
return HttpResponse('Successful. work_id: {}'.format(work_id))
except Exception as e:
return HttpResponseBadRequest('Unsuccessful. Exception: {}'.format(unicode(e)))
else:
return HttpResponse('No action')
else:
return HttpResponse('No action')
class ApiHelpView(TemplateView): class ApiHelpView(TemplateView):
template_name = "api_help.html" template_name = "api_help.html"