forked from drdee/bingle
/
bingle.py
executable file
·148 lines (125 loc) · 5.44 KB
/
bingle.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
#!/usr/bin/env python
import re
import json
import requests
from cgi import escape as htmlEscape
from config import *
from lib.bingle import Bingle
from lib.mingle import Mingle
import bingleResolved
def postComments(auth, apiBaseUrl, comments, mingle_id):
pos = mingle_id.rfind('/') + 1
mingle_id = mingle_id[pos:]
mingle_id = mingle_id.replace('.xml', '')
headers = {'content-type': 'application/x-www-form-urlencoded'}
url = '%s%s/%s/comments.xml' % (apiBaseUrl, 'cards', mingle_id)
# the first comment is already used in the summary of the mingle card.
comments = comments.get('comments')[1:]
for comment in comments:
payload = {'comment[content]': '%s\n#%s' % (
comment.get('text'), mingle_id)}
response = requests.post(url,
data=payload,
auth=(auth.get(
'username'), auth.get('password')),
headers=headers)
if __name__ == "__main__":
bingle = Bingle(debug=debug, picklePath=picklePath)
bingle.info("Pretend mode: %s" % options.pretend)
# prepare Mingle instance
mingle = Mingle(auth, apiBaseUrl)
fromTime = bingle.getTimeFromPickle()
bugzillaPayload = {'method': 'Bug.search', 'params': json.dumps([{
'product': product,
'component': component,
'status': ['UNCONFIRMED', 'NEW'],
'last_change_time': fromTime,
}])}
for bug in bingle.getBugEntries(bugzillaPayload):
bingle.info("Bug XML: %s" % bug)
bugCardName = bingle.generateBugCardName(
bug.get('id', '---'), bug.get('summary').encode('ascii', 'ignore'))
# see if there's a mingle card matching this bug
if len(bugIdFieldName) > 0:
foundBug = mingle.findCardNumByBugId(
bugCard, bug.get('id'), bugIdFieldName)
else:
foundBug = mingle.findCardNumByName(
bugCard, bugCardName)
bingle.info(mingle.dumpRequest())
if len(foundBug) > 0:
bingle.info('Existing card(s) %s match bug %s, so skip it.' % (
','.join([str(m['Number']) for m in foundBug]),
bug.get('id')))
continue
else:
bingle.info('Did not find card matching bug %s, so add it.'
% (bug.get('id')))
# retrieve bug comments
comment_payload = {'method': 'Bug.comments', 'params': json.dumps(
[{'ids': ['%s' % bug.get('id')]}])}
comments = bingle.getBugComments(comment_payload, bug.get('id'))
link = '<br><p>Full bug report at https://bugzilla.wikimedia.org/%s' \
'</p>' % bug.get('id')
# set common mingle parameters
description = htmlEscape(
comments.get('comments')[0].get('text')).replace(
"\n", "<br />") + link
cardParams = {
'card[name]': bugCardName,
'card[card_type_name]': bugCard,
'card[description]': description,
'card[created_by]': auth['username'],
}
cardLocation = mingle.addCard(cardParams)
bingle.info(mingle.dumpRequest())
postComments(auth, apiBaseUrl, comments, cardLocation)
# set custom mingle properties
properties = {}
for key, value in bugzillaProperties.iteritems():
properties[value] = bug.get(key, '')
properties.update(mingleProperties)
for prop, value in properties.iteritems():
propName = prop.strip('\'').strip('"')
propValue = mapping.get(value, value).strip('\'').strip('"')
cardParams = {
'card[properties][][name]': propName,
'card[properties][][value]': propValue
}
mingle.updateCardByLocation(cardLocation, cardParams)
bingle.info(mingle.dumpRequest())
# include bug ID if configured as a property
if len(bugIdFieldName):
bugId = bug.get('id')
cardParams = {
'card[properties][][name]': bugIdFieldName,
'card[properties][][value]': bugId,
}
mingle.updateCardByLocation(cardLocation, cardParams)
bingle.info(mingle.dumpRequest())
# post comment with mingle card it back to bugzilla bug
pos = cardLocation.rfind('/')
cardId = cardLocation[pos:-4]
comment = 'Prioritization and scheduling of this bug is tracked on ' \
'Mingle card %scards%s' % (mingleUrlBase, cardId)
bugzilla_payload = {
'jsonrpc': '1.1',
'method': 'Bug.add_comment', 'id': 1,
'params': [{
'id': '%s' % bug.get('id'),
'Bugzilla_login': config.get('auth_bugzilla', 'username'),
'Bugzilla_password': config.get('auth_bugzilla', 'password'),
'comment': comment
}]
}
bingle.addBugComment(bugzilla_payload, bug.get('id'))
if options.reconcile:
# handle resolved
bingle.info("Preparing to reconcile bugs marked as resolved in BZ \
with Mingle.")
bingle.info("Pretend mode: %s" % options.pretend)
bingle.info("Ignoring bugs in: %s" % mingleIgnoreResolved)
bingleResolved.execute(bingle=bingle, mingle=mingle,
pretend=options.pretend, fromTime=fromTime)
bingle.info("Done reconciling resolved bugs.")
bingle.updatePickleTime()