Repository navigation
Expand file tree
/
Copy pathexport_subtask_statuses.py
More file actions
198 lines (157 loc) · 5.44 KB
/
Copy pathexport_subtask_statuses.py
File metadata and controls
198 lines (157 loc) · 5.44 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from jira.resources import Issue
from mtlogger import logger
from mtprompt import Prompt
from mtvalidation import to_list, to_bool
from openpyxl import Workbook
from openpyxl.styles import Font
from tqdm import tqdm
from _common import get_jira_client, get_jira_url
OUTPUT_DIR_PATH = os.path.join(
os.path.dirname(__file__), '..', '_output', 'jira'
)
OUTPUT_FILE_PATH = os.path.join(OUTPUT_DIR_PATH, 'ticket_export.xlsx')
MAX_RESULTS = 255
def main():
if len(sys.argv) > 1:
ticket_ids = to_list(sys.argv[1])
skip_completed = to_bool(sys.argv[2]) if len(sys.argv) > 2 else False
else:
ticket_ids = Prompt.list('Enter parent ticket IDs (comma-separated)')
skip_completed = Prompt.bool(
'Skip completed tasks (Done/Closed)?', default=False
)
if not ticket_ids:
logger.error('No ticket IDs provided.')
return
os.makedirs(OUTPUT_DIR_PATH, exist_ok=True)
logger.debug(f'Processing {len(ticket_ids)} parent ticket(s)')
export_to_excel(process_tickets(ticket_ids, skip_completed))
logger.success(f'Saved "{OUTPUT_FILE_PATH}".')
def process_child(
child: Issue, parent_id: str, parent_name: str, skip_completed: bool
):
child_id = child.key
child_name = child.fields.summary
child_status = child.fields.status.name
if skip_completed and child_status in ('Done', 'Closed'):
return None
child_labels = ', '.join(getattr(child.fields, 'labels', []) or [])
return {
'Parent ID': parent_id,
'Parent Name': parent_name,
'Child ID': child_id,
'Child Name': child_name,
'Child Status': child_status,
'Child Labels': child_labels,
}
def process_tickets(ticket_ids: list[str], skip_completed: bool):
data = []
for ticket_id in tqdm(ticket_ids, desc='Processing tickets', unit='ticket'):
ticket_id = ticket_id.strip()
try:
parent_issue = get_jira_client().issue(ticket_id)
all_issues = []
child_issues = parent_issue.fields.subtasks
if child_issues:
all_issues.extend(child_issues)
try:
jql_query = f'"Epic Link" = {ticket_id}'
epic_tasks = get_jira_client().search_issues(
jql_query, maxResults=MAX_RESULTS
)
if epic_tasks:
existing_keys = {issue.key for issue in all_issues}
new_epic_tasks = [
issue for issue in epic_tasks if issue.key not in existing_keys
]
all_issues.extend(new_epic_tasks)
except Exception as ex:
logger.debug(f'No epic tasks found for {ticket_id}: {ex}')
if not all_issues:
continue
tqdm.write(f'Total: {len(all_issues)} item(s) for {ticket_id}')
parent_name = parent_issue.fields.summary
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(
process_child, child, ticket_id, parent_name, skip_completed
): child.key
for child in all_issues
}
for future in tqdm(
as_completed(futures),
total=len(all_issues),
desc=ticket_id,
leave=False,
unit='child',
):
try:
result = future.result()
if result:
data.append(result)
except Exception as ex:
logger.debug(f'Failed to process child: {ex}')
except Exception as ex:
logger.error(f'Failed to fetch child items for {ticket_id}: {ex}')
return data
def export_to_excel(data: list[dict]):
if not data:
logger.error('No child items to export.')
return
try:
jira_url = get_jira_url()
workbook = Workbook()
worksheet = workbook.active
worksheet.title = 'Tickets'
fieldnames = [
'Parent ID',
'Parent Name',
'Child ID',
'Child Name',
'Child Status',
'Child Labels',
]
worksheet.append(fieldnames)
for row_idx, row_data in enumerate(data, start=2):
parent_id = row_data['Parent ID']
parent_name = row_data['Parent Name']
child_id = row_data['Child ID']
child_name = row_data['Child Name']
child_status = row_data['Child Status']
labels = row_data['Child Labels']
worksheet.cell(row=row_idx, column=1, value=parent_id)
worksheet.cell(
row=row_idx, column=1
).hyperlink = f'{jira_url}/browse/{parent_id}'
worksheet.cell(row=row_idx, column=1).font = Font(
underline='single', color='0563C1'
)
worksheet.cell(row=row_idx, column=2, value=parent_name)
worksheet.cell(row=row_idx, column=3, value=child_id)
worksheet.cell(
row=row_idx, column=3
).hyperlink = f'{jira_url}/browse/{child_id}'
worksheet.cell(row=row_idx, column=3).font = Font(
underline='single', color='0563C1'
)
worksheet.cell(row=row_idx, column=4, value=child_name)
worksheet.cell(row=row_idx, column=5, value=child_status)
worksheet.cell(row=row_idx, column=6, value=labels)
worksheet.column_dimensions['A'].width = 15
worksheet.column_dimensions['B'].width = 30
worksheet.column_dimensions['C'].width = 15
worksheet.column_dimensions['D'].width = 30
worksheet.column_dimensions['E'].width = 15
worksheet.column_dimensions['F'].width = 40
workbook.save(OUTPUT_FILE_PATH)
except Exception as ex:
logger.error(f'Failed to write Excel file: {ex}')
if __name__ == '__main__':
try:
main()
except Exception as ex:
logger.unhandled_error(ex)
Prompt.enter_to_exit()