[ews-app] Handle incoming results data
authoraakash_jain@apple.com <aakash_jain@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Jan 2019 20:55:22 +0000 (20:55 +0000)
committeraakash_jain@apple.com <aakash_jain@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Jan 2019 20:55:22 +0000 (20:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193973

Reviewed by Lucas Forschler.

* BuildSlaveSupport/ews-app/ews/views/results.py:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@240674 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/BuildSlaveSupport/ews-app/ews/views/results.py
Tools/ChangeLog

index e3f7c5e..72b2fae 100644 (file)
 
 from __future__ import unicode_literals
 
+import json
+import logging
+
 from django.http import HttpResponse
 from django.shortcuts import render
 from django.views import View
+from django.views.decorators.csrf import csrf_exempt
+from django.utils.decorators import method_decorator
+
+from ews.models.build import Build
+from ews.models.step import Step
 
+_log = logging.getLogger(__name__)
 
+
+@method_decorator(csrf_exempt, name='dispatch')
 class Results(View):
-    def post(self, request, patch_id):
-        return HttpResponse("Placeholder to handle post request for result for patch: {}.".format(patch_id))
+    def post(self, request):
+        data = json.loads(request.body)
+
+        if data.get('type') == u'ews-build':
+            return self.build_event(data)
+
+        if data.get('type') == u'ews-step':
+            return self.step_event(data)
+
+        _log.error('Unexpected data type received: {}'.format(data.get('type')))
+        return HttpResponse('Unexpected data type received: {}'.format(data.get('type')))
+
+    def build_event(self, data):
+        if not self.is_valid_result(data):
+            return HttpResponse("Incomplete data.")
+
+        patch_id = data['patch_id']
+        if not patch_id or patch_id < 1:
+            return HttpResponse("Invalid patch id: {}.".format(patch_id))
+
+        Build.save_build(patch_id=int(patch_id), build_id=data['build_id'], builder_id=data['builder_id'], number=data['number'], result=data['result'],
+                   state_string=data['state_string'], started_at=data['started_at'], complete_at=data['complete_at'])
+        return HttpResponse("Saved data for patch: {}.\n".format(patch_id))
+
+    def step_event(self, data):
+        if not self.is_valid_result(data):
+            return HttpResponse("Incomplete data.")
+
+        Step.save_step(step_id=data['step_id'], build_id=data['build_id'], result=data['result'],
+                   state_string=data['state_string'], started_at=data['started_at'], complete_at=data['complete_at'])
+        return HttpResponse("Saved data for step: {}.\n".format(data['step_id']))
+
+    def is_valid_result(self, data):
+        if data['type'] not in [u'ews-build', u'ews-step']:
+            _log.error("Invalid data type: {}".format(data['type']))
+            return False
+
+        required_keys = {u'ews-build': ['patch_id', 'build_id', 'builder_id', 'number', 'result', 'state_string', 'started_at', 'complete_at'],
+                         u'ews-step': ['step_id', 'build_id', 'result', 'state_string', 'started_at', 'complete_at']}
+
+        for key in required_keys.get(data.get('type')):
+            if key not in data:
+                _log.warn('Data is missing {}'.format(key))
+                return False
+        return True
index e1ac0fe..543c685 100644 (file)
@@ -1,5 +1,14 @@
 2019-01-29  Aakash Jain  <aakash_jain@apple.com>
 
+        [ews-app] Handle incoming results data
+        https://bugs.webkit.org/show_bug.cgi?id=193973
+
+        Reviewed by Lucas Forschler.
+
+        * BuildSlaveSupport/ews-app/ews/views/results.py:
+
+2019-01-29  Aakash Jain  <aakash_jain@apple.com>
+
         [ews-app] Make the results url generic
         https://bugs.webkit.org/show_bug.cgi?id=193928