WebKitTools/Scripts/webkitpy/tool/bot/irc_command.py
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 import random
       
    30 import webkitpy.common.config.irc as config_irc
       
    31 
       
    32 from webkitpy.common.checkout.changelog import view_source_url
       
    33 from webkitpy.tool.bot.queueengine import TerminateQueue
       
    34 from webkitpy.common.net.bugzilla import parse_bug_id
       
    35 from webkitpy.common.system.executive import ScriptError
       
    36 
       
    37 # FIXME: Merge with Command?
       
    38 class IRCCommand(object):
       
    39     def execute(self, nick, args, tool, sheriff):
       
    40         raise NotImplementedError, "subclasses must implement"
       
    41 
       
    42 
       
    43 class LastGreenRevision(IRCCommand):
       
    44     def execute(self, nick, args, tool, sheriff):
       
    45         return "%s: %s" % (nick,
       
    46             view_source_url(tool.buildbot.last_green_revision()))
       
    47 
       
    48 
       
    49 class Restart(IRCCommand):
       
    50     def execute(self, nick, args, tool, sheriff):
       
    51         tool.irc().post("Restarting...")
       
    52         raise TerminateQueue()
       
    53 
       
    54 
       
    55 class Rollout(IRCCommand):
       
    56     def execute(self, nick, args, tool, sheriff):
       
    57         if len(args) < 2:
       
    58             tool.irc().post("%s: Usage: SVN_REVISION REASON" % nick)
       
    59             return
       
    60         svn_revision = args[0].lstrip("r")
       
    61         rollout_reason = " ".join(args[1:])
       
    62         tool.irc().post("Preparing rollout for r%s..." % svn_revision)
       
    63         try:
       
    64             complete_reason = "%s (Requested by %s on %s)." % (
       
    65                 rollout_reason, nick, config_irc.channel)
       
    66             bug_id = sheriff.post_rollout_patch(svn_revision, complete_reason)
       
    67             bug_url = tool.bugs.bug_url_for_bug_id(bug_id)
       
    68             tool.irc().post("%s: Created rollout: %s" % (nick, bug_url))
       
    69         except ScriptError, e:
       
    70             tool.irc().post("%s: Failed to create rollout patch:" % nick)
       
    71             tool.irc().post("%s" % e)
       
    72             bug_id = parse_bug_id(e.output)
       
    73             if bug_id:
       
    74                 tool.irc().post("Ugg...  Might have created %s" %
       
    75                     tool.bugs.bug_url_for_bug_id(bug_id))
       
    76 
       
    77 
       
    78 class Help(IRCCommand):
       
    79     def execute(self, nick, args, tool, sheriff):
       
    80         return "%s: Available commands: %s" % (nick, ", ".join(commands.keys()))
       
    81 
       
    82 
       
    83 class Hi(IRCCommand):
       
    84     def execute(self, nick, args, tool, sheriff):
       
    85         quips = tool.bugs.quips()
       
    86         quips.append('"Only you can prevent forest fires." -- Smokey the Bear')
       
    87         return random.choice(quips)
       
    88 
       
    89 
       
    90 class Eliza(IRCCommand):
       
    91     therapist = None
       
    92 
       
    93     def __init__(self):
       
    94         if not self.therapist:
       
    95             import webkitpy.thirdparty.autoinstalled.eliza as eliza
       
    96             Eliza.therapist = eliza.eliza()
       
    97 
       
    98     def execute(self, nick, args, tool, sheriff):
       
    99         return "%s: %s" % (nick, self.therapist.respond(" ".join(args)))
       
   100 
       
   101 
       
   102 # FIXME: Lame.  We should have an auto-registering CommandCenter.
       
   103 commands = {
       
   104     "last-green-revision": LastGreenRevision,
       
   105     "restart": Restart,
       
   106     "rollout": Rollout,
       
   107     "help": Help,
       
   108     "hi": Hi,
       
   109 }