forked from lemeurherve/jira-issues-importer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch_issues.py
executable file
·47 lines (35 loc) · 1.48 KB
/
fetch_issues.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
import os
import urllib.parse
# noinspection PyUnresolvedReferences
from lxml import etree, objectify
import requests
from math import ceil
jira_server = os.getenv('JIRA_MIGRATION_JIRA_URL', 'https://issues.jenkins.io')
jql_query = os.getenv('JIRA_MIGRATION_JQL_QUERY')
file_path = 'jira_output'
encoded_query = urllib.parse.quote(jql_query)
pager = 0
def fetch_total_results():
"""
Load one result from query to see how many results there will be to calculate pagination.
"""
global max_results, url, response, total_results
max_results = 1
url = f'{jira_server}/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery={encoded_query}&tempMax={max_results}&pager/start={max_results}'
response = requests.get(url)
result = objectify.fromstring(response.text)
return int(result.channel.issue.attrib['total'])
total_results = fetch_total_results()
total_pages = ceil(total_results / 1000)
max_results = 1000
while pager < total_results:
page_number = ceil(pager / 1000 + 1)
print(f'Fetching page {page_number}, out of {total_pages}')
url = f'{jira_server}/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery={encoded_query}&tempMax={max_results}&pager/start={pager}'
response = requests.get(url)
root = objectify.fromstring(response.text)
with open(f'{file_path}/result-{pager}.xml', 'wb') as doc:
doc.write(etree.tostring(root, pretty_print=True))
pager += 1000
print('Complete')