irc_command.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. import itertools
  29. import random
  30. import re
  31. from webkitpy.common.config import irc as config_irc
  32. from webkitpy.common.config import urls
  33. from webkitpy.common.config.committers import CommitterList
  34. from webkitpy.common.net.web import Web
  35. from webkitpy.common.system.executive import ScriptError
  36. from webkitpy.tool.bot.queueengine import TerminateQueue
  37. from webkitpy.tool.grammar import join_with_separators
  38. def _post_error_and_check_for_bug_url(tool, nicks_string, exception):
  39. tool.irc().post("%s" % exception)
  40. bug_id = urls.parse_bug_id(exception.output)
  41. if bug_id:
  42. bug_url = tool.bugs.bug_url_for_bug_id(bug_id)
  43. tool.irc().post("%s: Ugg... Might have created %s" % (nicks_string, bug_url))
  44. # FIXME: Merge with Command?
  45. class IRCCommand(object):
  46. usage_string = None
  47. help_string = None
  48. def execute(self, nick, args, tool, sheriff):
  49. raise NotImplementedError("subclasses must implement")
  50. @classmethod
  51. def usage(cls, nick):
  52. return "%s: Usage: %s" % (nick, cls.usage_string)
  53. @classmethod
  54. def help(cls, nick):
  55. return "%s: %s" % (nick, cls.help_string)
  56. class CreateBug(IRCCommand):
  57. usage_string = "create-bug BUG_TITLE"
  58. help_string = "Creates a Bugzilla bug with the given title."
  59. def execute(self, nick, args, tool, sheriff):
  60. if not args:
  61. return self.usage(nick)
  62. bug_title = " ".join(args)
  63. bug_description = "%s\nRequested by %s on %s." % (bug_title, nick, config_irc.channel)
  64. # There happens to be a committers list hung off of Bugzilla, so
  65. # re-using that one makes things easiest for now.
  66. requester = tool.bugs.committers.contributor_by_irc_nickname(nick)
  67. requester_email = requester.bugzilla_email() if requester else None
  68. try:
  69. bug_id = tool.bugs.create_bug(bug_title, bug_description, cc=requester_email, assignee=requester_email)
  70. bug_url = tool.bugs.bug_url_for_bug_id(bug_id)
  71. return "%s: Created bug: %s" % (nick, bug_url)
  72. except Exception, e:
  73. return "%s: Failed to create bug:\n%s" % (nick, e)
  74. class Help(IRCCommand):
  75. usage_string = "help [COMMAND]"
  76. help_string = "Provides help on my individual commands."
  77. def execute(self, nick, args, tool, sheriff):
  78. if args:
  79. for command_name in args:
  80. if command_name in commands:
  81. self._post_command_help(nick, tool, commands[command_name])
  82. else:
  83. tool.irc().post("%s: Available commands: %s" % (nick, ", ".join(sorted(visible_commands.keys()))))
  84. tool.irc().post('%s: Type "%s: help COMMAND" for help on my individual commands.' % (nick, sheriff.name()))
  85. def _post_command_help(self, nick, tool, command):
  86. tool.irc().post(command.usage(nick))
  87. tool.irc().post(command.help(nick))
  88. aliases = " ".join(sorted(filter(lambda alias: commands[alias] == command and alias not in visible_commands, commands)))
  89. if aliases:
  90. tool.irc().post("%s: Aliases: %s" % (nick, aliases))
  91. class Hi(IRCCommand):
  92. usage_string = "hi"
  93. help_string = "Responds with hi."
  94. def execute(self, nick, args, tool, sheriff):
  95. if len(args) and re.match(sheriff.name() + r'_*\s*!\s*', ' '.join(args)):
  96. return "%s: hi %s!" % (nick, nick)
  97. quips = tool.bugs.quips()
  98. quips.append('"Only you can prevent forest fires." -- Smokey the Bear')
  99. return random.choice(quips)
  100. class PingPong(IRCCommand):
  101. usage_string = "ping"
  102. help_string = "Responds with pong."
  103. def execute(self, nick, args, tool, sheriff):
  104. return nick + ": pong"
  105. class YouThere(IRCCommand):
  106. usage_string = "yt?"
  107. help_string = "Responds with yes."
  108. def execute(self, nick, args, tool, sheriff):
  109. return "%s: yes" % nick
  110. class Restart(IRCCommand):
  111. usage_string = "restart"
  112. help_string = "Restarts sherrifbot. Will update its WebKit checkout, and re-join the channel momentarily."
  113. def execute(self, nick, args, tool, sheriff):
  114. tool.irc().post("Restarting...")
  115. raise TerminateQueue()
  116. class RollChromiumDEPS(IRCCommand):
  117. usage_string = "roll-chromium-deps REVISION"
  118. help_string = "Rolls WebKit's Chromium DEPS to the given revision???"
  119. def execute(self, nick, args, tool, sheriff):
  120. if not len(args):
  121. return self.usage(nick)
  122. tool.irc().post("%s: Will roll Chromium DEPS to %s" % (nick, ' '.join(args)))
  123. tool.irc().post("%s: Rolling Chromium DEPS to %s" % (nick, ' '.join(args)))
  124. tool.irc().post("%s: Rolled Chromium DEPS to %s" % (nick, ' '.join(args)))
  125. tool.irc().post("%s: Thank You" % nick)
  126. class Rollout(IRCCommand):
  127. usage_string = "rollout SVN_REVISION [SVN_REVISIONS] REASON"
  128. help_string = "Opens a rollout bug, CCing author + reviewer, and attaching the reverse-diff of the given revisions marked as commit-queue=?."
  129. def _extract_revisions(self, arg):
  130. revision_list = []
  131. possible_revisions = arg.split(",")
  132. for revision in possible_revisions:
  133. revision = revision.strip()
  134. if not revision:
  135. continue
  136. revision = revision.lstrip("r")
  137. # If one part of the arg isn't in the correct format,
  138. # then none of the arg should be considered a revision.
  139. if not revision.isdigit():
  140. return None
  141. revision_list.append(int(revision))
  142. return revision_list
  143. def _parse_args(self, args):
  144. if not args:
  145. return (None, None)
  146. svn_revision_list = []
  147. remaining_args = args[:]
  148. # First process all revisions.
  149. while remaining_args:
  150. new_revisions = self._extract_revisions(remaining_args[0])
  151. if not new_revisions:
  152. break
  153. svn_revision_list += new_revisions
  154. remaining_args = remaining_args[1:]
  155. # Was there a revision number?
  156. if not len(svn_revision_list):
  157. return (None, None)
  158. # Everything left is the reason.
  159. rollout_reason = " ".join(remaining_args)
  160. return svn_revision_list, rollout_reason
  161. def _responsible_nicknames_from_revisions(self, tool, sheriff, svn_revision_list):
  162. commit_infos = map(tool.checkout().commit_info_for_revision, svn_revision_list)
  163. nickname_lists = map(sheriff.responsible_nicknames_from_commit_info, commit_infos)
  164. return sorted(set(itertools.chain(*nickname_lists)))
  165. def _nicks_string(self, tool, sheriff, requester_nick, svn_revision_list):
  166. # FIXME: _parse_args guarentees that our svn_revision_list is all numbers.
  167. # However, it's possible our checkout will not include one of the revisions,
  168. # so we may need to catch exceptions from commit_info_for_revision here.
  169. target_nicks = [requester_nick] + self._responsible_nicknames_from_revisions(tool, sheriff, svn_revision_list)
  170. return ", ".join(target_nicks)
  171. def _update_working_copy(self, tool):
  172. tool.scm().discard_local_changes()
  173. tool.executive.run_and_throw_if_fail(tool.deprecated_port().update_webkit_command(), quiet=True, cwd=tool.scm().checkout_root)
  174. def _check_diff_failure(self, error_log, tool):
  175. if not error_log:
  176. return None
  177. revert_failure_message_start = error_log.find("Failed to apply reverse diff for revision")
  178. if revert_failure_message_start == -1:
  179. return None
  180. lines = error_log[revert_failure_message_start:].split('\n')[1:]
  181. files = itertools.takewhile(lambda line: tool.filesystem.exists(tool.scm().absolute_path(line)), lines)
  182. if files:
  183. return "Failed to apply reverse diff for file(s): %s" % ", ".join(files)
  184. return None
  185. def execute(self, nick, args, tool, sheriff):
  186. svn_revision_list, rollout_reason = self._parse_args(args)
  187. if (not svn_revision_list or not rollout_reason):
  188. return self.usage(nick)
  189. revision_urls_string = join_with_separators([urls.view_revision_url(revision) for revision in svn_revision_list])
  190. tool.irc().post("%s: Preparing rollout for %s ..." % (nick, revision_urls_string))
  191. self._update_working_copy(tool)
  192. # FIXME: IRCCommand should bind to a tool and have a self._tool like Command objects do.
  193. # Likewise we should probably have a self._sheriff.
  194. nicks_string = self._nicks_string(tool, sheriff, nick, svn_revision_list)
  195. try:
  196. complete_reason = "%s (Requested by %s on %s)." % (
  197. rollout_reason, nick, config_irc.channel)
  198. bug_id = sheriff.post_rollout_patch(svn_revision_list, complete_reason)
  199. bug_url = tool.bugs.bug_url_for_bug_id(bug_id)
  200. tool.irc().post("%s: Created rollout: %s" % (nicks_string, bug_url))
  201. except ScriptError, e:
  202. tool.irc().post("%s: Failed to create rollout patch:" % nicks_string)
  203. diff_failure = self._check_diff_failure(e.output, tool)
  204. if diff_failure:
  205. return "%s: %s" % (nicks_string, diff_failure)
  206. _post_error_and_check_for_bug_url(tool, nicks_string, e)
  207. class Whois(IRCCommand):
  208. usage_string = "whois SEARCH_STRING"
  209. help_string = "Searches known contributors and returns any matches with irc, email and full name. Wild card * permitted."
  210. def _full_record_and_nick(self, contributor):
  211. result = ''
  212. if contributor.irc_nicknames:
  213. result += ' (:%s)' % ', :'.join(contributor.irc_nicknames)
  214. if contributor.can_review:
  215. result += ' (r)'
  216. elif contributor.can_commit:
  217. result += ' (c)'
  218. return unicode(contributor) + result
  219. def execute(self, nick, args, tool, sheriff):
  220. if not args:
  221. return self.usage(nick)
  222. search_string = unicode(" ".join(args))
  223. # FIXME: We should get the ContributorList off the tool somewhere.
  224. contributors = CommitterList().contributors_by_search_string(search_string)
  225. if not contributors:
  226. return unicode("%s: Sorry, I don't know any contributors matching '%s'.") % (nick, search_string)
  227. if len(contributors) > 5:
  228. return unicode("%s: More than 5 contributors match '%s', could you be more specific?") % (nick, search_string)
  229. if len(contributors) == 1:
  230. contributor = contributors[0]
  231. if not contributor.irc_nicknames:
  232. return unicode("%s: %s hasn't told me their nick. Boo hoo :-(") % (nick, contributor)
  233. return unicode("%s: %s is %s. Why do you ask?") % (nick, search_string, self._full_record_and_nick(contributor))
  234. contributor_nicks = map(self._full_record_and_nick, contributors)
  235. contributors_string = join_with_separators(contributor_nicks, only_two_separator=" or ", last_separator=', or ')
  236. return unicode("%s: I'm not sure who you mean? %s could be '%s'.") % (nick, contributors_string, search_string)
  237. # FIXME: Lame. We should have an auto-registering CommandCenter.
  238. visible_commands = {
  239. "create-bug": CreateBug,
  240. "help": Help,
  241. "hi": Hi,
  242. "ping": PingPong,
  243. "restart": Restart,
  244. "roll-chromium-deps": RollChromiumDEPS,
  245. "rollout": Rollout,
  246. "whois": Whois,
  247. "yt?": YouThere,
  248. }
  249. # Add revert as an "easter egg" command. Why?
  250. # revert is the same as rollout and it would be confusing to list both when
  251. # they do the same thing. However, this command is a very natural thing for
  252. # people to use and it seems silly to have them hunt around for "rollout" instead.
  253. commands = visible_commands.copy()
  254. commands["revert"] = Rollout
  255. # "hello" Alias for "hi" command for the purposes of testing aliases
  256. commands["hello"] = Hi