fetch-bugzilla.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """ Fetch issues that match given jql query """
  2. __author__ = "Kristian Berg"
  3. __copyright__ = "Copyright (c) 2018 Axis Communications AB"
  4. __license__ = "MIT"
  5. from urllib.parse import quote
  6. from urllib3 import PoolManager
  7. import json
  8. import os
  9. import argparse
  10. import io
  11. def translate_json(j):
  12. j['issues'] = j.pop('bugs')
  13. for i in j['issues']:
  14. i['key'] = '-' + str(i.pop('id'))
  15. i['fields'] = {'created': i.pop('creation_time').replace('Z', ' +0000'),
  16. 'resolutiondate': i.pop('cf_last_resolved').replace('Z', '+0000')}
  17. def fetch(keys, bugzilla_project_name):
  18. """ Fetch issues that match given bugzilla query """
  19. # Bugzilla query with necessary fields for fixed bugs
  20. q = 'include_fields=id,creation_time,cf_last_resolved' \
  21. + '&resolution=fixed'
  22. for key in keys:
  23. q += '&' + quote(key, safe='=')
  24. start_at = 0
  25. # max_results parameter is capped at 1000, specifying a higher value will
  26. # still return only the first 1000 results
  27. max_results = 1000
  28. os.makedirs('issues/', exist_ok=True)
  29. request = 'https://' + bugzilla_project_name + '/rest/bug?' + q \
  30. + '&startAt={}&maxResults=' + str(max_results)
  31. http = PoolManager()
  32. print('Progress: | = ' + str(max_results) + ' issues')
  33. while True:
  34. rurl = request.format(start_at)
  35. print(rurl)
  36. res = http.request('GET', request, retries=10)
  37. j = json.loads(res.data)
  38. translate_json(j)
  39. with io.open('issues/res' + str(start_at) + '.json', 'w',
  40. encoding="utf-8") as f:
  41. json.dump(j, f)
  42. if len(j['issues']) < max_results:
  43. break
  44. print('|', end='', flush='True')
  45. start_at += max_results
  46. print('\nDone!')
  47. if __name__ == '__main__':
  48. parser = argparse.ArgumentParser(description="""Fetch issues from a Bugzilla REST API.
  49. """)
  50. parser.add_argument('--bugzilla', type=str,
  51. help="The name of the Bugzilla repository of the project (i.e. the domain).")
  52. parser.add_argument('--key-value', type=str, action='append',
  53. help="Custom key-value string for Bugzilla query (MUST be a string of the form 'key=value'). You can find some of the options here: https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#search-bugs")
  54. #parser.add_argument('--products', type=str,
  55. # help="Comma-separated list of Prodcuts to query in (shorthand for multiple '--key-value product=Foo' options).")
  56. args = parser.parse_args()
  57. keys = args.key_value # + ['product=' + p for p in args.products.split(',')]
  58. bugzilla_project_name = args.bugzilla
  59. fetch(keys, bugzilla_project_name)