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
29from webkitpy.common.config import urls
30from webkitpy.common.net.bugzilla import parse_bug_id
31from webkitpy.common.system.deprecated_logging import log
32from webkitpy.common.system.executive import ScriptError
33from webkitpy.tool.grammar import join_with_separators
34
35
36class Sheriff(object):
37    def __init__(self, tool, sheriffbot):
38        self._tool = tool
39        self._sheriffbot = sheriffbot
40
41    def post_irc_warning(self, commit_info, builders):
42        irc_nicknames = sorted([party.irc_nickname for
43                                party in commit_info.responsible_parties()
44                                if party.irc_nickname])
45        irc_prefix = ": " if irc_nicknames else ""
46        irc_message = "%s%s%s might have broken %s" % (
47            ", ".join(irc_nicknames),
48            irc_prefix,
49            urls.view_revision_url(commit_info.revision()),
50            join_with_separators([builder.name() for builder in builders]))
51
52        self._tool.irc().post(irc_message)
53
54    def post_rollout_patch(self, svn_revision_list, rollout_reason):
55        # Ensure that svn revisions are numbers (and not options to
56        # create-rollout).
57        try:
58            svn_revisions = " ".join([str(int(revision)) for revision in svn_revision_list])
59        except:
60            raise ScriptError(message="Invalid svn revision number \"%s\"."
61                              % " ".join(svn_revision_list))
62
63        if rollout_reason.startswith("-"):
64            raise ScriptError(message="The rollout reason may not begin "
65                              "with - (\"%s\")." % rollout_reason)
66
67        output = self._sheriffbot.run_webkit_patch([
68            "create-rollout",
69            "--force-clean",
70            # In principle, we should pass --non-interactive here, but it
71            # turns out that create-rollout doesn't need it yet.  We can't
72            # pass it prophylactically because we reject unrecognized command
73            # line switches.
74            "--parent-command=sheriff-bot",
75            svn_revisions,
76            rollout_reason,
77        ])
78        return parse_bug_id(output)
79
80    def post_blame_comment_on_bug(self, commit_info, builders, tests):
81        if not commit_info.bug_id():
82            return
83        comment = "%s might have broken %s" % (
84            urls.view_revision_url(commit_info.revision()),
85            join_with_separators([builder.name() for builder in builders]))
86        if tests:
87            comment += "\nThe following tests are not passing:\n"
88            comment += "\n".join(tests)
89        self._tool.bugs.post_comment_to_bug(commit_info.bug_id(),
90                                            comment,
91                                            cc=self._sheriffbot.watchers)
92