tizen beta release
[profile/ivi/webkit-efl.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)
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 = self._patches_with_acceptable_review_flag(patches)
58         patches = sorted(patches, self._patch_cmp)
59         patch_ids = [patch.id() for patch in patches]
60         self._update_work_items(patch_ids)
61
62     def _patches_for_bug(self, bug_id):
63         return self._tool.bugs.fetch_bug(bug_id).commit_queued_patches(include_invalid=True)
64
65     # Filters out patches with r? or r-, only r+ or no review are OK to land.
66     def _patches_with_acceptable_review_flag(self, patches):
67         return [patch for patch in patches if patch.review() in [None, '+']]
68
69     def _validate_patches(self):
70         # Not using BugzillaQueries.fetch_patches_from_commit_queue() so we can reject patches with invalid committers/reviewers.
71         bug_ids = self._tool.bugs.queries.fetch_bug_ids_from_commit_queue()
72         all_patches = sum([self._patches_for_bug(bug_id) for bug_id in bug_ids], [])
73         return self.committer_validator.patches_after_rejecting_invalid_commiters_and_reviewers(all_patches)
74
75     def _patch_cmp(self, a, b):
76         # Sort first by is_rollout, then by attach_date.
77         # Reversing the order so that is_rollout is first.
78         rollout_cmp = cmp(b.is_rollout(), a.is_rollout())
79         if rollout_cmp != 0:
80             return rollout_cmp
81         return cmp(a.attach_date(), b.attach_date())
82
83
84 class EWSFeeder(AbstractFeeder):
85     def __init__(self, tool):
86         self._ids_sent_to_server = set()
87         AbstractFeeder.__init__(self, tool)
88
89     def feed(self):
90         ids_needing_review = set(self._tool.bugs.queries.fetch_attachment_ids_from_review_queue())
91         new_ids = ids_needing_review.difference(self._ids_sent_to_server)
92         log("Feeding EWS (%s, %s new)" % (pluralize("r? patch", len(ids_needing_review)), len(new_ids)))
93         for attachment_id in new_ids:  # Order doesn't really matter for the EWS.
94             self._tool.status_server.submit_to_ews(attachment_id)
95             self._ids_sent_to_server.add(attachment_id)