newcommitbot.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (c) 2012 Google Inc. All rights reserved.
  2. # Copyright (c) 2013 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. import logging
  30. import re
  31. from webkitpy.common.config.committers import CommitterList
  32. from webkitpy.common.system.executive import ScriptError
  33. from webkitpy.tool.bot.irc_command import IRCCommand
  34. from webkitpy.tool.bot.irc_command import Help
  35. from webkitpy.tool.bot.irc_command import Hi
  36. from webkitpy.tool.bot.irc_command import PingPong
  37. from webkitpy.tool.bot.irc_command import Restart
  38. from webkitpy.tool.bot.irc_command import YouThere
  39. from webkitpy.tool.bot.ircbot import IRCBot
  40. from webkitpy.tool.commands.queues import AbstractQueue
  41. from webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
  42. _log = logging.getLogger(__name__)
  43. class Agent(object):
  44. def __init__(self, tool, newcommitbot):
  45. self._tool = tool
  46. self._newcommitbot = newcommitbot
  47. def name(self):
  48. return 'WKR'
  49. class NewCommitBot(AbstractQueue, StepSequenceErrorHandler):
  50. name = "WKR"
  51. watchers = AbstractQueue.watchers + ["rniwa@webkit.org"]
  52. _commands = {
  53. "hi": Hi,
  54. "ping": PingPong,
  55. "restart": Restart,
  56. "yt?": YouThere,
  57. }
  58. _maximum_number_of_revisions_to_avoid_spamming_irc = 10
  59. # AbstractQueue methods
  60. def begin_work_queue(self):
  61. AbstractQueue.begin_work_queue(self)
  62. self._last_svn_revision = int(self._tool.scm().head_svn_revision())
  63. self._irc_bot = IRCBot(self.name, self._tool, Agent(self._tool, self), self._commands)
  64. self._tool.ensure_irc_connected(self._irc_bot.irc_delegate())
  65. def work_item_log_path(self, failure_map):
  66. return None
  67. def next_work_item(self):
  68. self._irc_bot.process_pending_messages()
  69. _log.info('Last SVN revision: %d' % self._last_svn_revision)
  70. count = 0
  71. while count < self._maximum_number_of_revisions_to_avoid_spamming_irc:
  72. new_revision = self._last_svn_revision + 1
  73. try:
  74. commit_log = self._tool.executive.run_command(['svn', 'log', 'https://svn.webkit.org/repository/webkit/trunk', '--non-interactive', '--revision',
  75. self._tool.scm().strip_r_from_svn_revision(new_revision)])
  76. except ScriptError:
  77. break
  78. self._last_svn_revision = new_revision
  79. if self._is_empty_log(commit_log):
  80. continue
  81. count += 1
  82. _log.info('Found revision %d' % new_revision)
  83. self._tool.irc().post(self._summarize_commit_log(commit_log).encode('utf-8'))
  84. def _is_empty_log(self, commit_log):
  85. return re.match(r'^\-+$', commit_log)
  86. def process_work_item(self, failure_map):
  87. return True
  88. _patch_by_regex = re.compile(r'^Patch\s+by\s+(?P<author>.+?)\s+on(\s+\d{4}-\d{2}-\d{2})?\n?', re.MULTILINE | re.IGNORECASE)
  89. _rollout_regex = re.compile(r'(rolling out|reverting) (?P<revisions>r?\d+((,\s*|,?\s*and\s+)?r?\d+)*)\.?\s*', re.MULTILINE | re.IGNORECASE)
  90. _requested_by_regex = re.compile(r'^\"?(?P<reason>.+?)\"? \(Requested\s+by\s+(?P<author>.+?)\s+on\s+#webkit\)\.', re.MULTILINE | re.IGNORECASE)
  91. _bugzilla_url_regex = re.compile(r'http(s?)://bugs\.webkit\.org/show_bug\.cgi\?id=(?P<id>\d+)', re.MULTILINE)
  92. _trac_url_regex = re.compile(r'http(s?)://trac.webkit.org/changeset/(?P<revision>\d+)', re.MULTILINE)
  93. @classmethod
  94. def _summarize_commit_log(self, commit_log, committer_list=CommitterList()):
  95. patch_by = self._patch_by_regex.search(commit_log)
  96. commit_log = self._patch_by_regex.sub('', commit_log, count=1)
  97. rollout = self._rollout_regex.search(commit_log)
  98. commit_log = self._rollout_regex.sub('', commit_log, count=1)
  99. requested_by = self._requested_by_regex.search(commit_log)
  100. commit_log = self._bugzilla_url_regex.sub(r'https://webkit.org/b/\g<id>', commit_log)
  101. commit_log = self._trac_url_regex.sub(r'https://trac.webkit.org/r\g<revision>', commit_log)
  102. for contributor in committer_list.contributors():
  103. if not contributor.irc_nicknames:
  104. continue
  105. name_with_nick = "%s (%s)" % (contributor.full_name, contributor.irc_nicknames[0])
  106. if contributor.full_name in commit_log:
  107. commit_log = commit_log.replace(contributor.full_name, name_with_nick)
  108. for email in contributor.emails:
  109. commit_log = commit_log.replace(' <' + email + '>', '')
  110. else:
  111. for email in contributor.emails:
  112. commit_log = commit_log.replace(email, name_with_nick)
  113. lines = commit_log.split('\n')[1:-2] # Ignore lines with ----------.
  114. firstline = re.match(r'^(?P<revision>r\d+) \| (?P<email>[^\|]+) \| (?P<timestamp>[^|]+) \| [^\n]+', lines[0])
  115. assert firstline
  116. author = firstline.group('email')
  117. if patch_by:
  118. author = patch_by.group('author')
  119. linkified_revision = 'https://trac.webkit.org/%s' % firstline.group('revision')
  120. lines[0] = '%s by %s' % (linkified_revision, author)
  121. if rollout:
  122. if requested_by:
  123. author = requested_by.group('author')
  124. contributor = committer_list.contributor_by_irc_nickname(author)
  125. if contributor:
  126. author = "%s (%s)" % (contributor.full_name, contributor.irc_nicknames[0])
  127. return '%s rolled out %s in %s : %s' % (author, rollout.group('revisions'),
  128. linkified_revision, requested_by.group('reason'))
  129. lines[0] = '%s rolled out %s in %s' % (author, rollout.group('revisions'), linkified_revision)
  130. return ' '.join(filter(lambda line: len(line), lines)[0:4])
  131. def handle_unexpected_error(self, failure_map, message):
  132. _log.error(message)
  133. # StepSequenceErrorHandler methods
  134. @classmethod
  135. def handle_script_error(cls, tool, state, script_error):
  136. # Ideally we would post some information to IRC about what went wrong
  137. # here, but we don't have the IRC password in the child process.
  138. pass