[ews-app] Fetch bugs from Bugzilla and submit to Buildbot
[WebKit-https.git] / Tools / BuildSlaveSupport / ews-app / ews / fetcher.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 import logging
24
25 from ews.common.bugzilla import Bugzilla
26 from ews.common.buildbot import Buildbot
27 from ews.models.patch import Patch
28
29 _log = logging.getLogger(__name__)
30
31
32 class BugzillaPatchFetcher():
33     def fetch(self):
34         patch_ids = Bugzilla.get_list_of_patches_needing_reviews()
35         patch_ids = BugzillaPatchFetcher.filter_valid_patches(patch_ids)
36         _log.debug('r? patches: {}'.format(patch_ids))
37         Patch.save_patches(patch_ids)
38         patches_to_send = self.patches_to_send_to_buildbot(patch_ids)
39         _log.info('{} r? patches, {} patches need to be sent to Buildbot.'.format(len(patch_ids), len(patches_to_send)))
40
41         for patch_id in patches_to_send:
42             bz_patch = Bugzilla.retrieve_attachment(patch_id)
43             if not bz_patch or bz_patch['id'] != patch_id:
44                 _log.error('Unable to retrive patch "{}"'.format(patch_id))
45                 continue
46             if bz_patch.get('is_obsolete'):
47                 _log.warn('Patch is obsolete, skipping')
48                 Patch.set_obsolete(patch_id)
49                 continue
50             rc = Buildbot.send_patch_to_buildbot(bz_patch['path'], properties=['patch_id={}'.format(patch_id)])
51             if rc == 0:
52                 Patch.set_bug_id(patch_id, bz_patch['bug_id'])
53                 Patch.set_sent_to_buildbot(patch_id)
54             else:
55                 _log.error('Failed to send patch to buildbot.')
56                 #FIXME: send an email for this failure
57         return patch_ids
58
59     def patches_to_send_to_buildbot(self, patch_ids):
60         return [patch_id for patch_id in patch_ids if not Patch.is_patch_sent_to_buildbot(patch_id)]
61
62     @classmethod
63     def filter_valid_patches(cls, patch_ids):
64         return list(filter(lambda p: Patch.is_valid_patch_id(p), patch_ids))