52399779c6052e7a42bc7b14a365e4802418c6ed
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / commands / newcommitbot.py
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
30 import logging
31 import re
32
33 from webkitpy.common.config.committers import CommitterList
34 from webkitpy.common.system.executive import ScriptError
35 from webkitpy.tool.bot.irc_command import IRCCommand
36 from webkitpy.tool.bot.irc_command import Help
37 from webkitpy.tool.bot.irc_command import Hi
38 from webkitpy.tool.bot.irc_command import PingPong
39 from webkitpy.tool.bot.irc_command import Restart
40 from webkitpy.tool.bot.irc_command import YouThere
41 from webkitpy.tool.bot.ircbot import IRCBot
42 from webkitpy.tool.commands.queues import AbstractQueue
43 from webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
44
45 _log = logging.getLogger(__name__)
46
47
48 class Agent(object):
49     def __init__(self, tool, newcommitbot):
50         self._tool = tool
51         self._newcommitbot = newcommitbot
52
53     def name(self):
54         return 'WKR'
55
56
57 class NewCommitBot(AbstractQueue, StepSequenceErrorHandler):
58     name = "WKR"
59     watchers = AbstractQueue.watchers + ["rniwa@webkit.org"]
60
61     _commands = {
62         "hi": Hi,
63         "ping": PingPong,
64         "restart": Restart,
65         "yt?": YouThere,
66     }
67
68     _maximum_number_of_revisions_to_avoid_spamming_irc = 10
69
70     # AbstractQueue methods
71
72     def begin_work_queue(self):
73         AbstractQueue.begin_work_queue(self)
74         self._last_svn_revision = int(self._tool.scm().head_svn_revision())
75         self._irc_bot = IRCBot(self.name, self._tool, Agent(self._tool, self), self._commands)
76         self._tool.ensure_irc_connected(self._irc_bot.irc_delegate())
77
78     def work_item_log_path(self, failure_map):
79         return None
80
81     def next_work_item(self):
82         self._irc_bot.process_pending_messages()
83
84         _log.info('Last SVN revision: %d' % self._last_svn_revision)
85
86         return
87         count = 0
88         while count < self._maximum_number_of_revisions_to_avoid_spamming_irc:
89             new_revision = self._last_svn_revision + 1
90             try:
91                 commit_log = self._tool.executive.run_command(['svn', 'log', 'https://svn.webkit.org/repository/webkit/trunk', '--non-interactive', '--revision',
92                     self._tool.scm().strip_r_from_svn_revision(new_revision)])
93             except ScriptError:
94                 break
95
96             if commit_log.find('No such revision') >= 0:
97                 continue
98
99             self._last_svn_revision = new_revision
100             if self._is_empty_log(commit_log):
101                 continue
102
103             count += 1
104             _log.info('Found revision %d' % new_revision)
105             self._tool.irc().post(self._summarize_commit_log(commit_log).encode('utf-8'))
106
107     def _is_empty_log(self, commit_log):
108         return re.match(r'^\-+$', commit_log)
109
110     def process_work_item(self, failure_map):
111         return True
112
113     _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)
114     _rollout_regex = re.compile(r'(rolling out|reverting) (?P<revisions>r?\d+((,\s*|,?\s*and\s+)?r?\d+)*)\.?\s*', re.MULTILINE | re.IGNORECASE)
115     _requested_by_regex = re.compile(r'^\"?(?P<reason>.+?)\"? \(Requested\s+by\s+(?P<author>.+?)\s+on\s+#webkit\)\.', re.MULTILINE | re.IGNORECASE)
116     _bugzilla_url_regex = re.compile(r'http(s?)://bugs\.webkit\.org/show_bug\.cgi\?id=(?P<id>\d+)', re.MULTILINE)
117     _trac_url_regex = re.compile(r'http(s?)://trac.webkit.org/changeset/(?P<revision>\d+)', re.MULTILINE)
118
119     @classmethod
120     def _summarize_commit_log(self, commit_log, committer_list=CommitterList()):
121         patch_by = self._patch_by_regex.search(commit_log)
122         commit_log = self._patch_by_regex.sub('', commit_log, count=1)
123
124         rollout = self._rollout_regex.search(commit_log)
125         commit_log = self._rollout_regex.sub('', commit_log, count=1)
126
127         requested_by = self._requested_by_regex.search(commit_log)
128
129         commit_log = self._bugzilla_url_regex.sub(r'https://webkit.org/b/\g<id>', commit_log)
130         commit_log = self._trac_url_regex.sub(r'https://trac.webkit.org/r\g<revision>', commit_log)
131
132         for contributor in committer_list.contributors():
133             if not contributor.irc_nicknames:
134                 continue
135             name_with_nick = "%s (%s)" % (contributor.full_name, contributor.irc_nicknames[0])
136             if contributor.full_name in commit_log:
137                 commit_log = commit_log.replace(contributor.full_name, name_with_nick)
138                 for email in contributor.emails:
139                     commit_log = commit_log.replace(' <' + email + '>', '')
140             else:
141                 for email in contributor.emails:
142                     commit_log = commit_log.replace(email, name_with_nick)
143
144         lines = commit_log.split('\n')[1:-2]  # Ignore lines with ----------.
145
146         firstline = re.match(r'^(?P<revision>r\d+) \| (?P<email>[^\|]+) \| (?P<timestamp>[^|]+) \| [^\n]+', lines[0])
147         assert firstline
148         author = firstline.group('email')
149         if patch_by:
150             author = patch_by.group('author')
151
152         linkified_revision = 'https://trac.webkit.org/%s' % firstline.group('revision')
153         lines[0] = '%s by %s' % (linkified_revision, author)
154
155         if rollout:
156             if requested_by:
157                 author = requested_by.group('author')
158                 contributor = committer_list.contributor_by_irc_nickname(author)
159                 if contributor:
160                     author = "%s (%s)" % (contributor.full_name, contributor.irc_nicknames[0])
161                 return '%s rolled out %s in %s : %s' % (author, rollout.group('revisions'),
162                     linkified_revision, requested_by.group('reason'))
163             lines[0] = '%s rolled out %s in %s' % (author, rollout.group('revisions'), linkified_revision)
164
165         return ' '.join(filter(lambda line: len(line), lines)[0:4])
166
167     def handle_unexpected_error(self, failure_map, message):
168         _log.error(message)
169
170     # StepSequenceErrorHandler methods
171
172     @classmethod
173     def handle_script_error(cls, tool, state, script_error):
174         # Ideally we would post some information to IRC about what went wrong
175         # here, but we don't have the IRC password in the child process.
176         pass