forked from openedx/repo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_tickets.py
140 lines (115 loc) · 4.53 KB
/
longest_tickets.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
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
#!/usr/bin/env python
"""
Parses JIRA a little to get at specific dates, quickly.
Doesn't run the scraper, run transitions_kpi.py to get that
"""
from transitions_kpi import parse_jira_info, engineering_time_spent, scrape_jira
import argparse
import datetime
import sys
# Valid open states in the ospr jira ticket workflow
OSPR_STATES = [
'Needs Triage',
'Waiting on Author',
'Blocked by Other Work',
'Product Review',
'Community Manager Review',
'Awaiting Prioritization',
'Engineering Review',
'All Engineering',
'All',
]
def longest_open_per_state(tickets, current=True):
"""
Returns the amount of time & ticket # for the longest amt of time
spent in engineering overall, triage, and awaiting prioritization.
current: only report on currently open tickets
"""
leng, ltri, lap = datetime.timedelta(0), datetime.timedelta(0), datetime.timedelta(0)
tixe = tixt = tixap = ''
for ticket in tickets:
if current and ticket.get('resolution', False):
continue
teng = engineering_time_spent(ticket['states'])
if teng and teng > leng:
leng = teng; tixe = ticket['issue']
ttri = ticket['states'].get('Needs Triage', False)
if ttri and ttri > ltri:
ltri = ttri; tixt = ticket['issue']
tap = ticket['states'].get('Awaiting Prioritization', False)
if tap and tap > lap:
lap = tap; tixap = ticket['issue']
if current:
print("Longest amount spent in each state, over currently-open tickets:")
else:
print("Historic longest time tickets:")
print(f"Longest amount spent in Engineering states: {leng} ({tixe})")
print(f"Longest amount spent in Needs Triage: {ltri} ({tixt})")
print(f"Longest amount spent in Awaiting Prioritization: {lap} ({tixap})")
def all_with_length(tickets, state):
"""
Show currently-open tickets in the given state, sorted by amount of time they've been there.
"""
if state not in OSPR_STATES:
print(f"Validation error: Unrecognized state '{state}'")
print(f"Valid states are: {OSPR_STATES}")
return
target_tickets = []
for ticket in tickets:
if ticket.get('current', None) == state:
target_tickets.append((ticket['issue'], ticket['states'][state]))
elif state == 'All Engineering' and not ticket.get('resolution', False):
target_tickets.append(
(ticket['issue'], engineering_time_spent(ticket['states']), ticket['current'])
)
elif state == 'All' and not ticket.get('resolution', False):
total_time = datetime.timedelta(0)
for __, tdelta in ticket['states'].iteritems():
total_time += tdelta
target_tickets.append(
(ticket['issue'], total_time, ticket['current'])
)
# sort these by how long they've been open.
target_tickets.sort(key=lambda x: x[1])
target_tickets.reverse()
if state == 'All Engineering' or state == 'All':
print(f"Issue Number (Time Spent in {state}) - Current state")
for issue, time, current in target_tickets:
print(f"{issue} ({time}) - {current}")
return
print(f"Issue Number (Time Spent in {state})")
for issue, time in target_tickets:
print(f"{issue} ({time})")
def main(argv):
"""a docstring for main, really?"""
parser = argparse.ArgumentParser(description="Get information about the tickets open the longest :(\nDefaults to --state='All'")
parser.add_argument(
"--scrape", action="store_true",
help="Rescrape JIRA"
)
parser.add_argument(
"--longest", action="store_true",
help="Show the longest amount of time spent in each state, over currently-open tickets"
)
parser.add_argument(
"--historic", action="store_true", default=False,
help="Show the historic longest open tickets, per state"
)
parser.add_argument(
"--state", type=str,
help="Show currently-open tickets in the given state, sorted by amount of time they've been there."
)
args = parser.parse_args(argv[1:])
if args.scrape:
scrape_jira()
tickets = parse_jira_info()
if args.state:
# TODO (potentially) report over historic data
all_with_length(tickets, args.state)
elif args.longest or args.historic:
current = not args.historic
longest_open_per_state(tickets, current)
else:
all_with_length(tickets, 'All')
if __name__ == "__main__":
sys.exit(main(sys.argv))