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
31import os
32
33from webkitpy.common.system.ospath import relpath
34from webkitpy.common.config import committers, urls
35
36
37class CommitterValidator(object):
38
39    def __init__(self, bugzilla):
40        self._bugzilla = bugzilla
41
42    def _checkout_root(self):
43        # FIXME: This is a hack, we would have this from scm.checkout_root
44        # if we had any way to get to an scm object here.
45        components = __file__.split(os.sep)
46        tools_index = components.index("Tools")
47        return os.sep.join(components[:tools_index])
48
49    def _committers_py_path(self):
50        # extension can sometimes be .pyc, we always want .py
51        (path, extension) = os.path.splitext(committers.__file__)
52        # FIXME: When we're allowed to use python 2.6 we can use the real
53        # os.path.relpath
54        path = relpath(path, self._checkout_root())
55        return ".".join([path, "py"])
56
57    def _flag_permission_rejection_message(self, setter_email, flag_name):
58        # This could be queried from the status_server.
59        queue_administrator = "eseidel@chromium.org"
60        # This could be queried from the tool.
61        queue_name = "commit-queue"
62        committers_list = self._committers_py_path()
63        message = "%s does not have %s permissions according to %s." % (
64                        setter_email,
65                        flag_name,
66                        urls.view_source_url(committers_list))
67        message += "\n\n- If you do not have %s rights please read %s for instructions on how to use bugzilla flags." % (
68                        flag_name, urls.contribution_guidelines)
69        message += "\n\n- If you have %s rights please correct the error in %s by adding yourself to the file (no review needed).  " % (
70                        flag_name, committers_list)
71        message += "The %s restarts itself every 2 hours.  After restart the %s will correctly respect your %s rights." % (
72                        queue_name, queue_name, flag_name)
73        return message
74
75    def _validate_setter_email(self, patch, result_key, rejection_function):
76        committer = getattr(patch, result_key)()
77        # If the flag is set, and we don't recognize the setter, reject the
78        # flag!
79        setter_email = patch._attachment_dictionary.get("%s_email" % result_key)
80        if setter_email and not committer:
81            rejection_function(patch.id(),
82                self._flag_permission_rejection_message(setter_email,
83                                                        result_key))
84            return False
85        return True
86
87    def _reject_patch_if_flags_are_invalid(self, patch):
88        return (self._validate_setter_email(
89                patch, "reviewer", self.reject_patch_from_review_queue)
90            and self._validate_setter_email(
91                patch, "committer", self.reject_patch_from_commit_queue))
92
93    def patches_after_rejecting_invalid_commiters_and_reviewers(self, patches):
94        return [patch for patch in patches if self._reject_patch_if_flags_are_invalid(patch)]
95
96    def reject_patch_from_commit_queue(self,
97                                       attachment_id,
98                                       additional_comment_text=None):
99        comment_text = "Rejecting attachment %s from commit-queue." % attachment_id
100        self._bugzilla.set_flag_on_attachment(attachment_id,
101                                              "commit-queue",
102                                              "-",
103                                              comment_text,
104                                              additional_comment_text)
105
106    def reject_patch_from_review_queue(self,
107                                       attachment_id,
108                                       additional_comment_text=None):
109        comment_text = "Rejecting attachment %s from review queue." % attachment_id
110        self._bugzilla.set_flag_on_attachment(attachment_id,
111                                              'review',
112                                              '-',
113                                              comment_text,
114                                              additional_comment_text)
115