-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzapi.py
369 lines (293 loc) · 13.2 KB
/
zapi.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import json
import jwt
import time
import hashlib
import requests
import datetime
from pprint import pprint
import xmltodict
import re
import sys
import platform
import random
import math
import os
# args $JENKINS_BUILD_NUM $ZEP_USER $ZEP_ACCESS_KEY $ZEP_SECRET_KEY
# USER
USER = 'admin'
# ACCESS KEY from navigation >> Tests >> API Keys
ACCESS_KEY = 'ZmQ2Nzk0OTktN2U3YS0zNWQ0LWIxYjgtM2I0MzBmM2FiNTdmIGFkbWluIFVTRVJfREVGQVVMVF9OQU1F'
# ACCESS KEY from navigation >> Tests >> API Keys
SECRET_KEY = 'wACKWxXWT9kYLS__bGU6K8aPiiUzEswscsq3MxL2X2Q'
# JWT EXPIRE how long token been to be active? 3600 == 1 hour
JWT_EXPIRE = 3600
# BASE URL for Zephyr for Jira Cloud
BASE_URL = 'https://prod-api.zephyr4jiracloud.com/connect'
pass_res = []
failed_res = []
unexecuted = []
final_result = {}
def is_json(data):
try:
json.loads(data)
except ValueError:
return False
return True
class zephyr_integration:
def __init__(self):
pass
# GENERATE TOKEN
# token = jwt.encode(payload_token, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
def create_token(self, rel_path):
RELATIVE_PATH = rel_path
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
payload_token = {
'sub': USER,
'qsh': hashlib.sha256(CANONICAL_PATH.encode('utf-8')).hexdigest(),
'iss': ACCESS_KEY,
'exp': int(time.time()) + JWT_EXPIRE,
'iat': int(time.time())
}
token = jwt.encode(payload_token, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
return token
def create_test_cycle(self, build_no=None):
rel_path_for_test_cycle = '/public/rest/api/1.0/cycle'
RELATIVE_PATH = rel_path_for_test_cycle
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
token = self.create_token(rel_path_for_test_cycle)
headers = {
'Authorization': 'JWT ' + token,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
# creat the test cycle name:
date = datetime.datetime.now()
# test_cycle_name = 'rest_test_'+datetime.datetime.now().strftime('%M:%S.%f')[:-4]
test_cycle_name = 'BVT-' + str(build_no) + ' ' + date.strftime("%D %H:%M")
cycle = {
'name': test_cycle_name,
'projectId': 10000,
'versionId': -1
}
# MAKE REQUEST:
# raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# PRINT RESPONSE: pretty print with 4 indent
print(json.dumps(json_result, indent=4, sort_keys=True))
print "------------------------------------------------"
return json_result['cycleIndex'], json_result['name']
else:
print(raw_result.text)
def add_tests_to_cycle(self, cycle_id):
# cycleindex="0001505977226965-242ac112-0001"
cycleindex = cycle_id
rel_path_for_add_tests = '/public/rest/api/1.0/executions/add/cycle/%s' % (cycleindex)
RELATIVE_PATH = rel_path_for_add_tests
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
token = self.create_token(rel_path_for_add_tests)
headers = {
'Authorization': 'JWT ' + token,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
# creat the test cycle name:
cycle = {"jql": "project = VRSC AND type = TEST AND LABELS = BVT", "versionId": -1, "projectId": 10000,
"method": "2"}
# MAKE REQUEST:
# raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# PRINT RESPONSE: pretty print with 4 indent
print(json.dumps(json_result, indent=4, sort_keys=True))
print "------------------------------------------------"
else:
print(raw_result.text)
# cycle = {"offset":0,"maxRecords":20,"fields":{},"zqlQuery":"project = \"VRSC\" AND fixVersion = \"Unscheduled\" AND cycleName = \"bvt\""}
def get_all_execution_ids_and_testnames(self, testname):
# cycleindex="0001505977226965-242ac112-0001"
jql = "project = \"VRSC\" AND fixVersion = \"Unscheduled\" AND cycleName = \"%s\"" % (testname)
RELATIVE_PATH = '/public/rest/api/1.0/zql/search'
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
token = self.create_token(RELATIVE_PATH)
headers = {
'Authorization': 'JWT ' + token,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
cycle = {"offset": 0, "maxRecords": 50, "fields": {}, "zqlQuery": jql}
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
total = ''
offset = ''
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# pprint (json_result)
print json_result.keys()
total = json_result['totalCount']
offset = json_result['currentOffset']
print offset, total
self.combine_offset_based_data(total, testname)
# searh_obj = json_result['searchObjectList']
# for each in searh_obj:
# # print each['issueKey'],each ['issueSummary'] , "\t executionID:" + each['execution']['id']
# final_result[each['issueKey'].strip()] = each['execution']['id']
# pprint(final_result)
# print "------------------------------------------------"
else:
print(raw_result.text)
def combine_offset_based_data(self, count, testname):
jql = "project = \"VRSC\" AND fixVersion = \"Unscheduled\" AND cycleName = \"%s\"" % (testname)
RELATIVE_PATH = '/public/rest/api/1.0/zql/search'
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
token = self.create_token(RELATIVE_PATH)
headers = {
'Authorization': 'JWT ' + token,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
print "calculating the how many offsets present"
float_data = count / 10.0
offset = int(math.ceil(float_data))
# Now for each offset send a post request and append the data so that all can be updated.
global final_result
for each in range(0, offset + 1):
print "inside each"
cycle = {"offset": each * 10, "maxRecords": 50, "fields": {}, "zqlQuery": jql}
pprint(cycle)
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
searh_obj = json_result['searchObjectList']
for each in searh_obj:
# print each['issueKey'],each ['issueSummary'] , "\t executionID:" + each['execution']['id']
final_result[each['issueKey'].strip()] = each['execution']['id']
pprint(final_result)
print "------------------------------------------------"
def update_status_in_jira(self):
RELATIVE_PATH = '/public/rest/api/1.0/executions'
CANONICAL_PATH = 'POST&' + RELATIVE_PATH + '&'
token = self.create_token(RELATIVE_PATH)
headers = {
'Authorization': 'JWT ' + token,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
print "updating PASS status in jira"
print pass_res
cycle = {"executions": pass_res, "status": 1, "clearDefectMappingFlag": False, "testStepStatusChangeFlag": True,
"stepStatus": 1}
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# PRINT RESPONSE: pretty print with 4 indent
print(json.dumps(json_result, indent=4, sort_keys=True))
else:
print(raw_result.text)
print "updating FAIL status in jira"
print failed_res
cycle = {"executions": failed_res, "status": 2, "clearDefectMappingFlag": False,
"testStepStatusChangeFlag": True, "stepStatus": 1}
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# PRINT RESPONSE: pretty print with 4 indent
print(json.dumps(json_result, indent=4, sort_keys=True))
else:
print(raw_result.text)
print "updating UNEXECUTED status in jira"
print unexecuted
cycle = {"executions": unexecuted, "status": -1, "clearDefectMappingFlag": False,
"testStepStatusChangeFlag": True, "stepStatus": 1}
raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
# PRINT RESPONSE: pretty print with 4 indent
print(json.dumps(json_result, indent=4, sort_keys=True))
else:
print(raw_result.text)
# Code from Raghu needs to be put here. for now use old jenkins junit.xml
def read_junit_xml(self, local_container_path=None):
print "reading the file from provided path"
reports_path = 'reports'
xml_files = os.listdir(reports_path)
for xml in xml_files:
local_container_path = os.path.join(reports_path, xml)
with open(local_container_path, "rb") as f: # notice the "rb" mode
d = xmltodict.parse(f, xml_attribs=True)
result_json = json.dumps(d, indent=4)
test_data = json.loads(result_json)['testsuite']['testcase']
failed = []
passed = []
result = {}
for each in test_data:
# result[each['@name']]= try: if each['failure']: return 'FAIL' else: return 'PASS'
try:
if each['failure']:
result[each['@name'].strip()] = 'FAIL'
except Exception as e:
result[each['@name'].strip()] = 'PASS'
pprint(result)
extracted_VRSC_val = {}
for each in result.iterkeys():
try:
match = re.search(r'^VRSC\-(\d){4}', each)
# print match.group()
extracted_VRSC_val[match.group()] = result[each]
except AttributeError:
# print each
extracted_VRSC_val[each] = result[each]
tests = {}
execution_ids = final_result
print "debug xml"
print execution_ids
for test in extracted_VRSC_val:
tests[test] = extracted_VRSC_val[test]
# with open("jira_output.txt", 'r') as infile:
# for line in infile:
# execution_ids[line.split(",")[0]] = line.split(",")[1].strip()
# print "extension ids for tests" % tests
def prepare_final_data(res):
# pass_res=[]
# failed_res = []
# unexecuted = []
for key, val in res.iteritems():
if 'PASS' in val and len(val) == 2:
# print "pass id's"
pass_res.append(val[1])
elif 'FAIL' in val and len(val) == 2:
# print "failed id's"
failed_res.append(val[1])
elif len(val) == 1:
if not (val[0].isalnum()):
unexecuted.append(val[0])
# print pass_res,failed_res,unexecuted
# print failed_res,unexecuted
def combineDictVal(*args):
result = {}
for dic in args:
for key in (result.viewkeys() | dic.keys()):
if key in dic:
result.setdefault(key, []).append(dic[key])
return result
res = (combineDictVal(tests, execution_ids))
print "res is %s" % (res)
prepare_final_data(res)
if __name__ == '__main__':
build_no = int(time.time())
z = zephyr_integration()
cycle_id, name = z.create_test_cycle(build_no)
# name = 'BVT-138 10/05/17 18:48'
# cycle_id = '0001507209531832-242ac112-0001'
z.add_tests_to_cycle(cycle_id)
z.get_all_execution_ids_and_testnames(name)
z.read_junit_xml()
z.update_status_in_jira()