This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
forked from neonux/git-bz-moz
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbz.py
149 lines (121 loc) · 4.3 KB
/
bz.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
# Copyright (C) 2010 Mozilla Foundation
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import base64
import urllib
import urllib2
import urlparse
JSON_HEADERS = {"Accept": "application/json",
"Content-Type": "application/json"}
def make_url(api_server, auth, command, args={}):
url = urlparse.urljoin(api_server, command)
if auth is None and not args.keys():
return url
params = [auth.auth()] if auth else []
params.extend([k + "=" + urllib.quote(str(v)) for k, v in args.iteritems()])
return url + "?" + '&'.join(params)
def create_bug(token, product, component, version, title, description,
assign_to=None, cc=[], depends=[], blocks=[]):
"""
Create a bugzilla bug.
"""
o = {
'product': product,
'component': component,
'summary': title,
'version': version,
'description': description,
'op_sys': 'All',
'platform': 'All',
'depends_on': depends,
'blocks': blocks,
'cc': cc,
}
if assign_to:
o['assigned_to'] = assign_to
o['status'] = 'ASSIGNED'
return token.rest_request('POST', 'bug', data=o)
def create_attachment(auth, bug, contents,
description="attachment",
filename="attachment", comment="",
reviewers=None, review_flag_id=None,
feedback=None, feedback_flag_id=None):
"""
Post an attachment to a bugzilla bug using BzAPI.
"""
attachment = base64.b64encode(contents)
o = {
'ids': [bug],
'data': attachment,
'encoding': 'base64',
'file_name': filename,
'summary': description,
'is_patch': True,
'content_type': 'text/plain',
'flags': [],
}
if reviewers:
assert review_flag_id
for requestee in reviewers:
o['flags'].append({
'name': 'review',
'requestee': requestee,
'status': '?',
'type_id': review_flag_id,
'new': True,
})
if feedback:
assert feedback_flag_id
for requestee in feedback:
o['flags'].append({
'name': 'feedback',
'requestee': requestee,
'status': '?',
'type_id': feedback_flag_id,
'new': True,
})
if comment:
o['comment'] = comment
return auth.rest_request('POST', 'bug/%s/attachment' % bug, data=o)
def get_attachments(auth, bug):
return auth.rest_request('GET', 'bug/%s/attachment' % bug)
def obsolete_attachment(auth, attachment):
o = {
'ids': [attachment['id']],
'is_obsolete': True,
}
return auth.rest_request('PUT', 'bug/attachment/%s' % attachment['id'],
data=o)
def find_users(auth, search_string):
return auth.rest_request('GET', 'user', params={'match': [search_string]})
def get_configuration(api_server):
url = make_url(api_server, None, 'configuration', {'cached_ok': 1})
return urllib2.Request(url, None, JSON_HEADERS)
def get_bug(auth, bug, **opts):
"""
Retrieve an existing bug
"""
args = {}
if 'include_fields' in opts:
args['include_fields'] = ",".join(set(
opts['include_fields'] + ['id', 'ref', 'token', 'last_change_time', 'update_token']))
resp = auth.rest_request('GET', 'bug/%s' % bug, data=args)
return resp['bugs'][0]
def update_bug(auth, bugid, bugdata):
"""
Update an existing bug. Must pass in an existing bug as a data structure.
Mid-air collisions are possible.
"""
return auth.rest_request('PUT', 'bug/%s' % bugid, bugdata)