forked from mmueller/supybot-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
309 lines (272 loc) · 10.8 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
###
# Copyright (c) 2011, Mike Mueller <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Do whatever you want
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.schedule as schedule
import supybot.log as log
import supybot.world as world
import ConfigParser
import git
import os
import subprocess
import traceback
class Repository:
"Represents a git repository being monitored."
def __init__(self, repo_dir, long_name, config_values):
"""
Initialize with a repository with the given name and list of (name,
value) pairs from the config section.
"""
required_values = [ 'short name', 'url', 'channel' ]
optional_values = [ 'branch', 'commit link', 'commit message' ]
for name in required_values:
if not filter(lambda pair: pair[0] == name, config_values):
raise Exception('Section %s missing required value: %s' %
(long_name, name))
for name, value in config_values:
if name not in required_values and name not in optional_values:
raise Exception('Section %s contains unrecognized value: %s' %
(long_name, name))
self.long_name = long_name
self.branch = 'master'
self.commit_link = ''
self.commit_message = '[%s|%b|%a] %m'
self.last_commit = None
self.repo = None
for name, value in config_values:
self.__dict__[name.replace(' ', '_')] = value
self.branch = 'origin/' + self.branch
if not os.path.exists(repo_dir):
os.makedirs(repo_dir)
self.path = os.path.join(repo_dir, self.short_name)
self.clone()
def clone(self):
"If the repository doesn't exist on disk, clone it."
if not os.path.exists(self.path):
git.Git('.').clone(self.url, self.path, no_checkout=True)
self.repo = git.Repo(self.path)
self.last_commit = self.repo.commit(self.branch)
def fetch(self):
"Contact git repository and update last_commit appropriately."
self.repo.git.fetch()
def get_commit(self, sha):
"Fetch the commit with the given SHA. Returns None if not found."
try:
return self.repo.commit(sha)
except ValueError:
return None
def get_new_commits(self):
result = self.repo.iter_commits(self.branch + '..' + self.last_commit.name_rev, '', max_count=10)
self.last_commit = self.repo.commit(self.branch)
return result
def get_recent_commits(self, count):
return self.repo.iter_commits(self.branch, "", max_count=count)
def format_link(self, commit):
"Return a link to view a given commit, based on config setting."
result = ''
escaped = False
for c in self.commit_link:
if escaped:
if c == 'c':
result += commit.id[0:7]
elif c == 'C':
result += commit.id
else:
result += c
escaped = False
elif c == '%':
escaped = True
else:
result += c
return result
def format_message(self, commit):
"""
Generate an formatted message for IRC from the given commit, using
the format specified in the config. Returns a list of strings.
"""
MODE_NORMAL = 0
MODE_SUBST = 1
MODE_COLOR = 2
subst = {
'a': commit.author.name,
'b': self.branch[self.branch.rfind('/')+1:],
'c': commit.name_rev[0:7],
'C': commit.name_rev,
'e': commit.author.email,
'l': self.format_link(commit),
'm': commit.message.split('\n')[0],
'n': self.long_name,
's': self.short_name,
'u': self.url,
'd': commit.committed_date,
'!': '\x02',
'%': '%',
}
result = []
lines = self.commit_message.split('\n')
for line in lines:
mode = MODE_NORMAL
outline = ''
for c in line:
if mode == MODE_SUBST:
if c in subst.keys():
outline += subst[c]
mode = MODE_NORMAL
elif c == '(':
color = ''
mode = MODE_COLOR
else:
outline += c
mode = MODE_NORMAL
elif mode == MODE_COLOR:
if c == ')':
outline += '\x03' + color
mode = MODE_NORMAL
else:
color += c
elif c == '%':
mode = MODE_SUBST
else:
outline += c
result.append(outline)
return result
class Git(callbacks.PluginRegexp):
"Please see the README file to configure and use this plugin."
threaded = True
unaddressedRegexps = [ '_snarf' ]
def __init__(self, irc):
self.__parent = super(Git, self)
self.__parent.__init__(irc)
self._read_config()
self._start_polling()
def die(self):
self._stop_polling()
self.__parent.die()
def gitrehash(self, irc, msg, args):
"""(takes no arguments)
Reload the Git ini file and restart any period polling.
"""
self._stop_polling()
try:
self._read_config()
self._start_polling()
irc.replySuccess()
except Exception, e:
irc.reply('Error reloading config: ' + str(e))
def repolist(self, irc, msg, args, channel):
"""(takes no arguments)
Display the names of known repositories configured for this channel.
"""
repositories = filter(lambda r: r.channel == channel, self.repositories)
if not repositories:
irc.reply('No repositories configured for this channel.')
return
for r in repositories:
fmt = '\x02%(short_name)s\x02 (%(name)s, branch: %(branch)s)'
irc.reply(fmt % {
'branch': r.branch.split('/')[-1],
'name': r.long_name,
'short_name': r.short_name,
'url': r.url,
})
repolist = wrap(repolist, ['channel'])
def shortlog(self, irc, msg, args, channel, name, count):
"""<short name> [count]
Display the last commits on the named repository. [count] defaults to
1 if unspecified.
"""
matches = filter(lambda r: r.short_name == name, self.repositories)
if not matches:
irc.reply('No configured repository named %s.' % name)
return
# Enforce a modest privacy measure... don't let people probe the
# repository outside the designated channel.
repository = matches[0]
if channel != repository.channel:
irc.reply('Sorry, not allowed in this channel.')
return
commits = repository.get_recent_commits(count)
self._display_commits(irc, repository, commits)
shortlog = wrap(shortlog,
['channel', 'somethingWithoutSpaces', optional('int', 1)])
def _display_commits(self, irc, repository, commits):
"Display a nicely-formatted list of commits in a channel."
for commit in commits:
lines = repository.format_message(commit)
for line in lines:
msg = ircmsgs.privmsg(repository.channel, line)
irc.queueMsg(msg)
def _error(self, message):
log.error("Git: " + message)
def _poll(self):
for repository in self.repositories:
# Find the channel among IRC connections (first found will win)
ircs = [irc for irc in world.ircs
if repository.channel in irc.state.channels]
if not ircs:
self._error("Skipping poll on repository %s: not in channel: %s"
% (repository.long_name, repository.channel))
continue
irc = ircs[0]
try:
repository.fetch()
commits = repository.get_new_commits()
self._display_commits(irc, repository, commits)
except Exception, e:
self._error('Exception polling repository %s: %s' %
(repository.short_name, str(e)))
new_period = self.registryValue('pollPeriod')
if new_period != self.poll_period:
_stop_polling()
_start_polling()
def _read_config(self):
self.repositories = []
repo_dir = self.registryValue('repoDir')
parser = ConfigParser.RawConfigParser()
parser.read(self.registryValue('configFile'))
for section in parser.sections():
self.repositories.append(
Repository(repo_dir, section, parser.items(section)))
def _snarf(self, irc, msg, match):
r"""\b(?P<sha>[0-9a-f]{6,40})\b"""
sha = match.group('sha')
channel = msg.args[0]
repositories = filter(lambda r: r.channel == channel, self.repositories)
for repository in repositories:
commit = repository.get_commit(sha)
if commit:
self._display_commits(irc, repository, [commit])
break
def _start_polling(self):
self.poll_period = self.registryValue('pollPeriod')
if self.poll_period:
schedule.addPeriodicEvent(
self._poll, self.poll_period, now=False, name=self.name())
def _stop_polling(self):
if self.poll_period:
schedule.removeEvent(self.name())
Class = Git
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: