forked from atdt/gerrit-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerrit.py
106 lines (78 loc) · 2.71 KB
/
gerrit.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
gerrit.py
Get Gerrit events!
Ideas:
- Growl notification!
- RSS / Atom feed!
- Twitter / identica updates!
- Bullet points with exclamation marks!
Requires paramiko
Based on http://code.google.com/p/gerritbot/
Apache license.
:author: Ori Livneh <[email protected]>
"""
import ConfigParser
import Queue
import json
import logging
import threading
import time
import paramiko
queue = Queue.Queue()
# Logging
logging.basicConfig(level=logging.INFO)
logger = paramiko.util.logging.getLogger()
logger.setLevel(logging.INFO)
# Config
config = ConfigParser.ConfigParser()
config.read('gerrit.conf')
options = dict(timeout=60)
options.update(config.items('Gerrit'))
options['port'] = int(options['port'])
class GerritStream(threading.Thread):
"""Threaded job; listens for Gerrit events and puts them in a queue."""
def run(self):
while 1:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(**options)
client.get_transport().set_keepalive(60)
_, stdout, _ = client.exec_command('gerrit stream-events')
for line in stdout:
queue.put(json.loads(line))
except:
logging.exception('Gerrit error')
finally:
client.close()
time.sleep(5)
# For a description of the JSON structure of events emitted by Gerrit's
# stream-events command, see the Gerrit documentation.
# http://gerrit.googlecode.com/svn/documentation/2.1.2/cmd-stream-events.html
templates = {
'comment-added': ('Comment added ({0[author][name]}): "{0[comment]}" '
'[{0[change][project]}] - {0[change][url]}'),
'change-merged': ('Change merged ({0[submitter][name]}):'
'{0[change][subject]} [{0[change][project]}] - '
'{0[change][url]}'),
'patchset-added': ('Change merged ({0[submitter][name]}):'
'{0[change][subject]} [{0[change][project]}] - '
'{0[change][url]}'),
'change-abandoned': ('Change merged ({0[submitter][name]}):'
'{0[change][subject]} [{0[change][project]}] - '
'{0[change][url]}'),
}
gerrit = GerritStream()
gerrit.daemon = True
gerrit.start()
while 1:
event = queue.get()
# If you just want json output, ...
print event
# Or if you want something more human-readable
template = templates[event['type']]
print template.format(event)
gerrit.join()