-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_pr_to_bugzilla.py
executable file
·172 lines (152 loc) · 5.15 KB
/
send_pr_to_bugzilla.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
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
#!/usr/bin/env python
import sys
import email
valid_send_pr_keys = (
'Submitter-Id',
'Originator',
'Organization',
'Confidential',
'Synopsis',
'Severity',
'Priority',
'Category',
'Class',
'Release',
'Environment',
'Description',
'How-To-Repeat',
'Fix',
)
items = {}
item_key = None
item_value = None
attachments = {}
is_attachment = False
attachment_filename = None
attachment_content = ''
# Read email from stdin
msg = email.message_from_file(sys.stdin)
body = msg.get_payload()
# Parse send-pr email
for line in body.splitlines(True):
if item_key is None and not line.startswith('>'): continue # Empty lines in top of email
if line.startswith('>'):
# Parse ">SETTING : VALUE" line
item_key, sep, item_value = [s.strip() for s in line[1:].partition(':')]
assert item_key in valid_send_pr_keys
assert item_key not in items
items[item_key] = item_value
continue
if line.startswith('--- ') and line.rstrip().endswith(' begins here ---'):
# Start parsing an attachment
assert is_attachment == False
assert attachment_filename is None
assert attachment_content == ''
is_attachment = True
attachment_filename = line.replace('--- ', '').replace(' begins here ---', '').strip()
assert len(attachment_filename) > 0
assert attachment_filename not in attachments
attachments[attachment_filename] = ''
continue
if line.startswith('--- ') and line.rstrip().endswith(' ends here ---'):
# Attachment ends
assert attachment_filename == line.replace('--- ', '').replace(' ends here ---', '').strip()
is_attachment = False
attachment_filename = None
attachment_content = ''
continue
if is_attachment:
#Read attachment contents
assert attachment_filename is not None
attachments[attachment_filename] += line
continue
# Multi-line setting values
items[item_key] += line.rstrip()
print msg
XMLAPI_URL = 'http://example.com/bugs/xmlrpc.cgi'
BZ_USER = '[email protected]'
BZ_PASS = 'anonymous'
import xmlrpclib
import gzip
from StringIO import StringIO
import Cookie
class SessionTransport(xmlrpclib.Transport):
def __init__(self, *args, **kwargs):
self.sessioncookie = Cookie.SimpleCookie()
super(SessionTransport, self).__init__(*args, **kwargs)
def parse_response(self, response):
# read response data from httpresponse, and parse it
# Check for new http response object, else it is a file object
if hasattr(response,'getheader'):
if response.getheader("Content-Encoding", "") == "gzip":
stream = gzip.GzipFile('', 'r', 0, StringIO(response))
else:
stream = response
cookie = response.getheader('Set-Cookie')
if cookie:
self.sessioncookie.load(cookie)
else:
stream = response
p, u = self.getparser()
while 1:
data = stream.read(1024)
if not data:
break
if self.verbose:
print "body:", repr(data)
p.feed(data)
if stream is not response:
stream.close()
p.close()
return u.close()
def send_request(self, connection, handler, request_body):
if (self.accept_gzip_encoding and gzip):
connection.putrequest("POST", handler, skip_accept_encoding=True)
connection.putheader("Accept-Encoding", "gzip")
else:
connection.putrequest("POST", handler)
if len(self.sessioncookie):
cookiestr = self.sessioncookie.output(header='', sep=';')
connection.putheader('Cookie', cookiestr)
bz = xmlrpclib.ServerProxy(XMLAPI_URL, transport=SessionTransport())
login = bz.User.login(dict(login=BZ_USER, password=BZ_PASS))
user_id = login.get('id')
if user_id: print 'login success:', login
bug = bz.Bug.create(dict(
product = 'TestProduct',
component = 'TestComponent',
summary = items['Synopsis'],
version = '9.0', #items['Release'],
description = items['Description'] + '\n\nReported by: ' + items['Originator'] +
' (' + (msg['Reply-To'] or msg['From']) + ')',
op_sys = 'FreeBSD', #items['Environment'],
platform = 'All', #items['Release'].strip().split(' ')[-1],
priority = items['Priority'].capitalize(),
severity = items['Severity'],
#alias=...,
#assigned_to =...,
#cc = [user.strip() for user in msg['Cc']],
comment_is_private = True if items['Confidential'] != 'no' else False,
#groups=...,
#qa_contact=...,
#status=...,
#resolution=...,
#target_milestone=...,)
))
bug_id = bug.get('id')
if bug_id: print 'bug create success', bug
for key, val in attachments.iteritems():
print 'add attachment', key, bz.Bug.add_attachment(dict(
ids = [int(bug_id)],
data = val,
file_name = key,
summary = 'Added by send-pr',
content_type = 'text/plain',
#comment = ...,
is_patch = True,
is_private = True if items['Confidential'] != 'no' else False,
))
#items['Category']
#items['Class']
#items['How-To-Repeat']
#items['Fix']