-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtcommit.py
More file actions
executable file
·299 lines (259 loc) · 8.42 KB
/
rtcommit.py
File metadata and controls
executable file
·299 lines (259 loc) · 8.42 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
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
#!/usr/local/bin/python
# Written by Joseph Huttner -- jhuttner@appnexus.com
# Licensed under the GNU General Public License
#
# Change Log
#
# Jan 7, 2011.
# First release. Commit against an RT ticket and have the
# commit message automatically populate with the RT subject.
#
# Jan 17
# Add support for use as Git subcommand
# Add support for rt#id=0
#
# Feb 7 -
# Add support for committing against multiple tickets in one commit.
#
# Feb 28
# "git rt" without ticket number uses ticket IDs from
# last time they were passed
#
# March 5
# Add history option with -b flag
# Usage "git rt -b 3" populates commit file w/ last 3 rt tix that were
# referenced in commit messages. (Does not work for rt=0)
#
# March 12
# Add xmpp capability. Requires Git post commit hook.
# Note: Once I switch off of git-svn the post commit hook will become a post
# update hook.
#
# March 13
# Make 'git rtcommit init' set everything up.
# Only import xmpp when necessary
#
# March 14
# Put recipients and message inside --blast. Separate with colon.
import base64
from datetime import datetime
import getopt
import os
import shlex
import subprocess
import sys
try:
import json
except:
import simplejson as json
xmpp = None
TMP_FILE = '/tmp/rtcommit'
RT_HISTORY_FILE = os.path.join(os.getcwd(), '.rtcommit/history.json')
BLAST_FILE = os.path.join(os.getcwd(), '.rtcommit/blast.json')
ALIAS_FILE = os.path.join(os.getenv('HOME'), 'xmpp-aliases.json')
XMPP_CONFIG_FILE = os.path.join(os.getenv('HOME'), 'xmpp-config.json')
################################################################################
# Helper functions
def read(path, default='', as_json=False):
try:
fobj = open(path, 'r')
data = fobj.read()
fobj.close()
if as_json:
data = json.loads(data)
return data
except IOError:
return default
except ValueError:
return default
def write(path, data, as_json=False):
fobj = open(path, 'w')
if as_json:
data = json.dumps(data)
fobj.write(data)
fobj.close()
################################################################################
# RT functions
def format_ticket_id(ticket_id):
return '00000' if int(ticket_id) == 0 else ticket_id
def get_ticket_subject(ticket_id):
if int(ticket_id) == 0:
return 'YourMessageHere'
else:
command = 'rt show -t ticket ' + ticket_id + ' -f subject,id'
subprocess.call(shlex.split(command), stdout=file(TMP_FILE, 'w'))
subject = open(TMP_FILE).readline().split(":", 1)[1].strip()
return subject
def make_tmp_commit_file(ticket_ids):
# TODO - make the commit msg formatted correctly
commit_msg_parts = []
for tid in ticket_ids:
formatted_tid = format_ticket_id(str(tid));
msg = " ".join(['#rt#' + formatted_tid + ':', get_ticket_subject(tid)])
commit_msg_parts.append(msg)
commit_msg = '\n'.join(commit_msg_parts)
write(TMP_FILE, commit_msg)
def update_history_file(current, newest):
if not current:
result = newest
else:
for _id in reversed(newest):
if current[0] != _id and id != 0:
current.insert(0, _id)
result = current
write(RT_HISTORY_FILE, result, True)
def exec_git_commit():
command = 'git commit -a --edit -F /tmp/rtcommit'
subprocess.call(shlex.split(command))
################################################################################
# Blast functions
class Blast(object):
def __init__(self):
self.aliases = read(ALIAS_FILE, {}, True)
self.blasts = read(BLAST_FILE, [], True)
self.xmpp_config = read(XMPP_CONFIG_FILE, as_json=True)
def not_at_no_op(self):
return self.blasts and 'no_op' not in self.blasts[0]
def store_blast(self, blast):
if blast.find(':') != -1:
to, msg = blast.split(':', 1)
else:
to = blast
msg = ''
to = [s.strip() for s in to.split(',')]
timestamp = datetime.utcnow()
new_blast = {
'to': to,
'msg': msg.strip(),
'timestamp': str(timestamp),
}
curr_blasts = read(BLAST_FILE, [], True)
curr_blasts.insert(0, new_blast)
write(BLAST_FILE, curr_blasts, True)
def xmpp_connect(self):
client = self.xmpp_config['client']
server = self.xmpp_config['server']
port = self.xmpp_config['port']
username = self.xmpp_config['username']
password = base64.b64decode(self.xmpp_config['password'])
self.cnx = xmpp.Client(client, debug=[])
# Turn on debugging
#self.cnx = xmpp.Client(client)
self.cnx.connect(server=(server, port))
self.cnx.auth(username, password, 'botty')
def _get_alias_type(self, alias):
if alias in self.aliases['group']:
return 'group'
elif alias in self.aliases['user']:
return 'user'
return 'unknown_type'
def _send_group_blast(self, room, blast):
nickname = self.xmpp_config['nickname']
self.cnx.send(xmpp.Presence(to="%s/%s" % (room, nickname)))
msg = xmpp.protocol.Message(body=blast)
msg.setTo(room)
msg.setType('groupchat')
self.cnx.send(msg)
def _send_user_blast(self, to, content):
msg = xmpp.Message(to, content)
self.cnx.send(msg)
def store_no_op(self):
"""No-op prohibits blast from executing."""
curr_blasts = read(BLAST_FILE, [], True)
curr_blasts.insert(0, {'no_op': 1})
write(BLAST_FILE, curr_blasts, True)
def get_git_commit_info(self):
command = 'git log -n 1'
subprocess.call(shlex.split(command), stdout=file(TMP_FILE, 'w'))
data = open(TMP_FILE).read()
return data
def send_blast(self):
xmpp_targets = {
'group': [],
'user': [],
'unknown_type': [],
}
if self.not_at_no_op():
blast = self.blasts[0]
for recipient in blast['to']:
_type = self._get_alias_type(recipient)
if _type in ['group', 'user']:
full = self.aliases[_type][recipient]
xmpp_targets[_type].append(full)
else:
xmpp_targets[_type].append(recipient)
parts = [blast['msg'], '', self.get_git_commit_info()]
msg = '\n'.join(parts)
for r in xmpp_targets['group']:
self._send_group_blast(r, msg)
# users.append(current_user) ?
# treat the unknowns as users
for r in xmpp_targets['user'] + xmpp_targets['unknown_type']:
self._send_user_blast(r, msg)
################################################################################
# Initialization functions
def is_initialized():
look_for = os.path.join(os.getcwd(), '.rtcommit')
return os.path.exists(look_for)
def initialize():
if is_initialized():
return 'Error. Directory already exists.'
else:
def prepend_cwd(*items):
"""Build up paths with os.getcwd more easily."""
items = list(items)
items.insert(0, os.getcwd())
return os.path.join(*items)
history_parts = ['touch', prepend_cwd('.rtcommit', 'history.json')]
blast_parts = ['touch', prepend_cwd('.rtcommit', 'blast.json')]
mkdir_cmd = ' '.join(['mkdir', prepend_cwd('.rtcommit')])
init_history_file_cmd = ' '.join(history_parts)
init_blast_file_cmd = ' '.join(blast_parts)
subprocess.call(shlex.split(mkdir_cmd))
subprocess.call(shlex.split(init_history_file_cmd))
subprocess.call(shlex.split(init_blast_file_cmd))
return 'RT Commit library initialized successfully. To remove, \
\'rm -rf .rtcommit\''
################################################################################
# Main function
def main(argv):
optlist, args = getopt.gnu_getopt(argv[0:], 'p:', ['blast=', 'send-blast'])
optlist = dict(optlist)
if 'init' in args:
result = initialize()
print result
return
else:
try:
assert is_initialized()
except AssertionError:
print 'You must initialize RT Commit with \'git rtcommit init\' \
before running that command.'
return
if '--send-blast' in optlist:
instance = Blast()
if instance.not_at_no_op():
global xmpp
xmpp = __import__('xmpp')
instance.xmpp_connect()
instance.send_blast()
instance.store_no_op()
return
if '--blast' in optlist:
instance = Blast()
blast = optlist['--blast']
instance.store_blast(blast)
cl_ticket_ids = args
history = read(RT_HISTORY_FILE, as_json=True)
if '-p' in optlist:
# Need 'map' because unicode breaks everything
from_history = map(str, history[:int(optlist['-p'])])
else:
from_history = []
ticket_ids = cl_ticket_ids + from_history
if ticket_ids:
make_tmp_commit_file(ticket_ids)
update_history_file(history, cl_ticket_ids)
exec_git_commit()
if __name__ == '__main__':
main(sys.argv[1:])