Rename WebKitTools to Tools
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / bot / feeders.py
1 # Copyright (c) 2010 Google 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 are
5 # met:
6 #
7 #     * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #     * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #     * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 from webkitpy.common.config.committervalidator import CommitterValidator
30 from webkitpy.common.system.deprecated_logging import log
31 from webkitpy.tool.grammar import pluralize
32
33
34 class AbstractFeeder(object):
35     def __init__(self, tool):
36         self._tool = tool
37
38     def feed(self):
39         raise NotImplementedError("subclasses must implement")
40
41
42 class CommitQueueFeeder(AbstractFeeder):
43     queue_name = "commit-queue"
44
45     def __init__(self, tool):
46         AbstractFeeder.__init__(self, tool)
47         self.committer_validator = CommitterValidator(self._tool.bugs)
48
49     def _update_work_items(self, item_ids):
50         # FIXME: This is the last use of update_work_items, the commit-queue
51         # should move to feeding patches one at a time like the EWS does.
52         self._tool.status_server.update_work_items(self.queue_name, item_ids)
53         log("Feeding %s items %s" % (self.queue_name, item_ids))
54
55     def feed(self):
56         patches = self._validate_patches()
57         patches = sorted(patches, self._patch_cmp)
58         patch_ids = [patch.id() for patch in patches]
59         self._update_work_items(patch_ids)
60
61     def _patches_for_bug(self, bug_id):
62         return self._tool.bugs.fetch_bug(bug_id).commit_queued_patches(include_invalid=True)
63
64     def _validate_patches(self):
65         # Not using BugzillaQueries.fetch_patches_from_commit_queue() so we can reject patches with invalid committers/reviewers.
66         bug_ids = self._tool.bugs.queries.fetch_bug_ids_from_commit_queue()
67         all_patches = sum([self._patches_for_bug(bug_id) for bug_id in bug_ids], [])
68         return self.committer_validator.patches_after_rejecting_invalid_commiters_and_reviewers(all_patches)
69
70     def _patch_cmp(self, a, b):
71         # Sort first by is_rollout, then by attach_date.
72         # Reversing the order so that is_rollout is first.
73         rollout_cmp = cmp(b.is_rollout(), a.is_rollout())
74         if rollout_cmp != 0:
75             return rollout_cmp
76         return cmp(a.attach_date(), b.attach_date())
77
78
79 class EWSFeeder(AbstractFeeder):
80     def __init__(self, tool):
81         self._ids_sent_to_server = set()
82         AbstractFeeder.__init__(self, tool)
83
84     def feed(self):
85         ids_needing_review = set(self._tool.bugs.queries.fetch_attachment_ids_from_review_queue())
86         new_ids = ids_needing_review.difference(self._ids_sent_to_server)
87         log("Feeding EWS (%s, %s new)" % (pluralize("r? patch", len(ids_needing_review)), len(new_ids)))
88         for attachment_id in new_ids:  # Order doesn't really matter for the EWS.
89             self._tool.status_server.submit_to_ews(attachment_id)
90             self._ids_sent_to_server.add(attachment_id)