fetch.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. + 'AND component = core '\
  20. + 'AND created <= "2018-02-20 10:34" '\
  21. + 'ORDER BY created DESC'
  22. jql = quote(jql, safe='')
  23. start_at = 0
  24. # max_results parameter is capped at 1000, specifying a higher value will
  25. # still return only the first 1000 results
  26. max_results = 1000
  27. os.makedirs('issues/', exist_ok=True)
  28. request = 'https://' + jira_project_name + '/rest/api/2/search?'\
  29. + 'jql={}&startAt={}&maxResults={}'
  30. # Do small request to establish value of 'total'
  31. with url.urlopen(request.format(jql, start_at, '1')) as conn:
  32. contents = json.loads(conn.read().decode('utf-8'))
  33. total = contents['total']
  34. # Fetch all matching issues and write to file(s)
  35. print('Total issue matches: ' + str(total))
  36. print('Progress: | = ' + str(max_results) + ' issues')
  37. while start_at < total:
  38. with url.urlopen(request.format(jql, start_at, max_results)) as conn:
  39. with io.open('issues/res' + str(start_at) + '.json', 'w', encoding="utf-8") as f:
  40. f.write(conn.read().decode('utf-8', 'ignore'))
  41. print('|', end='', flush='True')
  42. start_at += max_results
  43. print('\nDone!')
  44. if __name__ == '__main__':
  45. parser = argparse.ArgumentParser(description="""Convert a git log output to json.
  46. """)
  47. parser.add_argument('--issue-code', type=str,
  48. help="The code used for the project issues on JIRA: e.g., JENKINS-1123. Only JENKINS needs to be passed as parameter.")
  49. parser.add_argument('--jira-project', type=str,
  50. help="The name of the Jira repository of the project.")
  51. args = parser.parse_args()
  52. project_issue_code = args.issue_code
  53. jira_project_name = args.jira_project
  54. fetch(project_issue_code, jira_project_name)