FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
zcommit/zcommit.py at diffs · mit-scripts/zcommit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
mit-scripts
/
zcommit
Public
forked from
sipb/zcommit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
zcommit
/
zcommit.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
282 lines (251 loc) · 9.25 KB
Breadcrumbs
zcommit
/
zcommit.py
Copy path
File metadata and controls
executable file
·
282 lines (251 loc) · 9.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/python
import
cherrypy
from
flup
.
server
.
fcgi
import
WSGIServer
import
logging
import
json
import
os
import
posixpath
import
re
import
requests
import
subprocess
import
sys
import
traceback
import
dateutil
.
parser
import
zephyr
HERE
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
))
ZWRITE
=
os
.
path
.
join
(
HERE
,
'bin'
,
'zsend'
)
ZWRITE
=
'/usr/bin/zwrite'
LOG_FILENAME
=
'logs/zcommit.log'
MAX_DIFF_LINES
=
50
MAX_DIFFSTAT_WIDTH
=
80
MIN_DIFFSTAT_GRAPH_WIDTH
=
30
# Set up a specific logger with our desired output level
logger
=
logging
.
getLogger
(
__name__
)
logger
.
setLevel
(
logging
.
DEBUG
)
# Add the log message handler to the logger
handler
=
logging
.
FileHandler
(
LOG_FILENAME
)
logger
.
addHandler
(
handler
)
formatter
=
logging
.
Formatter
(
fmt
=
'%(levelname)-8s %(asctime)s %(message)s'
)
handler
.
setFormatter
(
formatter
)
def
zescape
(
input
):
return
(
input
.
replace
(
'@'
,
'@@'
)
.
replace
(
'}'
,
'@(})'
))
def
format_rename
(
old
,
new
):
prefix
=
posixpath
.
commonprefix
([
old
,
new
])
suffix
=
posixpath
.
commonprefix
([
old
[::
-
1
],
new
[::
-
1
]])[::
-
1
]
return
"%s{%s => %s}%s"
%
(
prefix
,
old
[
len
(
prefix
):
-
len
(
suffix
)],
new
[
len
(
prefix
):
-
len
(
suffix
)],
suffix
,
)
def
colorize_diff
(
diff
):
diff
=
zescape
(
diff
)
out
=
[]
in_diff
=
False
for
l
in
diff
.
splitlines
():
if
not
l
:
out
.
append
(
l
)
continue
color
=
None
if
l
[
0
]
==
'@'
:
in_diff
=
True
color
=
'cyan'
elif
in_diff
:
if
l
[
0
]
==
'+'
:
color
=
'green'
elif
l
[
0
]
==
'-'
:
color
=
'red'
elif
l
[
0
]
==
' '
:
out
.
append
(
l
)
continue
if
color
:
out
.
append
(
'@{@color{%s}%s}'
%
(
color
,
l
))
continue
in_diff
=
False
out
.
append
(
'@b{%s}'
%
(
l
,))
return
'
\n
'
.
join
(
out
)
def
format_commit
(
c
,
commit_url
):
info
=
{
'name'
:
zescape
(
c
[
'author'
][
'name'
]),
'email'
:
zescape
(
c
[
'author'
][
'email'
]),
'message'
:
zescape
(
c
[
'message'
]),
'timestamp'
:
dateutil
.
parser
.
parse
(
c
[
'timestamp'
]).
strftime
(
'%F %T %z'
),
'url'
:
zescape
(
c
[
'url'
])}
header
=
"""@{@color(yellow)%(url)s}
Author: %(name)s <%(email)s>
Date: %(timestamp)s
%(message)s
---
"""
%
info
try
:
if
not
commit_url
.
startswith
(
'https://api.github.com/'
):
raise
ValueError
(
'refusing to fetch commit information from '
+
commit_url
)
# Check if the diff is small enough
r
=
requests
.
get
(
commit_url
,
headers
=
{
'Accept'
:
'application/vnd.github.diff'
})
diff
=
r
.
text
if
len
(
diff
.
splitlines
())
<=
MAX_DIFF_LINES
:
return
header
+
colorize_diff
(
diff
)
# Otherwise, try to render a diffstat
r
=
requests
.
get
(
commit_url
)
commit_details
=
r
.
json
()
max_filename_len
=
0
max_changes_len
=
0
actions
=
[]
for
f
in
commit_details
[
'files'
]:
filename
=
f
[
'filename'
]
if
f
[
'status'
]
==
'renamed'
:
filename
=
format_rename
(
f
[
'previous_filename'
],
filename
)
max_filename_len
=
max
(
max_filename_len
,
len
(
filename
))
changes
=
str
(
f
[
'changes'
])
if
'patch'
not
in
f
:
changes
=
'Bin'
max_changes_len
=
max
(
max_changes_len
,
len
(
changes
))
actions
.
append
({
'filename'
:
filename
,
'changes'
:
changes
,
'status'
:
f
[
'status'
],
'additions'
:
f
[
'additions'
],
'deletions'
:
f
[
'deletions'
],
})
graph_width
=
max
(
MIN_DIFFSTAT_GRAPH_WIDTH
,
MAX_DIFFSTAT_WIDTH
-
(
max_filename_len
+
max_changes_len
+
3
))
lines
=
[]
for
a
in
actions
:
additions
=
a
[
'additions'
]
deletions
=
a
[
'deletions'
]
total
=
additions
+
deletions
if
additions
+
deletions
>
graph_width
:
additions
=
(
additions
*
graph_width
/
total
)
deletions
=
(
deletions
*
graph_width
/
total
)
additions
=
'+'
*
additions
if
additions
:
additions
=
'@{@color(green)%s}'
%
(
additions
,)
deletions
=
'-'
*
deletions
if
deletions
:
deletions
=
'@{@color(red)%s}'
%
(
deletions
,)
graph
=
additions
+
deletions
if
a
[
'changes'
]
==
'Bin'
:
graph
=
a
[
'status'
]
lines
.
append
(
'%-*s | %*s %s'
%
(
max_filename_len
,
a
[
'filename'
],
max_changes_len
,
a
[
'changes'
],
graph
,
))
return
header
+
'
\n
'
.
join
(
lines
)
except
:
logger
.
exception
(
'failed to fetch commit info from GitHub'
)
# If we can't get the diff, fall back on the list of changed files.
actions
=
[]
if
c
.
get
(
'added'
):
actions
.
extend
(
' A %s
\n
'
%
f
for
f
in
c
[
'added'
])
if
c
.
get
(
'removed'
):
actions
.
extend
(
' D %s
\n
'
%
f
for
f
in
c
[
'removed'
])
if
c
.
get
(
'modified'
):
actions
.
extend
(
' M %s
\n
'
%
f
for
f
in
c
[
'modified'
])
if
not
actions
:
actions
.
append
(
'Did not add/remove/modify any nonempty files.'
)
actions
=
''
.
join
(
actions
)
return
header
+
actions
def
send_zephyr
(
sender
,
klass
,
instance
,
zsig
,
msg
):
# TODO: spoof the sender
logger
.
info
(
"""About to send zephyr:
\"
\"
\"
sender: %(sender)s
class: %(klass)s
instance: %(instance)s
zsig: %(zsig)s
msg: %(msg)s
\"
\"
\"
"""
%
{
'sender'
:
sender
,
'klass'
:
klass
,
'instance'
:
instance
,
'zsig'
:
zsig
,
'msg'
:
msg
})
#z = zephyr.ZNotice()
#z.sender = sender
#z.cls = klass
#z.instance = instance
#z.fields = [ zsig, msg ]
#z.send()
cmd
=
[
ZWRITE
,
'-c'
,
klass
,
'-i'
,
instance
,
'-s'
,
zsig
,
'-d'
,
'-x'
,
'UTF-8'
,
'-m'
,
msg
]
output
=
subprocess
.
check_output
([
p
.
encode
(
'utf-8'
)
for
p
in
cmd
])
class
Application
(
object
):
@
cherrypy
.
expose
def
index
(
self
):
logger
.
debug
(
'Hello world app reached'
)
return
"""
<p> <i>Welcome to zcommit.</i> </p>
<p> zcommit allows you to send zephyr notifications by sending an HTTP
POST request to a URL. Currently zcommit supports POST-backs from
github. If you would like it to support another form of POST-back,
please let us know (zcommit@mit.edu). </p>
<h1> URL structure </h1>
The URL you post to is structured as follows:
<tt>http://zcommit.mit.edu/$type/$key1/$value1/$key2/$value2/...</tt>.
So for example, the URL
<tt>http://zcommit.mit.edu/github/class/zcommit/instance/commit</tt>
is parsed as having type <tt>github</tt>, class <tt>zcommit</tt>, and
instance <tt>commit</tt>. Using this information, zcommit figures out
how to form a useful message which is then sends as a zephyr.
<h1> Types </h1>
<h2> Github </h2>
Set your POST-back URL to
<tt>http://zcommit.mit.edu/github/class/$classname</tt>, followed by
any of the following optional key/value parameters:
<ul>
<li> <tt>/instance/$instance</tt> </li>
<li> <tt>/zsig/$zsig</tt> (sets the prefix of the zsig; the postfix is always the branch name) </li>
<li> <tt>/sender/$sender</tt> </li>
</ul>
"""
class
Github
(
object
):
@
cherrypy
.
expose
def
default
(
self
,
*
args
,
**
query
):
try
:
return
self
.
_default
(
*
args
,
**
query
)
except
Exception
,
e
:
logger
.
error
(
'Caught exception %s:
\n
%s'
%
(
e
,
traceback
.
format_exc
()))
raise
def
_default
(
self
,
*
args
,
**
query
):
logger
.
info
(
'A %s request with args: %r and query: %r'
%
(
cherrypy
.
request
.
method
,
args
,
query
))
opts
=
{}
if
len
(
args
)
%
2
:
raise
cherrypy
.
HTTPError
(
400
,
'Invalid submission URL'
)
logger
.
debug
(
'Passed validation'
)
for
i
in
xrange
(
0
,
len
(
args
),
2
):
opts
[
args
[
i
]]
=
unicode
(
args
[
i
+
1
],
'utf-8'
,
'replace'
)
logger
.
debug
(
'Set opts'
)
if
'class'
not
in
opts
:
raise
cherrypy
.
HTTPError
(
400
,
'Must specify a zephyr class name'
)
logger
.
debug
(
'Specified a class'
)
if
cherrypy
.
request
.
method
==
'POST'
:
logger
.
debug
(
'About to load data'
)
payload
=
json
.
loads
(
query
[
'payload'
])
logger
.
debug
(
'Loaded payload data'
)
zsig
=
payload
[
'ref'
]
if
'zsig'
in
opts
:
zsig
=
'%s: %s'
%
(
opts
[
'zsig'
],
zsig
)
sender
=
opts
.
get
(
'sender'
,
'daemon.zcommit'
)
logger
.
debug
(
'Set zsig'
)
for
c
in
payload
[
'commits'
]:
inst
=
opts
.
get
(
'instance'
,
c
[
'id'
][:
8
])
msg
=
format_commit
(
c
,
payload
[
'repository'
][
'commits_url'
].
replace
(
'{/sha}'
,
'/'
+
c
[
'id'
]))
send_zephyr
(
sender
,
opts
[
'class'
],
inst
,
zsig
,
msg
)
msg
=
'Thanks for posting!'
else
:
msg
=
(
'If you had sent a POST request to this URL, would have sent'
' a zephyr to -c %s'
%
opts
[
'class'
])
return
msg
github
=
Github
()
def
main
():
app
=
cherrypy
.
tree
.
mount
(
Application
(),
'/zcommit'
)
cherrypy
.
server
.
unsubscribe
()
cherrypy
.
engine
.
start
()
try
:
WSGIServer
(
app
,
environ
=
{
'SCRIPT_NAME'
:
'/zcommit'
}).
run
()
finally
:
cherrypy
.
engine
.
stop
()
if
__name__
==
'__main__'
:
sys
.
exit
(
main
())
Back
|
FazBrowse Home
|
New Git URL