We need a CIA replacement
[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.tool.bot.irc_command import IRCCommand
35 from webkitpy.tool.bot.irc_command import Help
36 from webkitpy.tool.bot.irc_command import Hi
37 from webkitpy.tool.bot.irc_command import Restart
38 from webkitpy.tool.bot.ircbot import IRCBot
39 from webkitpy.tool.commands.queues import AbstractQueue
40 from webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
41
42 _log = logging.getLogger(__name__)
43
44
45 class PingPong(IRCCommand):
46     def execute(self, nick, args, tool, sheriff):
47         return nick + ": pong"
48
49
50 class NewCommitBot(AbstractQueue, StepSequenceErrorHandler):
51     name = "new-commit-bot"
52     watchers = AbstractQueue.watchers + ["rniwa@webkit.org"]
53
54     _commands = {
55         "ping": PingPong,
56         "restart": Restart,
57     }
58
59     # AbstractQueue methods
60
61     def begin_work_queue(self):
62         AbstractQueue.begin_work_queue(self)
63         self._last_svn_revision = int(self._tool.scm().head_svn_revision())
64         self._irc_bot = IRCBot(self.name, self._tool, None, self._commands)
65         self._tool.ensure_irc_connected(self._irc_bot.irc_delegate())
66
67     def work_item_log_path(self, failure_map):
68         return None
69
70     def next_work_item(self):
71         self._irc_bot.process_pending_messages()
72
73         _log.info('Last SVN revision: %d' % self._last_svn_revision)
74
75         _log.info('Updating checkout')
76         self._update_checkout()
77
78         _log.info('Obtaining new SVN revisions')
79         revisions = self._new_svn_revisions()
80
81         _log.info('Obtaining commit logs for %d revisions' % len(revisions))
82         for revision in revisions:
83             commit_log = self._tool.scm().svn_commit_log(revision)
84             self._tool.irc().post(self._summarize_commit_log(commit_log))
85
86         return
87
88     def process_work_item(self, failure_map):
89         return True
90
91     def _update_checkout(self):
92         tool = self._tool
93         tool.executive.run_and_throw_if_fail(tool.deprecated_port().update_webkit_command(), quiet=True, cwd=tool.scm().checkout_root)
94
95     def _new_svn_revisions(self):
96         scm = self._tool.scm()
97         current_head = int(scm.head_svn_revision())
98         first_new_revision = self._last_svn_revision + 1
99         self._last_svn_revision = current_head
100         return range(max(first_new_revision, current_head - 20), current_head + 1)
101
102     _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)
103     _rollout_regex = re.compile(r'(rolling out|reverting) (?P<revisions>r?\d+((,\s*|,?\s*and\s+)?r?\d+)*)\.?', re.MULTILINE | re.IGNORECASE)
104     _requested_by_regex = re.compile(r'^\"?(?P<reason>.+?)\"? \(Requested\s+by\s+(?P<author>.+)\s+on\s+#webkit\)\.', re.MULTILINE | re.IGNORECASE)
105     _bugzilla_url_regex = re.compile(r'http(s?)://bugs\.webkit\.org/show_bug\.cgi\?id=(?P<id>\d+)', re.MULTILINE)
106     _trac_url_regex = re.compile(r'http(s?)://trac.webkit.org/changeset/(?P<revision>\d+)', re.MULTILINE)
107
108     @classmethod
109     def _summarize_commit_log(self, commit_log, committer_list=CommitterList()):
110         patch_by = self._patch_by_regex.search(commit_log)
111         commit_log = self._patch_by_regex.sub('', commit_log, count=1)
112
113         rollout = self._rollout_regex.search(commit_log)
114         commit_log = self._rollout_regex.sub('', commit_log, count=1)
115
116         requested_by = self._requested_by_regex.search(commit_log)
117
118         commit_log = self._bugzilla_url_regex.sub(r'https://webkit.org/b/\g<id>', commit_log)
119         commit_log = self._trac_url_regex.sub(r'https://trac.webkit.org/r\g<revision>', commit_log)
120
121         for contributor in committer_list.contributors():
122             if not contributor.irc_nicknames:
123                 continue
124             nickname = contributor.irc_nicknames[0]
125             commit_log = commit_log.replace(contributor.full_name, nickname)
126             for email in contributor.emails:
127                 commit_log = commit_log.replace(email, nickname)
128
129         lines = commit_log.split('\n')[1:-2]  # Ignore lines with ----------.
130
131         firstline = re.match(r'^(?P<revision>r\d+) \| (?P<email>[^\s\|]+) \| (?P<timestamp>[^|]+) \| [^\n]+', lines[0])
132         assert firstline
133         author = firstline.group('email')
134         if patch_by:
135             author = patch_by.group('author')
136
137         linkified_revision = 'https://trac.webkit.org/%s' % firstline.group('revision')
138         lines[0] = '%s by %s' % (linkified_revision, author)
139
140         if rollout:
141             if requested_by:
142                 return '%s rolled out %s in %s: %s' % (requested_by.group('author'), rollout.group('revisions'),
143                     linkified_revision, requested_by.group('reason'))
144             lines[0] = '%s rolled out %s in %s' % (author, rollout.group('revisions'), linkified_revision)
145
146         return '  '.join(filter(lambda line: len(line), lines)[0:4])
147
148     def handle_unexpected_error(self, failure_map, message):
149         _log.error(message)
150
151     # StepSequenceErrorHandler methods
152
153     @classmethod
154     def handle_script_error(cls, tool, state, script_error):
155         # Ideally we would post some information to IRC about what went wrong
156         # here, but we don't have the IRC password in the child process.
157         pass