[ews-app] Add methods to update Patch fields
[WebKit-https.git] / Tools / BuildSlaveSupport / ews-app / ews / models / patch.py
1 # Copyright (C) 2018 Apple Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 from __future__ import unicode_literals
24
25 import logging
26
27 from django.db import models
28
29 from ews.config import ERR_EXISTING_PATCH, ERR_INVALID_PATCH_ID, ERR_NON_EXISTING_PATCH, SUCCESS
30
31 _log = logging.getLogger(__name__)
32
33
34 class Patch(models.Model):
35     patch_id = models.IntegerField(primary_key=True)
36     bug_id = models.IntegerField()
37     obsolete = models.BooleanField(default=False)
38     sent_to_buildbot = models.BooleanField(default=False)
39     created = models.DateTimeField(auto_now_add=True)
40     modified = models.DateTimeField(auto_now=True)
41
42     def __str__(self):
43         return str(self.patch_id)
44
45     @classmethod
46     def save_patch(cls, patch_id, bug_id=-1, obsolete=False, sent_to_buildbot=False):
47         if not Patch.is_valid_patch_id(patch_id):
48             return ERR_INVALID_PATCH_ID
49
50         if Patch.is_existing_patch_id(patch_id):
51             _log.info("Patch id {} already exists in database. Skipped saving.".format(patch_id))
52             return ERR_EXISTING_PATCH
53         Patch(patch_id, bug_id, obsolete, sent_to_buildbot).save()
54         _log.info('Saved patch in database, id: {}'.format(patch_id))
55         return SUCCESS
56
57     @classmethod
58     def save_patches(cls, patch_id_list):
59         for patch_id in patch_id_list:
60             Patch.save_patch(patch_id)
61
62     @classmethod
63     def is_valid_patch_id(cls, patch_id):
64         if not patch_id or type(patch_id) != int or patch_id < 1:
65             _log.warn('Invalid patch id: {}'.format(patch_id))
66             return False
67         return True
68
69     @classmethod
70     def is_existing_patch_id(cls, patch_id):
71         return bool(Patch.objects.filter(patch_id=patch_id))
72
73     @classmethod
74     def is_patch_sent_to_buildbot(cls, patch_id):
75         return Patch.is_existing_patch_id(patch_id) and Patch.objects.get(pk=patch_id).sent_to_buildbot
76
77     @classmethod
78     def set_sent_to_buildbot(cls, patch_id):
79         if not Patch.is_existing_patch_id(patch_id):
80             return ERR_NON_EXISTING_PATCH
81
82         patch = Patch.objects.get(pk=patch_id)
83         if patch.sent_to_buildbot:
84             _log.warn('Patch {} has already been sent to Buildbot.'.format(patch_id))
85             return SUCCESS
86
87         patch.sent_to_buildbot = True
88         patch.save()
89         _log.debug('Updated patch {} with sent_to_buildbot=True'.format(patch_id))
90         return SUCCESS
91
92     @classmethod
93     def set_bug_id(cls, patch_id, bug_id):
94         if not Patch.is_existing_patch_id(patch_id):
95             return ERR_NON_EXISTING_PATCH
96
97         patch = Patch.objects.get(pk=patch_id)
98         if patch.bug_id == bug_id:
99             _log.warn('Patch {} already has bug id {} set.'.format(patch_id, bug_id))
100             return SUCCESS
101
102         patch.bug_id = bug_id
103         patch.save()
104         _log.debug('Updated patch {} with bug id {}'.format(patch_id, bug_id))
105         return SUCCESS
106
107     @classmethod
108     def set_obsolete(cls, patch_id, obsolete=True):
109         if not Patch.is_existing_patch_id(patch_id):
110             return ERR_NON_EXISTING_PATCH
111
112         patch = Patch.objects.get(pk=patch_id)
113         if patch.obsolete == obsolete:
114             _log.warn('Patch {} is already marked with obsolete={}.'.format(patch_id, obsolete))
115             return SUCCESS
116         patch.obsolete = obsolete
117         patch.save()
118         _log.debug('Marked patch {} as obsolete={}'.format(patch_id, obsolete))
119         return SUCCESS