[ews-app] Add method to save BuilderMapping to database
authoraakash_jain@apple.com <aakash_jain@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 17:21:24 +0000 (17:21 +0000)
committeraakash_jain@apple.com <aakash_jain@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 17:21:24 +0000 (17:21 +0000)
https://bugs.webkit.org/show_bug.cgi?id=194264

Reviewed by Lucas Forschler.

* BuildSlaveSupport/ews-app/ews/models/buildermapping.py:
(BuilderMapping.__str__):
(BuilderMapping):
(BuilderMapping.save_mapping):
(BuilderMapping.update_mapping):
(BuilderMapping.get_existing_mapping):
(BuilderMapping.is_valid_mapping):

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

Tools/BuildSlaveSupport/ews-app/ews/models/buildermapping.py
Tools/ChangeLog

index eeae731..dc61a27 100644 (file)
 
 from __future__ import unicode_literals
 
+import logging
+
 from django.db import models
+from ews.config import ERR_UNEXPECTED, SUCCESS
+import ews.common.util as util
+
+_log = logging.getLogger(__name__)
 
 
 class BuilderMapping(models.Model):
@@ -33,4 +39,47 @@ class BuilderMapping(models.Model):
     modified = models.DateTimeField(auto_now=True)
 
     def __str__(self):
-        return "{}: {}".format(self.builder_id, self.name)
+        return "{}: {}".format(self.builder_id, self.display_name)
+
+    @classmethod
+    def save_mapping(cls, builder_id, builder_name, display_name):
+        if not BuilderMapping.is_valid_mapping(builder_id, builder_name, display_name):
+            return ERR_UNEXPECTED
+
+        mapping = BuilderMapping.get_existing_mapping(builder_id)
+        if mapping:
+            # If the mapping is updated, e.g.: display name changed.
+            return BuilderMapping.update_mapping(mapping, builder_id, builder_name, display_name)
+
+        BuilderMapping(builder_id, builder_name, display_name).save()
+        _log.info('Saved mapping for builder_id: {}, name: {}, display_name: {}'.format(builder_id, builder_name, display_name))
+        return SUCCESS
+
+    @classmethod
+    def update_mapping(cls, mapping, builder_id, builder_name, display_name):
+        if mapping.builder_id != builder_id:
+            _log.error('builder_id {} does not match with builder_id {}. Ignoring new data.'.format(mapping.builder_id, builder_id))
+            return ERR_UNEXPECTED
+
+        if mapping.builder_name == builder_name and mapping.display_name == display_name:
+            _log.debug('Mapping already exist for builder_id: {}'.format(builder_id))
+            return SUCCESS
+
+        mapping.builder_name = builder_name
+        mapping.display_name = display_name
+        mapping.save(update_fields=['builder_name', 'display_name'])
+        _log.info('Updated mapping for builder_id: {}, name: {}, display_name: {}'.format(builder_id, builder_name, display_name))
+        return SUCCESS
+
+    @classmethod
+    def get_existing_mapping(cls, builder_id):
+        try:
+            return BuilderMapping.objects.get(builder_id=builder_id)
+        except:
+            return None
+
+    @classmethod
+    def is_valid_mapping(cls, builder_id, builder_name, display_name):
+        if not util.is_valid_id(builder_id):
+            return False
+        return True
index 9703ca3..5288226 100644 (file)
@@ -1,5 +1,20 @@
 2019-02-05  Aakash Jain  <aakash_jain@apple.com>
 
+        [ews-app] Add method to save BuilderMapping to database
+        https://bugs.webkit.org/show_bug.cgi?id=194264
+
+        Reviewed by Lucas Forschler.
+
+        * BuildSlaveSupport/ews-app/ews/models/buildermapping.py:
+        (BuilderMapping.__str__):
+        (BuilderMapping):
+        (BuilderMapping.save_mapping):
+        (BuilderMapping.update_mapping):
+        (BuilderMapping.get_existing_mapping):
+        (BuilderMapping.is_valid_mapping):
+
+2019-02-05  Aakash Jain  <aakash_jain@apple.com>
+
         [ews-app] Add timestamp fields for BuilderMappings
         https://bugs.webkit.org/show_bug.cgi?id=194266