-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzephyr.py
164 lines (125 loc) · 5.03 KB
/
zephyr.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
import json
import jwt
import time
import hashlib
import requests
def is_json(data):
try:
json.loads(data)
except ValueError:
return False
return True
# USER
USER = 'admin'
# ACCESS KEY from navigation >> Tests >> API Keys
ACCESS_KEY = ''
# ACCESS KEY from navigation >> Tests >> API Keys
SECRET_KEY = ''
# 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'
# CYCLE NAME FROM ZEPHYR
CYCLE_NAME = "API_REGRESSION"
PROJECT_ID = 10000
VERSION_ID = 10000
def post_result():
# RELATIVE PATH for token generation and make request to api
RELATIVE_PATH = '/public/rest/api/1.0/cycles/search?versionId={}&projectId={}'.format(VERSION_ID, PROJECT_ID)
# CANONICAL PATH (Http Method & Relative Path & Query String)
CANONICAL_PATH = 'GET&/public/rest/api/1.0/cycles/search&'+'projectId='+str(PROJECT_ID)+'&versionId=' + str(VERSION_ID)
# TOKEN HEADER: to generate jwt token
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())
}
# GENERATE TOKEN
token = jwt.encode(payload_token, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
# REQUEST HEADER: to authenticate and authorize api
headers = {
'Authorization': 'JWT '+token,
'Content-Type': 'text/plain',
'zapiAccessKey': ACCESS_KEY
}
# FIND CYCLE ID
raw_result = requests.get(BASE_URL + RELATIVE_PATH, headers=headers)
if is_json(raw_result.text):
# JSON RESPONSE: convert response to JSON
json_result = json.loads(raw_result.text)
for json_val in json_result:
if json_val['name'] == CYCLE_NAME:
cycle_id = json_val['id']
else:
print(raw_result.text)
# GET LIST OF ALL TESTS IN A CYCLE
RELATIVE_PATH = '/public/rest/api/1.0/executions/search/cycle/{}?versionId={}&projectId={}'.format(cycle_id, VERSION_ID, PROJECT_ID)
CANONICAL_PATH = 'GET&/public/rest/api/1.0/executions/search/cycle/'+cycle_id+'&'+'projectId='+str(PROJECT_ID)+'&versionId=' + str(VERSION_ID)
# TOKEN HEADER: to generate jwt token
payload_token2 = {
'sub': USER,
'qsh': hashlib.sha256(CANONICAL_PATH.encode('utf-8')).hexdigest(),
'iss': ACCESS_KEY,
'exp': int(time.time())+JWT_EXPIRE,
'iat': int(time.time())
}
# GENERATE TOKEN
token2 = jwt.encode(payload_token2, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
# REQUEST HEADER: to authenticate and authorize api
headers2 = {
'Authorization': 'JWT '+token2,
'Content-Type': 'text/plain',
'zapiAccessKey': ACCESS_KEY
}
result2 = requests.get(BASE_URL + RELATIVE_PATH, headers=headers2)
test_details = {}
test_cases_available = []
if is_json(result2.text):
# JSON RESPONSE: convert response to JSON
json_result2 = json.loads(result2.text)
for json_val in json_result2['searchObjectList']:
test_details['issueId'] = json_val['execution']['issueId']
test_details['executionId'] = json_val['execution']['id']
test_cases_available.append(test_details.copy())
# PRINT RESPONSE: pretty print with 4 indent
else:
print(result2.text)
# PICKING UP EXECUTION ID AND ISSUE ID OF ANY ONE TEST
execution_id = test_cases_available[0]['executionId']
issue_id = test_cases_available[0]['issueId']
# UPDATE TEST RESULT
RELATIVE_PATH = '/public/rest/api/1.0/execution/{}?issueId={}&projectId=10000'.format(execution_id, issue_id)
CANONICAL_PATH = 'PUT&/public/rest/api/1.0/execution/' + execution_id + '&' + 'issueId=' + str(issue_id) + '&projectId=' + str(PROJECT_ID)
# TOKEN HEADER: to generate jwt token
payload_token2 = {
'sub': USER,
'qsh': hashlib.sha256(CANONICAL_PATH.encode('utf-8')).hexdigest(),
'iss': ACCESS_KEY,
'exp': int(time.time())+JWT_EXPIRE,
'iat': int(time.time())
}
# GENERATE TOKEN
token2 = jwt.encode(payload_token2, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
# REQUEST HEADER: to authenticate and authorize api
headers2 = {
'Authorization': 'JWT '+token2,
'Content-Type': 'application/json',
'zapiAccessKey': ACCESS_KEY
}
payload = {
"cycleId": cycle_id,
"id": execution_id,
"issueId": issue_id,
"projectId": PROJECT_ID,
"status": {
"id": "1"
},
"versionId": VERSION_ID
}
result2 = requests.put(BASE_URL + RELATIVE_PATH, headers=headers2, json=payload)
print result2
print result2.text
if __name__ == "__main__":
post_result()