-
Notifications
You must be signed in to change notification settings - Fork 2
/
post-commit
executable file
·204 lines (144 loc) · 5.76 KB
/
post-commit
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
#!/usr/bin/env python
import httplib, mimetools, re, svn.core, svn.fs, svn.repos, sys, urllib, urlparse
from lxml.etree import ElementTree, HTMLParser
from lxml.cssselect import CSSSelector
# Create HTTP connection from URL scheme, host, and port
def http_connection(url):
components = list(urlparse.urlparse(url))
if 'https' == components[0]:
return httplib.HTTPSConnection(components[1])
return httplib.HTTPConnection(components[1])
# Depending on the method, add params to the URL or the request body and set
# any headers. An HTTP request object would be nice here, but need to remember
# this is just a quick script to close Google project hosting issues : )
def encode_params(method, url, params):
body = None
headers = {}
if 'POST' == method.upper():
headers['Content-Type'] = 'application/x-www-form-urlencoded'
body = urllib.urlencode(params)
else:
components = list(urlparse.urlparse(url))
components[4] = urllib.urlencode(params)
url = urlparse.urlunparse(components)
return url, headers, body
def encode_form(method, url, params, form):
url = urlparse.urljoin(url, form.get('action'))
url, headers, body = encode_params(method, url, params)
# Encode multipart/form-data
if 'multipart/form-data' == form.get('enctype'):
boundary = mimetools.choose_boundary()
headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary
components = []
for name, value in params:
components.append('--' + boundary)
components.append('Content-Disposition: form-data; name="' + name + '"')
components.append('')
components.append(value)
components.append('--' + boundary + '--')
body = '\r\n'.join(components)
return url, headers, body
# Check arguments and print usage
if 6 != len(sys.argv):
print """
%s requires five arguments,
[1] REPOS-PATH (the path to the repository)
[2] REV (the number of the revision)
[3] EMAIL (the Google account email)
[4] PASSWORD (the Google account password)
[5] PROJECT (the Google project hosting project)
""" % sys.argv[0]
sys.exit(1)
# Open Subversion repository
repos = svn.repos.open(sys.argv[1])
fs = svn.repos.fs(repos)
# Get commit log
log = svn.fs.revision_prop(fs, int(sys.argv[2]), svn.core.SVN_PROP_REVISION_LOG)
# Find magic words in commit log, e.g.
#
# * Closes issue 123
# * Fixes issue 123
# * Closes issues 123 and 321
# * Closes issue 123 and 321
# NOT Closes issues 123
# * Closes issues 123, 321, and 456
# * Closes issues 123, 321 and 456
# * Closes issue 123 and issue 321
# NOT Closes issues 123 and issue 321
# TODO Closes issues 123, 321, and issue 456
matches = re.findall('(?:closes|fixes)\\s+issue(?:s?\\s+#?\\d+(?:,?\\s+(?:(?:&|and)\\s+)?#?\\d+)+|\\s+#?\\d+(?:,?\\s+(?:(?:&|and)\\s+)?issue\s+#?\\d+)*)', log, re.I)
if matches:
# Login to Google account
method = 'GET'
url = 'https://www.google.com/accounts/ServiceLogin'
params = [('service', 'code')]
url, headers, body = encode_params(method, url, params)
conn = http_connection(url)
conn.request(method, url, body, headers)
response = conn.getresponse()
# Get cookie
cookie = response.getheader('Set-Cookie')
parser = HTMLParser()
tree = ElementTree()
tree.parse(response, parser)
sel = CSSSelector('#gaia_loginform')
form = sel(tree)[0]
# Submit #gaia_loginform
if form.get('method'):
method = form.get('method').upper()
params = []
# Get params from form inputs. Override email and password.
sel = CSSSelector('input, textarea')
for input in sel(form):
if 'Email' == input.get('name'):
params.append(('Email', sys.argv[3]))
elif 'Passwd' == input.get('name'):
params.append(('Passwd', sys.argv[4]))
else:
params.append((input.get('name'), input.get('value')))
url, headers, body = encode_form(method, url, params, form)
headers['Cookie'] = cookie
conn.request(method, url, body, headers)
response = conn.getresponse()
# Get cookie
cookie = response.getheader('Set-Cookie')
# Close Google project hosting issues
url = 'http://code.google.com/p/' + sys.argv[5] + '/issues/detail'
# Close issues with one connection. Add headers to make it persistent?
conn = http_connection(url)
for match in matches:
for match in re.findall('\\d+', match, re.I):
# Get #makechages form
method = 'GET'
url = 'http://code.google.com/p/' + sys.argv[5] + '/issues/detail'
params = [('id', match)]
url, headers, body = encode_params(method, url, params)
headers['Cookie'] = cookie
conn.request(method, url, body, headers)
response = conn.getresponse()
tree.parse(response, parser)
sel = CSSSelector('#makechanges form')
form = sel(tree)[0]
# Submit #makechanges form
if form.get('method'):
method = form.get('method').upper()
params = []
# Get params from form inputs. Override comment and status.
sel = CSSSelector('input, textarea')
for input in sel(form):
if 'comment' == input.get('name'):
params.append(('comment', 'Fixed in commit ' + sys.argv[2]))
elif 'status' == input.get('name'):
params.append(('status', 'Fixed'))
else:
params.append((input.get('name'), input.get('value')))
url, headers, body = encode_form(method, url, params, form)
headers['Cookie'] = cookie
# Close issue
conn.request(method, url, body, headers)
# Note that you must have read the whole response before you can send a
# new request to the server. Actually, you must call getresponse()
# before you can send a new request to the server and you must have read
# the whole response before you can call getresponse() again : P
conn.getresponse().read()
print 'Closed issue ' + match + '.'