-
Notifications
You must be signed in to change notification settings - Fork 0
/
jirautils.py
241 lines (181 loc) · 5.38 KB
/
jirautils.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
import migrationauth
import requests
root_url = 'https://issues.redhat.com'
base_url = f'{root_url}/rest/api/latest'
html_url = f'{root_url}/browse'
issue_url = f'{base_url}/issue'
project_key = 'ACM'
security_level = 'Red Hat Employee' # To be safe, restrict to RH Employees
contributors_field = 'customfield_12315950'
epic_field = 'customfield_12311141'
gh_issue_field = 'customfield_12316846'
severity_field = 'customfield_12316142'
story_points_field = 'customfield_12310243'
data = {
'projectKeys': project_key
}
headers = {
'Authorization': f'Bearer {migrationauth.JIRA_TOKEN}',
'Content-Type': 'application/json',
}
def get_user(user_query):
"""Get user object from query (username, name, or e-mail)"""
url = f'{base_url}/user/search'
data = {
'username': user_query
}
response = requests.get(
url,
headers=headers,
params=data,
)
if not response.ok:
print(
f"An unexpected response was returned from Jira: {response} {response.reason}")
exit(1)
return response.json()
def get_issue_types():
"""Get types of issues from Jira"""
url = f'{issue_url}/createmeta'
response = requests.get(
url,
headers=headers,
params=data,
)
return response.json()['projects'][0]['issuetypes']
def get_issue_meta(issue_type_name):
"""Get meta fields for an issue type"""
request_data = data
request_data['issuetypeNames'] = issue_type_name
request_data['expand'] = 'projects.issuetypes.fields'
url = f'{issue_url}/createmeta'
response = requests.get(
url,
headers=headers,
params=request_data,
)
return response.json()['projects'][0]['issuetypes'][0]
def get_transitions(issue_key):
"""Get available transitions for an issue"""
url = f'{issue_url}/{issue_key}/transitions'
data = {
'expand': 'transitions.fields'
}
return requests.get(
url,
headers=headers,
json=data
).json()
def do_transition(issue_key, target_status_name):
"""Execute a transition for an issue"""
target_status = None
transition_response = get_transitions(issue_key)
for transition in transition_response['transitions']:
if target_status_name == transition['name']:
target_status = {'id': transition['id']}
url = f'{issue_url}/{issue_key}/transitions'
data = {
'transition': target_status
}
return requests.post(
url,
headers=headers,
json=data
)
def create_issue(props):
"""Create Jira issue"""
url = issue_url
issue_type = props['issuetype']
request_data = {
'project': {
'key': project_key
},
'security': {
'name': security_level
},
'issuetype': issue_type,
'components': props['components'],
'summary': props['summary'],
'description': props['description'],
'reporter': props['reporter'],
'assignee': props['assignee'],
'priority': props['priority'],
'fixVersions': props['fixVersions'],
'labels': props['labels'],
# Custom "Contributors" field
contributors_field: props[contributors_field],
# Custom "Story Points" field
story_points_field: props[story_points_field],
# Custom "GitHub Issue" field
gh_issue_field: props[gh_issue_field]
}
if issue_type['name'] == 'Epic':
# Custom "Epic Name" field
request_data[epic_field] = props[epic_field]
elif issue_type['name'] == 'Bug':
# Custom "Severity" field
request_data[severity_field] = props[severity_field]
response = requests.post(
url,
json={'fields': request_data},
headers=headers,
)
if not response.ok:
print(
f'* An unexpected response was returned from Jira: {response} {response.reason}')
print(response.json())
exit(1)
return response.json()
def update_issue(issue_key, data):
"""Update existing Jira issue"""
url = f'{issue_url}/{issue_key}'
request_data = {
'update': {},
'fields': data
}
return requests.put(
url,
headers=headers,
json=request_data
)
def get_issue_from_url(api_url):
"""Get specific issue data given API URL"""
return requests.get(
api_url,
headers=headers,
)
def get_single_issue(issue_key):
"""Get specific issue data"""
url = f'{issue_url}/{issue_key}'
response = get_issue_from_url(url)
return response.json()
def search_issues(jql_query):
"""Get issues based on JQL query"""
url = f'{base_url}/search'
return requests.post(
url,
headers=headers,
json={
'jql': jql_query,
# 'fields': ['status']
}
).json()
def add_comment(issue_key, props):
"""Add comment given issue key and props"""
api_url = f'{issue_url}/{issue_key}/comment'
return add_comment_from_url(api_url, props)
def add_comment_from_url(api_url, props):
"""Add comment given API URL and props"""
request_data = {
'visibility': {
'type': 'group',
'value': security_level
},
'body': props['body']
}
response = requests.post(
api_url,
headers=headers,
json=request_data
)
return response.json()