commit
2256940bc7
42
api/tests.py
42
api/tests.py
|
@ -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/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)
|
||||
|
||||
|
||||
|
|
|
@ -27,5 +27,6 @@ urlpatterns = patterns('',
|
|||
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'^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)),
|
||||
)
|
||||
|
|
48
api/views.py
48
api/views.py
|
@ -1,19 +1,26 @@
|
|||
from tastypie.models import ApiKey
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.contrib import auth
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.shortcuts import render_to_response, get_object_or_404
|
||||
from django.template import RequestContext
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.generic.base import View, TemplateView
|
||||
from django.http import (
|
||||
HttpResponse,
|
||||
HttpResponseNotFound,
|
||||
HttpResponseBadRequest,
|
||||
HttpResponseRedirect,
|
||||
Http404,
|
||||
)
|
||||
|
||||
|
||||
|
||||
import regluit.core.isbn
|
||||
from regluit.core.bookloader import load_from_yaml
|
||||
from regluit.api import opds, onix
|
||||
|
@ -21,6 +28,8 @@ from regluit.api.models import repo_allowed
|
|||
|
||||
from regluit.core import models
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def editions(request):
|
||||
editions = models.Edition.objects.all()
|
||||
|
@ -76,6 +85,45 @@ def load_yaml(request):
|
|||
return HttpResponseRedirect(reverse('work', args=[work_id]))
|
||||
except:
|
||||
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):
|
||||
template_name = "api_help.html"
|
||||
|
|
Loading…
Reference in New Issue