-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrationutils.py
259 lines (201 loc) · 7.29 KB
/
migrationutils.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import utils.ghutils as ghutils
import utils.jirautils as jirautils
import utils.zenhubutils as zenhubutils
jira_product_versions = {}
gh_repo_id = ""
def user_map(gh_username, user_mapping, default_user=''):
"""Return the user e-mail from the usermap"""
assert user_mapping != None # user_mapping cannot be None
user = None
user_email = default_user
if gh_username in user_mapping:
user_email = user_mapping[gh_username]
if user_email != '':
user_query = jirautils.get_user(user_email)
if user_query and len(user_query) > 0:
user = {'name': user_query[0]['name']}
return user
def component_map(gh_labels, component_map):
"""Return the Jira components from a given GitHub label"""
components = []
component_count = 0
is_ui = False
for label in gh_labels:
label_name = str(label['name'])
if label_name.startswith('squad:'):
component_count += 1
if label_name in component_map:
components.append({'name': component_map[label_name]})
if label_name.endswith("-ui"):
is_ui = True
elif label_name in component_map:
component_count += 1
components.append({'name': component_map[label_name]})
return components, component_count, is_ui
def type_map(gh_labels):
"""Return the Jira issue type from a given GitHub label"""
type_map = {
'task': 'Task',
'bug': 'Bug',
'user_story': 'Story',
'Epic': 'Epic'
}
for label in gh_labels:
label_name = str(label['name'])
if label_name in type_map:
return type_map[label_name]
return 'Task'
def priority_map(gh_labels):
"""Return the Jira priority from a given GitHub label"""
priority_map = {
'blocker (P0)': 'Blocker',
'Priority/P1': 'Critical',
'Priority/P2': 'Normal',
'Priority/P3': 'Minor',
}
priority = {
'name': 'Undefined'
}
for label in gh_labels:
label_name = str(label['name'])
if label_name in priority_map:
if priority_map[label_name] != '':
priority['name'] = priority_map[label_name]
break
return priority
def severity_map(gh_labels):
"""Return the Jira severity from a given GitHub label"""
severity_map = {
'Severity 1 - Urgent': 'Critical',
'Severity 2 - Major': 'Moderate',
'Severity 3 - Minor': 'Low',
}
severity = {}
for label in gh_labels:
label_name = str(label['name'])
if label_name in severity_map:
if severity_map[label_name] != '':
severity['value'] = severity_map[label_name]
break
if 'value' in severity:
return severity
return None
def status_map(pipeline, issue_type):
"""Return equivalent Jira status for a given ZenHub pipeline"""
# Untriaged and Backlogs will remain in the state on creation ("New" or "To Do")
pipeline_map = {
"In Progress": {
"Bug": "ASSIGNED",
"Default": "In Progress"
},
"Awaiting Verification": {
"Bug": "ON_QA",
"Default": "Review",
"Epic": "Testing"
},
"Epics In Progress": "In Progress",
"Ready For Playback": {
"Bug": "ON_QA",
"Epic": "Testing",
"Default": "Review"
},
"Awaiting Docs": "In Progress",
"Closed": "Closed"
}
if pipeline in pipeline_map:
mapping_obj = pipeline_map[pipeline]
if isinstance(mapping_obj, str):
return mapping_obj
if issue_type in mapping_obj:
return mapping_obj[issue_type]
if 'Default' in mapping_obj:
return mapping_obj['Default']
return None
def should_close(gh_issue):
"""Return the whether an issue has a label signaling it should not be closed"""
no_close_labels = 'bugzilla,canary-failure'
return ghutils.has_label(gh_issue, no_close_labels)
def issue_map(gh_issue, component_mapping, user_mapping, default_user):
"""Return a dict for Jira to process from a given GitHub issue"""
assert user_mapping != None # user_mapping cannot be None
gh_labels = gh_issue['labels']
# Flag for whether the GitHub issue can be closed after migration
# Don't close the issue if:
# - It's connected to Bugzilla
# - It's a multi-squad issue
can_close = True
components, component_count, is_ui = component_map(
gh_labels, component_mapping)
if component_count > 1 or should_close(gh_issue):
can_close = False
assignee = None
contributors = []
for gh_assignee in gh_issue['assignees']:
assignee_id = user_map(gh_assignee['login'], user_mapping)
if assignee_id:
if assignee:
contributors.append(assignee_id)
else:
assignee = assignee_id
# Make sure a string is returned for the issue body
issue_body = ''
if gh_issue['body']:
issue_body = gh_issue['body']
issue_title = gh_issue['title']
issue_type = type_map(gh_labels)
# Fetch repo ID if not already populated
global gh_repo_id
if gh_repo_id == "":
gh_repo_id = str(ghutils.get_repo()['id'])
# Gather ZenHub issue data
zenhub_data = zenhubutils.get_issue_data(
gh_repo_id, str(gh_issue['number']))
releases = []
for release in zenhub_data['releases']:
# Only fetch if not already populated
if not issue_type in jira_product_versions:
version_response = jirautils.get_issue_meta(
issue_type)['fields']['fixVersions']['allowedValues']
if len(version_response) > 0:
jira_product_versions[issue_type] = list(
map(lambda version: version['name'], version_response))
for version in jira_product_versions[issue_type]:
if version == release:
releases.append({
'name': release
})
break
# Handle labels
labels = []
if is_ui:
labels.append('ui')
issue_mapping = {
'issuetype': {
'name': issue_type
},
'components': components,
'summary': issue_title,
'description': issue_body,
'reporter': user_map(gh_issue['user']['login'], user_mapping, default_user),
'assignee': assignee,
jirautils.contributors_field: contributors,
'status': status_map(zenhub_data['pipeline'], issue_type),
'priority': priority_map(gh_labels),
'fixVersions': releases,
'labels': labels,
jirautils.story_points_field: zenhub_data['estimate'],
jirautils.gh_issue_field: gh_issue['html_url']
}
if issue_type == 'Epic':
# Custom "Epic Name" field
issue_mapping[jirautils.epic_field] = issue_title
if issue_type == 'Bug':
# Custom "Severity" field
issue_mapping[jirautils.severity_field] = severity_map(gh_labels)
return issue_mapping, can_close
def comment_map(gh_comment):
"""Return a dict for Jira to process from a given GitHub comment"""
gh_user = gh_comment['user']['login']
return {
'body': f'{gh_comment["created_at"]} @{gh_user}\n{gh_comment["body"]}'
}