diff mbox

[7/7] api: Expose events

Message ID 1445380847-22838-8-git-send-email-damien.lespiau@intel.com
State Rejected
Headers show

Commit Message

Damien Lespiau Oct. 20, 2015, 10:40 p.m. UTC
One interesting thing to be doing is polling for new events on a
project. Right now, the only event is series-new-revision, so listening
to events is really just listening to series creation and update.

This is quite useful for testing infrastructures, knowing when a series
is ready to be tested so a bot can retrieve its patches and test them.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 docs/api.rst             | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 patchwork/serializers.py |  8 +++++++-
 patchwork/urls.py        |  5 +++++
 patchwork/views/api.py   | 23 +++++++++++++++++++---
 4 files changed, 83 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/docs/api.rst b/docs/api.rst
index 7979ac4..64fedf4 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -354,9 +354,60 @@  Patches
                 "content": "<diff content>"
             }
 
+Events
+~~~~~~
+
+.. http:get:: /api/1.0/projects/(string: linkname)/events/
+.. http:get:: /api/1.0/projects/(int: project_id)/events/
+
+    List of events for this project.
+
+    .. sourcecode:: http
+
+        GET /api/1.0/patches/120/ HTTP/1.1
+        Accept: application/json
+
+    .. sourcecode:: http
+
+        HTTP/1.1 200 OK
+        Content-Type: application/json
+        Vary: Accept
+        Allow: GET, HEAD, OPTIONS
+
+        {
+            "count": 23,
+            "next": "http://127.0.0.1:8000/api/1.0/events/?page=2",
+            "previous": null,
+            "results": [
+                {
+                    "name": "series-new-revision",
+                    "event_time": "2015-10-20T19:49:49.494",
+                    "series": 23,
+                    "user": null
+                },
+                {
+                    "name": "series-new-revision",
+                    "event_time": "2015-10-20T19:49:43.895",
+                    "series": 22,
+                    "user": null
+                }
+            ]
+        }
+
+At the moment, only one event is listed:
+
+- **series-new-revision**: This event corresponds to patchwork receiving a
+  full new revision of a series, should it be the initial submission of
+  subsequent updates. The difference can be made by looking at the version of
+  the series.
+
 API Revisions
 ~~~~~~~~~~~~~
 
+**Revision 1**
+
+- Add /projects/${linkname}/events/ entry point.
+
 **Revision 0**
 
 - Initial revision. Basic objects exposed: api root, projects, series,
diff --git a/patchwork/serializers.py b/patchwork/serializers.py
index 418a140..4449534 100644
--- a/patchwork/serializers.py
+++ b/patchwork/serializers.py
@@ -19,7 +19,7 @@ 
 
 from django.contrib.auth.models import User
 from patchwork.models import Project, Series, SeriesRevision, Patch, Person, \
-                             State
+                             State, EventLog
 from rest_framework import serializers
 from enum import Enum
 
@@ -131,3 +131,9 @@  class RevisionSerializer(PatchworkModelSerializer):
         expand_serializers = {
             'patches': PatchSerializer,
         }
+
+class EventLogSerializer(serializers.ModelSerializer):
+    name = serializers.CharField(source='event.name', read_only=True)
+    class Meta:
+        model = EventLog
+        fields = ('name', 'event_time', 'series', 'user')
diff --git a/patchwork/urls.py b/patchwork/urls.py
index 3ba734e..c7b5664 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -33,6 +33,10 @@  project_router.register('projects', api.ProjectViewSet)
 series_list_router = routers.NestedSimpleRouter(project_router, 'projects',
                                                 lookup='project')
 series_list_router.register(r'series', api.SeriesListViewSet)
+# /projects/$project/events/
+event_router = routers.NestedSimpleRouter(project_router, 'projects',
+                                          lookup='project')
+event_router.register(r'events', api.EventLogViewSet)
 # /series/$id/
 series_router = routers.SimpleRouter()
 series_router.register(r'series', api.SeriesViewSet)
@@ -56,6 +60,7 @@  urlpatterns = patterns('',
     (r'^api/1.0/', include(series_router.urls)),
     (r'^api/1.0/', include(revisions_router.urls)),
     (r'^api/1.0/', include(patches_router.urls)),
+    (r'^api/1.0/', include(event_router.urls)),
 
     # project view:
     (r'^$', 'patchwork.views.projects'),
diff --git a/patchwork/views/api.py b/patchwork/views/api.py
index 01539b6..0f3ae55 100644
--- a/patchwork/views/api.py
+++ b/patchwork/views/api.py
@@ -17,7 +17,7 @@ 
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from patchwork.models import Project, Series, SeriesRevision, Patch
+from patchwork.models import Project, Series, SeriesRevision, Patch, EventLog
 from rest_framework import views, viewsets, mixins, generics, filters, permissions
 from rest_framework.decorators import api_view, renderer_classes, \
                                       permission_classes
@@ -25,10 +25,11 @@  from rest_framework.renderers import JSONRenderer
 from rest_framework.response import Response
 from rest_framework.generics import get_object_or_404
 from patchwork.serializers import ProjectSerializer, SeriesSerializer, \
-                                  RevisionSerializer, PatchSerializer
+                                  RevisionSerializer, PatchSerializer, \
+                                  EventLogSerializer
 
 
-API_REVISION = 0
+API_REVISION = 1
 
 class MaintainerPermission(permissions.BasePermission):
     def has_object_permission(self, request, view, obj):
@@ -125,3 +126,19 @@  class PatchViewSet(mixins.ListModelMixin,
     permission_classes = (MaintainerPermission, )
     queryset = Patch.objects.all()
     serializer_class = PatchSerializer
+
+class EventLogViewSet(mixins.ListModelMixin,
+                      ListMixin,
+                      viewsets.GenericViewSet):
+    permission_classes = (MaintainerPermission, )
+    queryset = EventLog.objects.all()
+    serializer_class = EventLogSerializer
+
+    def get_queryset(self):
+
+        pk = self.kwargs['project_pk']
+        if is_integer(pk):
+            queryset = self.queryset.filter(series__project__pk=pk)
+        else:
+            queryset = self.queryset.filter(series__project__linkname=pk)
+        return queryset