fetch.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import io
  11. import sys
  12. def fetch(project_issue_code, jira_project_name):
  13. """ Fetch issues that match given jql query """
  14. # Jira Query Language string which filters for resolved issues of type bug
  15. jql = 'project = ' + project_issue_code + ' ' \
  16. + 'AND issuetype = Bug '\
  17. + 'AND status in (Resolved, Closed) '\
  18. + 'AND resolution = Fixed '\
  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://' + jira_project_name + '/rest/api/2/search?'\
  27. + 'jql={}&startAt={}&maxResults={}'
  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 io.open('issues/res' + str(start_at) + '.json', 'w', encoding="utf-8") as f:
  38. f.write(conn.read().decode('utf-8', 'ignore'))
  39. print('|', end='', flush='True')
  40. start_at += max_results
  41. print('\nDone!')
  42. if __name__ == '__main__':
  43. parser = argparse.ArgumentParser(description="""Convert a git log output to json.
  44. """)
  45. parser.add_argument('--issue-code', type=str,
  46. help="The code used for the project issues on JIRA: e.g., JENKINS-1123. Only JENKINS needs to be passed as parameter.")
  47. parser.add_argument('--jira-project', type=str,
  48. help="The name of the Jira repository of the project.")
  49. args = parser.parse_args()
  50. project_issue_code = args.issue_code
  51. jira_project_name = args.jira_project
  52. fetch(project_issue_code, jira_project_name)