def test_merge(self): def bughandler1(bug, data): data[bug['id']] = bug def bughandler2(bug, data): data[bug['id']] = bug bugs1 = {} bugs2 = {} bz1 = bugzilla.Bugzilla([12345, 12346], include_fields=['id'], bughandler=bughandler1, bugdata=bugs1) bz2 = bugzilla.Bugzilla([12345, 12346], include_fields=['id', 'resolution'], bughandler=bughandler2, bugdata=bugs2) bz1.merge(bz2).get_data().wait() self.assertEqual(bugs1[12345]['id'], 12345) self.assertEqual(bugs1[12346]['id'], 12346) self.assertEqual(bugs2[12345]['id'], 12345) self.assertEqual(bugs2[12345]['resolution'], u'FIXED') self.assertEqual(bugs2[12346]['id'], 12346) self.assertEqual(bugs2[12346]['resolution'], u'FIXED')
def test_search_dict(self): def bughandler(bug, data): data[bug['id']] = bug bugs = {} # Unique bug id terms = { 'bug_id': 12345, 'bug_id_type': 'anyexact', 'list_id': 12958345, 'resolution': 'FIXED', 'query_format': 'advanced', } bugzilla.Bugzilla(terms, bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(len(bugs), 1) self.assertEqual(bugs[12345]['id'], 12345) bugs = {} # Mutiple bugs terms = { 'bug_id': [12345, 12346], 'bug_id_type': 'anyexact', 'list_id': 12958345, 'resolution': 'FIXED', 'query_format': 'advanced', } bugzilla.Bugzilla(terms, bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(len(bugs), 2) self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12346]['id'], 12346) bugs = {} # Mutiple queries terms = [ { 'bug_id': 12345, }, { 'bug_id': 12346, }, ] bugzilla.Bugzilla(terms, bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(len(bugs), 2) self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12346]['id'], 12346)
def test_search(self): def bughandler(bug, data): data['bug'] = bug def commenthandler(bug, bugid, data): data['comment'] = bug['comments'] def historyhandler(bug, data): data['history'] = bug def attachmenthandler(bug, bugid, data): data['attachment'] = bug data = {} bugzilla.Bugzilla('bug_id=12345', bughandler=bughandler, bugdata=data, commenthandler=commenthandler, commentdata=data, historyhandler=historyhandler, historydata=data, attachmenthandler=attachmenthandler, attachmentdata=data).get_data().wait() self.assertEqual(data['bug']['id'], 12345) self.assertEqual(len(data['comment']), 19) self.assertTrue( data['comment'][0]['text'].startswith(u'Steps to reproduce')) self.assertEqual(len(data['history']['history']), 24) self.assertEqual(len(data['attachment']), 1) self.assertEqual(data['attachment'][0]['description'], 'Some patch.') self.assertEqual(data['attachment'][0]['is_patch'], 1) self.assertEqual(data['attachment'][0]['is_obsolete'], 1)
def test_bugids_multihandlers2(self): bugs1 = {} bugs2 = {} bugs3 = {} def bughandler1(bug): bugs1[bug['id']] = bug def bughandler2(bug): bugs2[bug['id']] = bug def bughandler3(bug, data): data[bug['id']] = bug bugzilla.Bugzilla( [12345, 12346], bughandler=[bughandler1, bughandler2, (bughandler3, bugs3)]).get_data().wait() for bugs in [bugs1, bugs2, bugs3]: self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12345]['resolution'], u'FIXED') self.assertEqual(bugs[12345]['assigned_to'], u'*****@*****.**') self.assertEqual( bugs[12345]['summary'], u'[DOGFOOD] Unable to Forward a message received as an Inline page or an attachment' ) self.assertEqual(bugs[12346]['id'], 12346) self.assertEqual(bugs[12346]['resolution'], u'FIXED') self.assertEqual(bugs[12346]['assigned_to'], u'*****@*****.**') self.assertEqual(bugs[12346]['summary'], u'nsOutputFileStream should buffer the output')
def list_bugs(self, query): """ List all the bugs from a Bugzilla query """ def _bughandler(bug, data): bugid = bug['id'] data[bugid] = bug def _attachmenthandler(attachments, bugid, data): data[int(bugid)] = attachments bugs, attachments = {}, {} bz = bugzilla.Bugzilla(query, bughandler=_bughandler, attachmenthandler=_attachmenthandler, bugdata=bugs, attachmentdata=attachments) bz.get_data().wait() # Map attachments on bugs for bugid, _attachments in attachments.items(): if bugid not in bugs: continue bugs[bugid]['attachments'] = _attachments return bugs
def test_queries(self): bugs = {} def bughandler(data): bug = data['bugs'][0] bugs[bug['id']] = bug queries = [ Query(bugzilla.Bugzilla.API_URL, {'id': '12345'}, bughandler), Query(bugzilla.Bugzilla.API_URL, {'id': '12346'}, bughandler) ] bugzilla.Bugzilla(queries=queries, bughandler=bughandler).wait() self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12345]['resolution'], u'FIXED') self.assertEqual(bugs[12345]['assigned_to'], u'*****@*****.**') self.assertEqual( bugs[12345]['summary'], u'[DOGFOOD] Unable to Forward a message received as an Inline page or an attachment' ) self.assertEqual(bugs[12346]['id'], 12346) self.assertEqual(bugs[12346]['resolution'], u'FIXED') self.assertEqual(bugs[12346]['assigned_to'], u'*****@*****.**') self.assertEqual(bugs[12346]['summary'], u'nsOutputFileStream should buffer the output')
def test_empty_queries(self): bugs = {} def bughandler(data): bug = data['bugs'][0] bugs[bug['id']] = bug bugzilla.Bugzilla(queries=[], bughandler=bughandler).wait() self.assertEqual(bugs, {})
def test_search(self): def bughandler(bug, data): data[bug['id']] = bug bugs = {} bugzilla.Bugzilla( 'bug_id=12345%2C12346&bug_id_type=anyexact&list_id=12958345&resolution=FIXED&query_format=advanced', bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12346]['id'], 12346)
def test_attachment_include_fields(self): def attachmenthandler(bug, bugid, data): data['attachment'] = bug data = {} bugzilla.Bugzilla(12345, attachmenthandler=attachmenthandler, attachmentdata=data, attachment_include_fields=['description' ]).get_data().wait() self.assertEqual(data['attachment'][0]['description'], 'Some patch.') self.assertNotIn('is_patch', data['attachment'][0]) self.assertNotIn('is_obsolete', data['attachment'][0])
def test_search_multiple(self): def bughandler(bug, data): data[bug['id']] = bug bugs = {} bugzilla.Bugzilla( ['bug_id=12345%2C12346%2C12347', 'bug_id=12348%2C12349%2C12350'], bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12346]['id'], 12346) self.assertEqual(bugs[12347]['id'], 12347) self.assertEqual(bugs[12348]['id'], 12348) self.assertEqual(bugs[12349]['id'], 12349) self.assertEqual(bugs[12350]['id'], 12350)
def test_bugid(self): def bughandler(bug, data): data.update(bug) bug = {} bugzilla.Bugzilla(12345, bughandler=bughandler, bugdata=bug).get_data().wait() self.assertEqual(bug['id'], 12345) self.assertEqual(bug['resolution'], u'FIXED') self.assertEqual(bug['assigned_to'], u'*****@*****.**') self.assertEqual( bug['summary'], u'[DOGFOOD] Unable to Forward a message received as an Inline page or an attachment' )
def test_comment_include_fields(self): def commenthandler(bug, bugid, data): data['comments'] = bug['comments'] data = {} bugzilla.Bugzilla(12345, commenthandler=commenthandler, commentdata=data, comment_include_fields=['author']).get_data().wait() self.assertEqual(data['comments'][0]['author'], '*****@*****.**') for field in [ 'bug_id', 'creator', 'raw_text', 'id', 'tags', 'text', 'is_private', 'time', 'creation_time', 'attachment_id' ]: self.assertNotIn(field, data['comments'][0])
def get_ids(query): ids = [] while True: new_ids = [] def bughandler(bug): ids.append(bug['id']) new_ids.append(bug['id']) bugzilla.Bugzilla(query + str(max(ids) if len(ids) > 0 else 0) + '&include_fields=id', bughandler=bughandler).get_data().wait() if len(new_ids) < 500: break return ids
def _download(ids_or_query): new_bugs = {} def bughandler(bug): bug_id = int(bug['id']) if bug_id not in new_bugs: new_bugs[bug_id] = dict() new_bugs[bug_id].update(bug) def commenthandler(bug, bug_id): bug_id = int(bug_id) if bug_id not in new_bugs: new_bugs[bug_id] = dict() new_bugs[bug_id]['comments'] = bug['comments'] def attachmenthandler(bug, bug_id): bug_id = int(bug_id) if bug_id not in new_bugs: new_bugs[bug_id] = dict() new_bugs[bug_id]['attachments'] = bug def historyhandler(bug): bug_id = int(bug['id']) if bug_id not in new_bugs: new_bugs[bug_id] = dict() new_bugs[bug_id]['history'] = bug['history'] bugzilla.Bugzilla(ids_or_query, bughandler=bughandler, commenthandler=commenthandler, comment_include_fields=COMMENT_INCLUDE_FIELDS, attachmenthandler=attachmenthandler, attachment_include_fields=ATTACHMENT_INCLUDE_FIELDS, historyhandler=historyhandler).get_data().wait() return new_bugs
def test_search_only_attachment(self): def bughandler(bug, data): data['bug'] = bug def attachmenthandler(bug, bugid, data): data['attachment'] = bug data = {} bugzilla.Bugzilla('bug_id=12345', bughandler=bughandler, bugdata=data, attachmenthandler=attachmenthandler, attachmentdata=data).get_data().wait() self.assertEqual(data['bug']['id'], 12345) self.assertEqual(len(data['attachment']), 1) self.assertEqual(data['attachment'][0]['description'], 'Some patch.') self.assertEqual(data['attachment'][0]['is_patch'], 1) self.assertEqual(data['attachment'][0]['is_obsolete'], 1)
def test_bugids(self): def bughandler(bug, data): data[bug['id']] = bug bugs = {} bugzilla.Bugzilla([12345, 12346], bughandler=bughandler, bugdata=bugs).get_data().wait() self.assertEqual(bugs[12345]['id'], 12345) self.assertEqual(bugs[12345]['resolution'], u'FIXED') self.assertEqual(bugs[12345]['assigned_to'], u'*****@*****.**') self.assertEqual( bugs[12345]['summary'], u'[DOGFOOD] Unable to Forward a message received as an Inline page or an attachment' ) self.assertEqual(bugs[12346]['id'], 12346) self.assertEqual(bugs[12346]['resolution'], u'FIXED') self.assertEqual(bugs[12346]['assigned_to'], u'*****@*****.**') self.assertEqual(bugs[12346]['summary'], u'nsOutputFileStream should buffer the output')
def test_bugid(self): def bughandler(bug, data): data['bug'] = bug def commenthandler(bug, bugid, data): data['comment'] = bug['comments'] def historyhandler(bug, data): data['history'] = bug data = {} bugzilla.Bugzilla(12345, bughandler=bughandler, bugdata=data, commenthandler=commenthandler, commentdata=data, historyhandler=historyhandler, historydata=data).get_data().wait() self.assertEqual(data['bug']['id'], 12345) self.assertEqual(len(data['comment']), 19) self.assertTrue( data['comment'][0]['text'].startswith(u'Steps to reproduce')) self.assertEqual(len(data['history']['history']), 24)
for k, v in bug.items(): bugs[bug_id][k] = v def historyhandler(bug): bugid = str(bug['id']) if bugid not in bugs: bugs[bugid] = dict() bugs[bugid]['history'] = bug['history'] bugzilla.Bugzilla(bug_ids, bughandler=bughandler, historyhandler=historyhandler, include_fields=['id', 'creation_time', 'cf_last_resolved']).get_data().wait() with open('bug_data.csv', 'w') as f: csv_writer = csv.writer(f) csv_writer.writerow([ 'bug_id', 'fixing_time', 'fixing_time_since_blocking', 'fixing_time_since_tracked', 'tracked_or_blocking', 'which_blocking' ]) for bug_id, bug_data in bugs.items(): tracked = False blocking = False which_blocking = [] when_blocking = None when_tracked = None
def __download_bugs(name, query): bugs = [] i = 0 while True: try: with open(name + '/' + name + str(i) + '.json', 'r') as f: bugs += json.load(f) i += 1 except IOError: break print('Loaded ' + str(len(bugs)) + ' bugs.') search_query = query + '&limit=500&order=bug_id&f1=bug_id&o1=greaterthan&v1=' last_id = max([bug['id'] for bug in bugs]) if len(bugs) > 0 else 0 ATTACHMENT_INCLUDE_FIELDS = [ 'id', 'creation_time', 'is_obsolete', 'flags', 'is_patch', 'creator', 'content_type', ] COMMENT_INCLUDE_FIELDS = [ 'id', 'text', 'author', 'time', ] found = [] finished = False while not finished: bugs_dict = dict() def bughandler(bug): bugid = str(bug['id']) if bugid not in bugs_dict: bugs_dict[bugid] = dict() for k, v in bug.items(): bugs_dict[bugid][k] = v def commenthandler(bug, bugid): bugid = str(bugid) if bugid not in bugs_dict: bugs_dict[bugid] = dict() bugs_dict[bugid]['comments'] = bug['comments'] def attachmenthandler(bug, bugid): bugid = str(bugid) if bugid not in bugs_dict: bugs_dict[bugid] = dict() bugs_dict[bugid]['attachments'] = bug def historyhandler(bug): bugid = str(bug['id']) if bugid not in bugs_dict: bugs_dict[bugid] = dict() bugs_dict[bugid]['history'] = bug['history'] bugzilla.Bugzilla(search_query + str(last_id), bughandler=bughandler, commenthandler=commenthandler, comment_include_fields=COMMENT_INCLUDE_FIELDS, attachmenthandler=attachmenthandler, attachment_include_fields=ATTACHMENT_INCLUDE_FIELDS, historyhandler=historyhandler).get_data().wait() found = list(bugs_dict.values()) last_id = max([last_id] + [bug['id'] for bug in found]) bugs += found print('Total number of bugs: ' + str(len(bugs))) if len(found) != 0 and (len(bugs) % 5000 == 0 or len(found) < 500): for i in range(0, int(math.ceil(float(len(bugs)) / 1000))): with open(name + '/' + name + str(i) + '.json', 'w') as f: json.dump(bugs[i * 1000:(i + 1) * 1000], f) if len(found) < 500: finished = True return bugs
def test_search_history(self): def historyhandler(bug, data): data['history'] = bug['history'] data = {} bugzilla.Bugzilla(12345, historyhandler=historyhandler, historydata=data).get_data().wait() all = bugzilla.Bugzilla.get_history_matches(data['history'], {}) self.assertEqual(len(all), len(data['history'])) change_to_assigned = bugzilla.Bugzilla.get_history_matches( data['history'], {'added': 'ASSIGNED'}) self.assertEqual(change_to_assigned, [{ 'when': '1999-08-29T17:43:15Z', 'changes': [{ 'added': 'ASSIGNED', 'field_name': 'status', 'removed': 'NEW' }], 'who': '*****@*****.**' }]) blocks_changes = bugzilla.Bugzilla.get_history_matches( data['history'], {'field_name': 'blocks'}) self.assertEqual(blocks_changes, [{ 'changes': [{ 'removed': '', 'added': '11091', 'field_name': 'blocks' }], 'who': '*****@*****.**', 'when': '1999-09-20T22:58:39Z' }, { 'changes': [{ 'removed': '', 'added': '17976', 'field_name': 'blocks' }], 'who': '*****@*****.**', 'when': '1999-11-04T14:05:18Z' }]) single_block_change = bugzilla.Bugzilla.get_history_matches( data['history'], { 'added': '11091', 'field_name': 'blocks' }) self.assertEqual(single_block_change, [{ 'changes': [{ 'removed': '', 'added': '11091', 'field_name': 'blocks' }], 'who': '*****@*****.**', 'when': '1999-09-20T22:58:39Z' }]) data = {} bugzilla.Bugzilla(1005958, historyhandler=historyhandler, historydata=data).get_data().wait() multiple_changes = bugzilla.Bugzilla.get_history_matches( data['history'], {'added': 'approval-mozilla-release?'}) self.assertEqual(multiple_changes, [{ 'changes': [{ 'added': 'approval-mozilla-aurora?, approval-mozilla-beta?, approval-mozilla-release?', 'attachment_id': 8417443, 'field_name': 'flagtypes.name', 'removed': '' }], 'when': '2014-05-05T20:25:06Z', 'who': '*****@*****.**' }])
def test_search_landing(self): def commenthandler(bug, bugid, data): data['comments'] = bug['comments'] data = {} bugzilla.Bugzilla(538189, commenthandler=commenthandler, commentdata=data).get_data().wait() inbound = bugzilla.Bugzilla.get_landing_comments( data['comments'], 'inbound') self.assertEqual(len(inbound), 1) self.assertEqual(inbound[0]['revision'], '42c54c7cb4a3') self.assertEqual( inbound[0]['comment'], { 'count': 39, 'attachment_id': None, 'raw_text': 'http://hg.mozilla.org/integration/mozilla-inbound/rev/42c54c7cb4a3', 'tags': [], 'is_private': False, 'creator': '*****@*****.**', 'bug_id': 538189, 'author': '*****@*****.**', 'text': 'http://hg.mozilla.org/integration/mozilla-inbound/rev/42c54c7cb4a3', 'id': 5655196, 'creation_time': '2011-08-15T21:21:13Z', 'time': '2011-08-15T21:21:13Z' }) central = bugzilla.Bugzilla.get_landing_comments( data['comments'], 'central') self.assertEqual(len(central), 1) self.assertEqual(central[0]['revision'], '42c54c7cb4a3') self.assertEqual( central[0]['comment'], { 'count': 43, 'attachment_id': None, 'raw_text': 'http://hg.mozilla.org/mozilla-central/rev/42c54c7cb4a3\n\nAsa, did you mean to set approval-beta+ instead of approval-beta?', 'tags': [], 'is_private': False, 'creator': '*****@*****.**', 'bug_id': 538189, 'author': '*****@*****.**', 'text': 'http://hg.mozilla.org/mozilla-central/rev/42c54c7cb4a3\n\nAsa, did you mean to set approval-beta+ instead of approval-beta?', 'id': 5656549, 'creation_time': '2011-08-16T11:02:36Z', 'time': '2011-08-16T11:02:36Z' }) beta = bugzilla.Bugzilla.get_landing_comments(data['comments'], 'beta') self.assertEqual(len(beta), 1) self.assertEqual(beta[0]['revision'], '1d02edaa92bc') self.assertEqual( beta[0]['comment'], { 'count': 51, 'attachment_id': None, 'raw_text': 'http://hg.mozilla.org/releases/mozilla-beta/rev/1d02edaa92bc', 'tags': [], 'is_private': False, 'creator': '*****@*****.**', 'bug_id': 538189, 'author': '*****@*****.**', 'text': 'http://hg.mozilla.org/releases/mozilla-beta/rev/1d02edaa92bc', 'id': 5686198, 'creation_time': '2011-08-29T21:55:57Z', 'time': '2011-08-29T21:55:57Z' }) multiple = bugzilla.Bugzilla.get_landing_comments( data['comments'], ['beta', 'inbound', 'central']) self.assertEqual(multiple, [{ 'revision': '42c54c7cb4a3', 'channel': 'inbound', 'comment': { 'count': 39, 'creation_time': '2011-08-15T21:21:13Z', 'is_private': False, 'attachment_id': None, 'text': 'http://hg.mozilla.org/integration/mozilla-inbound/rev/42c54c7cb4a3', 'creator': '*****@*****.**', 'tags': [], 'bug_id': 538189, 'author': '*****@*****.**', 'time': '2011-08-15T21:21:13Z', 'id': 5655196, 'raw_text': 'http://hg.mozilla.org/integration/mozilla-inbound/rev/42c54c7cb4a3' } }, { 'revision': '42c54c7cb4a3', 'channel': 'central', 'comment': { 'count': 43, 'creation_time': '2011-08-16T11:02:36Z', 'is_private': False, 'attachment_id': None, 'text': 'http://hg.mozilla.org/mozilla-central/rev/42c54c7cb4a3\n\nAsa, did you mean to set approval-beta+ instead of approval-beta?', 'creator': '*****@*****.**', 'tags': [], 'bug_id': 538189, 'author': '*****@*****.**', 'time': '2011-08-16T11:02:36Z', 'id': 5656549, 'raw_text': 'http://hg.mozilla.org/mozilla-central/rev/42c54c7cb4a3\n\nAsa, did you mean to set approval-beta+ instead of approval-beta?' } }, { 'revision': '1d02edaa92bc', 'channel': 'beta', 'comment': { 'count': 51, 'creation_time': '2011-08-29T21:55:57Z', 'is_private': False, 'attachment_id': None, 'text': 'http://hg.mozilla.org/releases/mozilla-beta/rev/1d02edaa92bc', 'creator': '*****@*****.**', 'tags': [], 'bug_id': 538189, 'author': '*****@*****.**', 'time': '2011-08-29T21:55:57Z', 'id': 5686198, 'raw_text': 'http://hg.mozilla.org/releases/mozilla-beta/rev/1d02edaa92bc' } }]) data = {} bugzilla.Bugzilla(679352, commenthandler=commenthandler, commentdata=data).get_data().wait() central = bugzilla.Bugzilla.get_landing_comments( data['comments'], 'central') self.assertEqual(len(central), 8)