-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathissue_normalizer.py
146 lines (131 loc) · 5.18 KB
/
issue_normalizer.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
141
142
143
144
145
146
import collections
import csv
import os
from urllib import parse
critical_fields = {
"Assignee": "Assignee",
"Attachment": "Attachment",
"Custom field (Capital Expenditure (CapEx) Hours)": "Capital Expenditure (CapEx) Hours",
"Comment": "Comment",
"Created": "Created",
"Description": "Description",
"Custom field (Developer)": "Developer",
"Custom field (Epic Colour)": "Epic Color",
"Custom field (Epic Link)": "Epic Link",
"Custom field (Epic Name)": "Epic Name",
"Custom field (Epic Status)": "Epic Status",
"Fix Version/s": "Fix Version/s",
"Custom field (Flagged)": "Flagged",
"Issue id": "Issue id",
"Issue key": "Issue key",
"Issue Type": "Issue Type",
"Labels": "Labels",
"Outward issue link (Blocks)": "Outward issue link (Blocks)",
"Outward issue link (Cloners)": "Outward issue link (Cloners)",
"Outward issue link (Duplicate)": "Outward issue link (Duplicate)",
"Outward issue link (Relates)": "Outward issue link (Relates)",
"Parent id": "Parent id",
"Priority": "Priority",
"Custom field (Rank)": "Rank",
"Reporter": "Reporter",
"Resolution": "Resolution",
"Resolved": "Resolved",
'"Custom field (Set to ""To Do"" Status (Ready for active development/work)?)"': 'Set as ToDo Status (Ready for Dev?)',
"Sprint": "Sprint",
"Status": "Status",
"Custom field (Story Points)": "Story Points",
"Summary": "Summary"
}
old_field_to_new = dict((v,k) for k,v in critical_fields.items())
class Counts:
max_counts = None
def get_max_counts():
if Counts.max_counts == None:
Counts.max_counts = Counts._calculate_header_counts()
return Counts.max_counts
def _calculate_header_counts():
max_counts = collections.Counter()
csvs = os.listdir('jiratix')
for tickets_dump in csvs:
my_counts = collections.Counter()
with open(f'./jiratix/{tickets_dump}') as f:
headers = str.strip(f.readline())
for header in headers.split(','):
my_counts[header] += 1
max_counts = max_counts | my_counts
return max_counts
class Issue:
def __init__(self):
self.data = {}
for field, count in Counts.get_max_counts().items():
self.data[field] = [''] * count
def add_field(self, header, field):
# id like to use a generator here but not gonna take the
# code complexity hit -- for this throwaway script i'll
# take the time complexity hit in exchange
# for i in range(len(self.data[header])):
# if self.data[header][i] == "":
# self.data[header][i] = field
# break
self.data[header].insert(0,field)
self.data[header].pop()
def to_csv_as_list(self):
output = []
for k in sorted(critical_fields.keys()): # be sure to sort!!!
if k == "Comment":
output += self.data[k]
# import pdb; pdb.set_trace()
# output += reversed(self.data[k]) # comments will be ingested in reverse order
elif k == "Attachment":
escpd_attchmnts = []
for attachment in self.data[k]:
# here goes, some uply code
splt_fields = attachment.split(';')
parsed_url = parse.urlparse(splt_fields[-1])
newurl = parsed_url._replace(path=parse.quote(parsed_url.path)).geturl()
# print(newurl)
splt_fields[-1] = newurl
escpd_attchmnts.append(';'.join(splt_fields))
output += escpd_attchmnts
else:
output += self.data[k]
return output
def std_header():
"""
abc sorted csv header with correct number of fields
"""
header_counts = Counts.get_max_counts()
csvheader = ""
for k in sorted(critical_fields.keys()): # be sure to sort!!!
# csvheader += f'{critical_fields[k]},' * header_counts[k]
for n in range(header_counts[k]):
csvheader += f'{critical_fields[k]}{n},'
return csvheader.rstrip(',')
def consume_files():
csv.field_size_limit(999999)
csvs = os.listdir('jiratix')
issues = []
for tickets_dump in csvs:
# for tickets_dump in ['jira-tix-1000.csv']:
with open(f'./jiratix/{tickets_dump}') as f:
special_header = str.strip(f.readline())
headers_array = special_header.split(',')
tickets = csv.reader(f)
# import pdb; pdb.set_trace()
for ticket in tickets:
issue = Issue()
for idx, field in enumerate(ticket):
hdr = headers_array[idx]
issue.add_field(hdr, field)
issues.append(issue)
return issues
def write_output_csv(ticket_corpus):
with open('normalized-jira-tix.csv', 'w') as f:
f.write(Issue.std_header())
f.write('\n')
writer = csv.writer(f)
for t in ticket_corpus:
writer.writerow(t.to_csv_as_list())
ticket_corpus = consume_files()
# import pdb; pdb.set_trace()
write_output_csv(ticket_corpus)