Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a simple "API" for reading/writing PR/ticket linkage.

We'll use this to interact with pull request data over on the Trac side.
  • Loading branch information...
commit 06e95dcb41c6ba2bfd2a86e5894b1a8623e226f9 1 parent 512beab
@jacobian jacobian authored
View
4 django_website/gitrachub/urls.py
@@ -2,5 +2,7 @@
from . import views
urlpatterns = patterns('',
- url(r'^webhook/$', views.GithubWebhook.as_view(), name='github_webhook')
+ url(r'^webhook/$', views.GithubWebhook.as_view(), name='github_webhook'),
+ url(r'^pulls/(?P<ticket_id>\d+)/$',
+ views.PullRequestsForTicket.as_view(), name='pull_requests_for_ticket'),
)
View
53 django_website/gitrachub/views.py
@@ -1,5 +1,4 @@
import json
-from django.contrib.auth.decorators import user_passes_test
from django.forms import model_to_dict
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
@@ -32,3 +31,55 @@ def handle_push_webhook(self):
def handle_issue_comment_webhook(self):
print "Issue comment!"
return HttpResponse(status=204)
+
+class PullRequestsForTicket(View):
+ """
+ Expose pull requests for a ticket as a minimal web service.
+ """
+ @method_decorator(csrf_exempt)
+ def dispatch(self, request, ticket_id):
+ self.ticket = get_object_or_404(Ticket, id=ticket_id)
+ return super(PullRequestsForTicket, self).dispatch(request)
+
+ def get(self, request):
+ """
+ GET pulls/{ticket_id} --> pull requests linked to ticket {ticket_id}
+
+ Returns JSON in the form of::
+
+ {"pulls": [
+ {"number": <pull-request-number>, "ticket_id": <ticket-id>, "title": "ticket title"},
+ ...
+ ]}
+ """
+ pull_requests = PullRequest.objects.filter(ticket_id=self.ticket.id)
+ js = json.dumps({'pulls': [model_to_dict(p) for p in pull_requests]})
+ return HttpResponse(js, content_type='application/json')
+
+ def post(self, request):
+ """
+ POST pulls/{ticket_id} --> link a pull request to ticket {ticket_id}
+
+ Expects form-encoded data (yeah yeah, it's inconsistant. Deal with it.)
+ containing a "number" field.
+ """
+ if not request.user.is_staff:
+ return HttpResponse(status=401)
+ try:
+ number = int(self.request.POST['number'])
+ except KeyError:
+ return HttpResponse("Missing number field in POST", status=400)
+ except (ValueError, TypeError):
+ return HttpResponse("Invalid number - must be an int.", status=400)
+
+ # Make sure the PR exists over on the GitHub side.
+ response = github.session().get('repos/django/django/pulls/%s' % number)
+ if response.status_code != 200:
+ return HttpResponse("Bad PR number: GItHub returned HTTP %s." % response.status_code, status=400)
+
+ pr, created = PullRequest.objects.get_or_create(number=number)
+ pr.ticket_id = self.ticket.id
+ pr.title = response.json['title']
+ pr.save()
+
+ return HttpResponse(status=204)
Please sign in to comment.
Something went wrong with that request. Please try again.