fetch.py 1.7 KB

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