The list of contributors in committers.py should be a separate JSON
[WebKit-https.git] / Tools / Scripts / webkitpy / common / config / committervalidator.py
1 # Copyright (c) 2009 Google Inc. All rights reserved.
2 # Copyright (c) 2009 Apple Inc. All rights reserved.
3 # Copyright (c) 2010 Research In Motion Limited. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 #     * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 from webkitpy.common.config import committers, urls
32
33
34 class CommitterValidator(object):
35     def __init__(self, host):
36         self.host = host
37
38     def _committers_py_path(self):
39         # extension can sometimes be .pyc, we always want .py
40         committers_path = self.host.filesystem.path_to_module(committers.__name__)
41         (path, extension) = self.host.filesystem.splitext(committers_path)
42         path = self.host.filesystem.relpath(path, self.host.scm().checkout_root)
43         return ".".join([path, "py"])
44
45     def _flag_permission_rejection_message(self, setter_email, flag_name):
46         # This could be queried from the tool.
47         queue_name = "commit-queue"
48         committers_list = self._committers_py_path()
49         message = "%s does not have %s permissions according to %s." % (
50                         setter_email,
51                         flag_name,
52                         urls.view_source_url(committers_list))
53         message += "\n\n- If you do not have %s rights please read %s for instructions on how to use bugzilla flags." % (
54                         flag_name, urls.contribution_guidelines)
55         message += "\n\n- If you have %s rights please correct the error in %s by adding yourself to the file (no review needed).  " % (
56                         flag_name, committers_list)
57         message += "The %s restarts itself every 2 hours.  After restart the %s will correctly respect your %s rights." % (
58                         queue_name, queue_name, flag_name)
59         return message
60
61     def _validate_setter_email(self, patch, result_key, rejection_function):
62         committer = getattr(patch, result_key)()
63         # If the flag is set, and we don't recognize the setter, reject the flag!
64         setter_email = patch._attachment_dictionary.get("%s_email" % result_key)
65         if setter_email and not committer:
66             rejection_function(patch.id(), self._flag_permission_rejection_message(setter_email, result_key))
67             return False
68         return True
69
70     def _reject_patch_if_flags_are_invalid(self, patch):
71         return (self._validate_setter_email(patch, "reviewer", self.reject_patch_from_review_queue)
72             and self._validate_setter_email(patch, "committer", self.reject_patch_from_commit_queue))
73
74     def patches_after_rejecting_invalid_commiters_and_reviewers(self, patches):
75         return [patch for patch in patches if self._reject_patch_if_flags_are_invalid(patch)]
76
77     def reject_patch_from_commit_queue(self,
78                                        attachment_id,
79                                        additional_comment_text=None):
80         comment_text = "Rejecting attachment %s from commit-queue." % attachment_id
81         if additional_comment_text:
82             comment_text += "\n\n%s" % additional_comment_text
83         self.host.bugs.set_flag_on_attachment(attachment_id,
84                                               "commit-queue",
85                                               "-",
86                                               comment_text)
87
88     def reject_patch_from_review_queue(self,
89                                        attachment_id,
90                                        additional_comment_text=None):
91         comment_text = "Rejecting attachment %s from review queue." % attachment_id
92         if additional_comment_text:
93             comment_text += "\n\n%s" % additional_comment_text
94         self.host.bugs.set_flag_on_attachment(attachment_id,
95                                               'review',
96                                               '-',
97                                               comment_text)