forked from metajack/commitbot
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitbot.py
More file actions
Latest commit
80 lines (62 loc) · 2.45 KB
/
Copy pathcommitbot.py
File metadata and controls
80 lines (62 loc) · 2.45 KB
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
fromtwisted.pythonimportlog
fromtwisted.webimportresource
fromtwisted.words.xishimportdomish
fromwokkel.subprotocolsimportXMPPHandler
fromwokkel.xmppimimportAvailablePresence, Presence
importsimplejsonasjson
NS_MUC='http://jabber.org/protocol/muc'
NS_XHTML_IM='http://jabber.org/protocols/xhtml-im'
NS_XHTML_W3C='http://www.w3.org/1999/xhtml'
classCommitBot(XMPPHandler):
def__init__(self, room, nick, password=None):
XMPPHandler.__init__(self)
self.room=room
self.nick=nick
self.password=password
defconnectionMade(self):
self.send(AvailablePresence())
# add handlers
# join room
pres=Presence()
pres['to'] =self.room+'/'+self.nick
x=pres.addElement((NS_MUC, 'x'))
ifnotself.passwordisNone:
x.addElement('password', content=self.password)
self.send(pres)
defnotify(self, data):
# build the messages
text= []
html= []
link=r"<a href='%s' name='%s'>%s</a>"
text.append('New commits in %s:\n'%data['repository']['url'])
html.append("New commits in " \
"<a href='%s'>%s</a>:<br/>"% \
(data['repository']['url'],
data['repository']['name']))
forcindata['commits']:
text.append('%s | %s | %s\n'% (c['message'],
c['author']['email'],
c['url']))
ltxt=link% (c['url'], c['id'], c['id'][:7])
html.append('%s | %s | %s<br />'% (c['message'],
c['author']['email'],
ltxt))
msg=domish.Element((None, 'message'))
msg['to'] =self.room
msg['type'] ='groupchat'
msg.addElement('body', content=''.join(text))
wrap=msg.addElement((NS_XHTML_IM, 'html'))
body=wrap.addElement((NS_XHTML_W3C, 'body'))
body.addRawXml(''.join(html))
self.send(msg)
classWebHook(resource.Resource):
isLeaf=True
def__init__(self, bot):
resource.Resource.__init__(self)
self.bot=bot
defrender_GET(self, req):
return"commitbot ready to rock!"
defrender_POST(self, req):
data=json.loads(req.args['payload'][0])
self.bot.notify(data)
return""