-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingle.py
executable file
·140 lines (118 loc) · 5.6 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
#!/usr/bin/env python
import ConfigParser
import re
import json
import requests
from optparse import OptionParser
from lib.bingle import Bingle
from lib.mingle import Mingle
def createDictionaryFromPropertiesList(properties):
return dict((key.strip(), value.strip()) for key, value in (prop.split(',')
for prop in properties.split(';') if prop.find(',') > -1))
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)
comments = comments.get('comments')[
1:] # the first comment is already used in the summary of the mingle card.
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__":
parser = OptionParser()
parser.add_option("-c", "--config", dest="config",
help="Path to bingle config file", default="bingle.ini")
(options, args) = parser.parse_args()
config = ConfigParser.ConfigParser()
config.read(options.config)
auth = {'username': config.get('auth', 'username'),
'password': config.get('auth', 'password')}
debug = config.getboolean('debug', 'debug')
picklePath = config.get('paths', 'picklePath')
apiBaseUrl = config.get('urls', 'mingleApiBase')
mingleUrlBase = config.get('urls', 'mingleUrlBase')
bugCard = config.get('mingle', 'bugCard')
product = config.get('bugzilla', 'product').split(',')
component = config.get('bugzilla', 'component').split(',')
tags = config.get('mingle', 'tags')
bugzillaProperties = createDictionaryFromPropertiesList(
config.get('bugzilla', 'properties'))
mingleProperties = createDictionaryFromPropertiesList(
config.get('mingle', 'properties'))
mapping = createDictionaryFromPropertiesList(
config.get('mapping', 'properties'))
payload = {'method': 'Bug.search', 'params': json.dumps([{
'product': product,
'component': component,
'status': ['UNCONFIRMED', 'NEW'],
'creation_time': '2012-08-01 00:00 UTC',
}])}
bingle = Bingle(payload, debug=debug, picklePath=picklePath, feedUrl=config.get(
'urls', 'bugzillaFeed'))
# prepare Mingle instance
mingle = Mingle(auth, apiBaseUrl)
for bug in bingle.getBugEntries():
bingle.info("Bug XML: %s" % bug)
# look for card
foundBugs = mingle.findCardByName(
bugCard, bug.get('summary'), bug.get('id'))
bingle.info(mingle.dumpRequest())
if len(foundBugs) > 0:
continue
# 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
cardParams = {
'card[name]': '[Bug %s] %s' % (bug.get('id', '---'), bug.get('summary').encode('ascii', 'ignore')),
'card[card_type_name]': bugCard,
'card[description]': comments.get('comments')[0].get('text') + link,
'card[created_by]': auth['username'],
'card[tags][]': tags # is not supported by Mingle API currently
}
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():
cardParams = {
'card[properties][][name]': prop,
'card[properties][][value]': mapping.get(value, value)
}
mingle.updateCard(cardLocation, cardParams)
bingle.info(mingle.dumpRequest())
# include bug ID if configured as a property
bugIdFieldName = config.get('mingle', 'bugIdFieldName')
if len(bugIdFieldName):
bugId = bug.get('id')
cardParams = {
'card[properties][][name]': bugIdFieldName,
'card[properties][][value]': bugId,
}
mingle.updateCard(cardLocation, cardParams)
bingle.info(mingle.dumpRequest())
# post comment with mingle card it back to bugzilla bug
pos = cardLocation.rfind('/')
cardId = cardLocation[pos:-4]
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': 'Prioritization and scheduling of this bug is tracked on Mingle card %s%s' % (mingleUrlBase, cardId)}]}
bingle.addBugComment(bugzilla_payload, bug.get('id'))
bingle.updatePickleTime()