Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jacob Kaplan-Moss authored April 27, 2012
4  django_website/gitrachub/urls.py
@@ -2,5 +2,7 @@
2 2
 from . import views
3 3
 
4 4
 urlpatterns = patterns('',
5  
-    url(r'^webhook/$', views.GithubWebhook.as_view(), name='github_webhook')
  5
+    url(r'^webhook/$', views.GithubWebhook.as_view(), name='github_webhook'),
  6
+    url(r'^pulls/(?P<ticket_id>\d+)/$',
  7
+        views.PullRequestsForTicket.as_view(), name='pull_requests_for_ticket'),
6 8
 )
53  django_website/gitrachub/views.py
... ...
@@ -1,5 +1,4 @@
1 1
 import json
2  
-from django.contrib.auth.decorators import user_passes_test
3 2
 from django.forms import model_to_dict
4 3
 from django.http import HttpResponse
5 4
 from django.shortcuts import get_object_or_404
@@ -32,3 +31,55 @@ def handle_push_webhook(self):
32 31
     def handle_issue_comment_webhook(self):
33 32
         print "Issue comment!"
34 33
         return HttpResponse(status=204)
  34
+
  35
+class PullRequestsForTicket(View):
  36
+    """
  37
+    Expose pull requests for a ticket as a minimal web service.
  38
+    """
  39
+    @method_decorator(csrf_exempt)
  40
+    def dispatch(self, request, ticket_id):
  41
+        self.ticket = get_object_or_404(Ticket, id=ticket_id)
  42
+        return super(PullRequestsForTicket, self).dispatch(request)
  43
+
  44
+    def get(self, request):
  45
+        """
  46
+        GET pulls/{ticket_id} --> pull requests linked to ticket {ticket_id}
  47
+
  48
+        Returns JSON in the form of::
  49
+
  50
+            {"pulls": [
  51
+                {"number": <pull-request-number>, "ticket_id": <ticket-id>, "title": "ticket title"},
  52
+                ...
  53
+            ]}
  54
+        """
  55
+        pull_requests = PullRequest.objects.filter(ticket_id=self.ticket.id)
  56
+        js = json.dumps({'pulls': [model_to_dict(p) for p in pull_requests]})
  57
+        return HttpResponse(js, content_type='application/json')
  58
+
  59
+    def post(self, request):
  60
+        """
  61
+        POST pulls/{ticket_id} --> link a pull request to ticket {ticket_id}
  62
+
  63
+        Expects form-encoded data (yeah yeah, it's inconsistant. Deal with it.)
  64
+        containing a "number" field.
  65
+        """
  66
+        if not request.user.is_staff:
  67
+            return HttpResponse(status=401)
  68
+        try:
  69
+            number = int(self.request.POST['number'])
  70
+        except KeyError:
  71
+            return HttpResponse("Missing number field in POST", status=400)
  72
+        except (ValueError, TypeError):
  73
+            return HttpResponse("Invalid number - must be an int.", status=400)
  74
+
  75
+        # Make sure the PR exists over on the GitHub side.
  76
+        response = github.session().get('repos/django/django/pulls/%s' % number)
  77
+        if response.status_code != 200:
  78
+            return HttpResponse("Bad PR number: GItHub returned HTTP %s." % response.status_code, status=400)
  79
+
  80
+        pr, created = PullRequest.objects.get_or_create(number=number)
  81
+        pr.ticket_id = self.ticket.id
  82
+        pr.title = response.json['title']
  83
+        pr.save()
  84
+
  85
+        return HttpResponse(status=204)

0 notes on commit 06e95dc

Please sign in to comment.
Something went wrong with that request. Please try again.