1# Copyright (c) 2009 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
29import os
30
31from webkitpy.common.system.deprecated_logging import log
32from webkitpy.common.config.ports import WebKitPort
33from webkitpy.tool.bot.sheriff import Sheriff
34from webkitpy.tool.bot.sheriffircbot import SheriffIRCBot
35from webkitpy.tool.commands.queues import AbstractQueue
36from webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
37
38
39class SheriffBot(AbstractQueue, StepSequenceErrorHandler):
40    name = "sheriff-bot"
41    watchers = AbstractQueue.watchers + [
42        "abarth@webkit.org",
43        "eric@webkit.org",
44    ]
45
46    def _update(self):
47        self.run_webkit_patch(["update", "--force-clean", "--quiet"])
48
49    # AbstractQueue methods
50
51    def begin_work_queue(self):
52        AbstractQueue.begin_work_queue(self)
53        self._sheriff = Sheriff(self._tool, self)
54        self._irc_bot = SheriffIRCBot(self._tool, self._sheriff)
55        self._tool.ensure_irc_connected(self._irc_bot.irc_delegate())
56
57    def work_item_log_path(self, failure_map):
58        return None
59
60    def _is_old_failure(self, revision):
61        return self._tool.status_server.svn_revision(revision)
62
63    def next_work_item(self):
64        self._irc_bot.process_pending_messages()
65        self._update()
66
67        # FIXME: We need to figure out how to provoke_flaky_builders.
68
69        failure_map = self._tool.buildbot.failure_map()
70        failure_map.filter_out_old_failures(self._is_old_failure)
71        if failure_map.is_empty():
72            return None
73        return failure_map
74
75    def should_proceed_with_work_item(self, failure_map):
76        # Currently, we don't have any reasons not to proceed with work items.
77        return True
78
79    def process_work_item(self, failure_map):
80        failing_revisions = failure_map.failing_revisions()
81        for revision in failing_revisions:
82            builders = failure_map.builders_failing_for(revision)
83            tests = failure_map.tests_failing_for(revision)
84            try:
85                commit_info = self._tool.checkout().commit_info_for_revision(revision)
86                if not commit_info:
87                    print "FAILED to fetch CommitInfo for r%s, likely missing ChangeLog" % revision
88                    continue
89                self._sheriff.post_irc_warning(commit_info, builders)
90                self._sheriff.post_blame_comment_on_bug(commit_info, builders, tests)
91
92            finally:
93                for builder in builders:
94                    self._tool.status_server.update_svn_revision(revision, builder.name())
95        return True
96
97    def handle_unexpected_error(self, failure_map, message):
98        log(message)
99
100    # StepSequenceErrorHandler methods
101
102    @classmethod
103    def handle_script_error(cls, tool, state, script_error):
104        # Ideally we would post some information to IRC about what went wrong
105        # here, but we don't have the IRC password in the child process.
106        pass
107