-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubversion.py
More file actions
248 lines (188 loc) · 5.96 KB
/
subversion.py
File metadata and controls
248 lines (188 loc) · 5.96 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
#!/usr/bin/python
import subprocess
import xml.etree.ElementTree
import json
import sys
import os
import requests
"""
SVN properties
"""
username = ""
password = ""
barnch_path = ""
"""
SLack properties
e.g. https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
"""
domain_url = ""
"""
Hipchat properties
"""
server = ""
token = ""
room = ""
"""
Paths
"""
pwd = os.getcwd()
rev_path_file = pwd + '/rev.txt'
log_path_file = pwd + '/temp/info.xml'
""" Programm """
class Logentry:pass
class Path:
def __init__(self, action, path):
self.action = action
self.path = path
class Payload:
def __init__(self, attachments):
self.attachments = attachments
class Attachment:pass
class SvnData:
def __init__(self, username, password, barnch_path):
self.username = username
self.password = password
self.barnch_path = barnch_path
def get_head_revision_number(self):
cmd = "svn info --username=" + self.username + " --password=" + self.password + " " + barnch_path + " | grep 'Revision' | awk '{print $2}'";
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
rev = proc.stdout.read()
return rev
def write_log_from_revision(self, revision):
cmd = 'svn log -v --username=' + self.username + ' --password=' + self.password + " " + barnch_path + ' -r'+str(revision)+':HEAD --xml >> '+ log_path_file
proc = subprocess.call(cmd, shell=True)
def proccessLogentry(logentry):
l = Logentry();
l.revision = logentry.get("revision");
l.author = logentry.find("author").text;
l.msg = logentry.find("msg").text;
l.date = logentry.find("date").text;
e_paths = logentry.find("paths");
e_list_paths = e_paths.findall("path")
paths = [];
for path in e_list_paths:
paths.append(Path(path.get("action"), path.text))
l.paths = paths;
return l;
def clean_and_write(f, text):
f.seek(0);
f.truncate();
f.write(text);
def create_payload(logentry):
ats = []
at = Attachment()
at.title = "Revision #" + logentry.revision
at.text = "*Author:*\t" + logentry.author + "\n\n" + logentry.msg
at.color = "#7CD197"
at.mrkdwn_in = ["text", "pretext"]
ats.append(at)
at_msg_files = Attachment()
at_msg_files.pretext = "Affected files:"
ats.append(at_msg_files)
for e_file in logentry.paths:
at_file = Attachment()
action = e_file.action
if action=='A':
at_file.color = "#7CD197"
elif action == 'M':
at_file.color = "#DE9E31"
elif action == 'D':
at_file.color = "#D50200"
at_file.text = e_file.path
ats.append(at_file)
payload = Payload(ats)
payload.text = "*New commit in " + logentry.date + "*"
return payload
def create_payload_for_hipchat(logentry):
message = "<b>New commit in " + logentry.date + "</b><br><br>"
message += "<b>Revision #" + logentry.revision + "</b><br>"
message += "<b>Author:</b>\t" + logentry.author + "<br><br>" + logentry.msg + "<br><br>"
message += "Affected files:<br>"
for e_file in logentry.paths:
action = e_file.action
message += "<span style='color:"
if action=='A':
message += "#7CD197"
elif action == 'M':
message += "#DE9E31"
elif action == 'D':
message += "#D50200"
message += "'>"+action+"</span>"
message += " " + e_file.path + "<br>"
return message
def hipchat_notify(message, color='yellow', notify=False,
format='html'):
"""Send notification to a HipChat room via API version 2
Parameters
----------
token : str
HipChat API version 2 compatible token (room or user token)
room: str
Name or API ID of the room to notify
message: str
Message to send to room
color: str, optional
Background color for message, defaults to yellow
Valid values: yellow, green, red, purple, gray, random
notify: bool, optional
Whether message should trigger a user notification, defaults to False
format: str, optional
Format of message, defaults to text
Valid values: text, html
host: str, optional
Host to connect to, defaults to api.hipchat.com
"""
if len(message) > 10000:
raise ValueError('Message too long')
if format not in ['text', 'html']:
raise ValueError("Invalid message format '{0}'".format(format))
if color not in ['yellow', 'green', 'red', 'purple', 'gray', 'random']:
raise ValueError("Invalid color {0}".format(color))
if not isinstance(notify, bool):
raise TypeError("Notify must be boolean")
url = "https://{0}/v2/room/{1}/notification".format(server, room)
headers = {'Content-type': 'application/json'}
headers['Authorization'] = "Bearer " + token
payload = {
'message': message,
'notify': notify,
'message_format': format,
'color': color
}
r = requests.post(url, data=json.dumps(payload), headers=headers)
r.raise_for_status()
def main():
svn = SvnData(username, password, barnch_path)
current_head_rev = svn.get_head_revision_number()
print "HEAD revision: " + current_head_rev
f = open(rev_path_file, 'r+w')
prev_rev = f.read()
if prev_rev=='':
clean_and_write(f, current_head_rev)
print "Initial action. Writing HEAD revision into the file"
sys.exit()
if prev_rev==current_head_rev:
print "No changes."
sys.exit()
else:
if os.path.isfile(log_path_file):
os.remove(log_path_file)
prev_rev=int(prev_rev)+1
svn.write_log_from_revision(prev_rev)
print "Chages have detected. Writing logs into XML"
clean_and_write(f, current_head_rev)
print "Start parsing XML"
e = xml.etree.ElementTree.parse(log_path_file).getroot();
logentries = e.findall("logentry");
data = [];
for logentry in logentries:
data.append(proccessLogentry(logentry));
for item in data:
payload = create_payload(item)
message = create_payload_for_hipchat(item)
print "Message is ready for sending."
print message
subprocess.call("curl -X POST --data-urlencode 'payload=" + json.dumps(payload, default=lambda o: o.__dict__) + "' " + domain_url +"", shell=True)
hipchat_notify(message)
print "Message has been sent to slack"
main()