fetch.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import urllib.request as url
  7. import json
  8. import os
  9. import argparse
  10. def fetch():
  11. """ Fetch issues that match given jql query """
  12. # Jira Query Language string which filters for resolved issues of type bug
  13. jql = 'project = JENKINS '\
  14. + 'AND issuetype = Bug '\
  15. + 'AND status in (Resolved, Closed) '\
  16. + 'AND resolution = Fixed '\
  17. + 'AND component = core '\
  18. + 'AND created <= "2018-02-20 10:34" '\
  19. + 'ORDER BY created DESC'
  20. jql = quote(jql, safe='')
  21. start_at = 0
  22. # max_results parameter is capped at 1000, specifying a higher value will
  23. # still return only the first 1000 results
  24. max_results = 1000
  25. os.makedirs('issues/', exist_ok=True)
  26. request = 'https://issues.jenkins-ci.org/rest/api/2/search?'\
  27. + 'jql={}&start_at={}&max_results={}'
  28. # Do small request to establish value of 'total'
  29. with url.urlopen(request.format(jql, start_at, '1')) as conn:
  30. contents = json.loads(conn.read().decode('utf-8'))
  31. total = contents['total']
  32. # Fetch all matching issues and write to file(s)
  33. print('Total issue matches: ' + str(total))
  34. print('Progress: | = ' + str(max_results) + ' issues')
  35. while start_at < total:
  36. with url.urlopen(request.format(jql, start_at, max_results)) as conn:
  37. with open('issues/res' + str(start_at) + '.json', 'w') as f:
  38. f.write(conn.read().decode('utf-8'))
  39. print('|', end='', flush='True')
  40. start_at += max_results
  41. print('\nDone!')
  42. if __name__ == '__main__':
  43. fetch()