forked from alshurov13/allure-testops-mcp-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallure_client.py
More file actions
336 lines (285 loc) · 13.3 KB
/
allure_client.py
File metadata and controls
336 lines (285 loc) · 13.3 KB
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
"""
Allure TestOps REST API Client
Provides HTTP methods for interacting with Allure TestOps API
"""
import json
from typing import Any, Dict, Optional, List
from urllib.parse import urljoin
import httpx
class AllureClient:
"""HTTP client for Allure TestOps API"""
def __init__(self, base_url: str, token: str):
self.base_url = base_url.rstrip('/')
self.token = token
def _get_headers(self) -> Dict[str, str]:
"""Get HTTP headers for API requests"""
return {
'Authorization': f'Api-Token {self.token}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
def _build_url(self, path: str, params: Optional[Dict[str, Any]] = None) -> str:
"""Build full URL with query parameters"""
url = urljoin(self.base_url, path)
if params:
# Filter out None values and handle arrays
filtered_params = {}
for key, value in params.items():
if value is not None:
if isinstance(value, list):
# For arrays, we need to append each value
filtered_params[key] = value
else:
filtered_params[key] = value
# Build query string manually to handle arrays
query_parts = []
for key, value in filtered_params.items():
if isinstance(value, list):
for item in value:
query_parts.append(f"{key}={item}")
else:
query_parts.append(f"{key}={value}")
if query_parts:
url += '?' + '&'.join(query_parts)
return url
async def get(self, path: str, params: Optional[Dict[str, Any]] = None) -> Any:
"""Perform GET request"""
url = self._build_url(path, params)
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self._get_headers())
response.raise_for_status()
return response.json()
async def post(self, path: str, body: Optional[Any] = None, params: Optional[Dict[str, Any]] = None) -> Any:
"""Perform POST request"""
url = self._build_url(path, params)
async with httpx.AsyncClient() as client:
response = await client.post(
url,
headers=self._get_headers(),
json=body if body else None
)
response.raise_for_status()
return response.json()
async def patch(self, path: str, body: Any, params: Optional[Dict[str, Any]] = None) -> Any:
"""Perform PATCH request"""
url = self._build_url(path, params)
async with httpx.AsyncClient() as client:
response = await client.patch(
url,
headers=self._get_headers(),
json=body
)
response.raise_for_status()
return response.json()
async def put(self, path: str, body: Any, params: Optional[Dict[str, Any]] = None) -> Any:
"""Perform PUT request"""
url = self._build_url(path, params)
async with httpx.AsyncClient() as client:
response = await client.put(
url,
headers=self._get_headers(),
json=body
)
response.raise_for_status()
return response.json()
async def delete(self, path: str, params: Optional[Dict[str, Any]] = None) -> None:
"""Perform DELETE request"""
url = self._build_url(path, params)
async with httpx.AsyncClient() as client:
response = await client.delete(url, headers=self._get_headers())
response.raise_for_status()
# Test Case CRUD
async def get_test_cases(self, project_id: str, params: Optional[Dict[str, Any]] = None) -> Any:
"""Get all test cases for a project"""
query_params = {"projectId": project_id}
if params:
query_params.update(params)
return await self.get("/api/rs/testcase", query_params)
async def get_test_case(self, test_case_id: int) -> Any:
"""Get a specific test case by ID with overview (extended information)"""
return await self.get(f"/api/testcase/{test_case_id}/overview")
async def get_test_case_scenario(self, test_case_id: int) -> Any:
"""Get scenario (steps) for a test case"""
return await self.get(f"/api/testcase/{test_case_id}/step")
async def create_test_case_step(
self,
body: Dict[str, Any],
after_id: Optional[int] = None,
before_id: Optional[int] = None,
with_expected_result: bool = False
) -> Any:
"""Create a test case step
POST /api/testcase/step
Args:
body: Step body containing testCaseId and either bodyJson (for text) or attachmentId
after_id: Insert step after this step ID (mutually exclusive with before_id)
before_id: Insert step before this step ID (mutually exclusive with after_id)
with_expected_result: Include expected result section
Returns:
Created step object
"""
params = {}
if after_id is not None:
params['afterId'] = after_id
if before_id is not None:
params['beforeId'] = before_id
params['withExpectedResult'] = str(with_expected_result).lower()
return await self.post("/api/testcase/step", body, params)
async def update_test_case_step(
self,
step_id: int,
body: Dict[str, Any],
after_id: Optional[int] = None,
before_id: Optional[int] = None,
with_expected_result: bool = False
) -> Any:
"""Update a test case step
PATCH /api/testcase/step/:step_id
Args:
step_id: ID of the step to update
body: Step body with bodyJson (for text) or attachmentId (testCaseId not needed)
after_id: Insert step after this step ID (mutually exclusive with before_id)
before_id: Insert step before this step ID (mutually exclusive with after_id)
with_expected_result: Include expected result section
Returns:
Updated step object
"""
params = {}
if after_id is not None:
params['afterId'] = after_id
if before_id is not None:
params['beforeId'] = before_id
params['withExpectedResult'] = str(with_expected_result).lower()
return await self.patch(f"/api/testcase/step/{step_id}", body, params)
async def delete_test_case_step(self, step_id: int) -> None:
"""Delete a test case step
DELETE /api/testcase/step/:id
"""
await self.delete(f"/api/testcase/step/{step_id}")
async def create_test_case(self, project_id: str, test_case: Dict[str, Any]) -> Any:
"""Create a new test case"""
body = {**test_case, "projectId": int(project_id)}
return await self.post("/api/rs/testcase", body)
async def update_test_case(self, test_case_id: int, test_case: Dict[str, Any]) -> Any:
"""Update an existing test case"""
return await self.patch(f"/api/rs/testcase/{test_case_id}", test_case)
async def delete_test_case(self, test_case_id: int) -> None:
"""Delete a test case"""
await self.delete(f"/api/rs/testcase/{test_case_id}")
async def get_test_case_custom_fields(self, test_case_id: int, project_id: str) -> Any:
"""Get custom field values for a test case with v2 format
GET /api/testcase/:id/cfv?projectId=:project_id&v2=true
Returns list of custom fields with their current values
"""
return await self.get(f"/api/testcase/{test_case_id}/cfv", {"projectId": project_id, "v2": "true"})
async def get_available_custom_fields(self, project_id: int) -> Any:
"""Get available custom fields for test cases in a project
POST /api/testcase/cfv
Body: { "projectId": int, "ids": [test_case_ids] }
Returns list of available custom fields with their schemas
"""
body = {
"projectId": project_id
}
return await self.post("/api/testcase/cfv", body)
async def update_test_case_custom_fields(self, test_case_id: int, custom_fields: List[Dict[str, Any]]) -> Any:
"""Update custom field values for a test case
PATCH /api/testcase/{testCaseId}/cfv
Body: array of CustomFieldWithValuesDto
"""
return await self.post(f"/api/testcase/{test_case_id}/cfv", custom_fields)
async def get_custom_field_values(self, project_id: int, custom_field_id: int, size: int = 100, page: int = 0) -> Any:
"""Get possible values for a custom field in a project
GET /api/project/:project_id/cfv?customFieldId=:id&size=100&page=0
Returns paginated list of available custom field values
"""
params = {
"customFieldId": custom_field_id,
"size": size
}
if page > 0:
params["page"] = page
return await self.get(f"/api/project/{project_id}/cfv", params)
async def bulk_create_test_cases(self, project_id: str, test_cases: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Bulk create test cases"""
results = []
for test_case in test_cases:
try:
result = await self.create_test_case(project_id, test_case)
results.append({"success": True, "data": result})
except Exception as e:
results.append({"success": False, "error": str(e), "testCase": test_case})
return results
# Launch CRUD
async def get_launches(self, project_id: str, params: Optional[Dict[str, Any]] = None) -> Any:
"""Get all launches for a project"""
query_params = {"projectId": project_id}
if params:
query_params.update(params)
return await self.get("/api/rs/launch", query_params)
async def get_launch(self, launch_id: int) -> Any:
"""Get a specific launch by ID"""
return await self.get(f"/api/rs/launch/{launch_id}")
async def create_launch(self, project_id: str, launch: Dict[str, Any]) -> Any:
"""Create a new launch"""
body = {**launch, "projectId": int(project_id)}
return await self.post("/api/rs/launch", body)
async def update_launch(self, launch_id: int, launch: Dict[str, Any]) -> Any:
"""Update an existing launch"""
return await self.patch(f"/api/rs/launch/{launch_id}", launch)
async def delete_launch(self, launch_id: int) -> None:
"""Delete a launch"""
await self.delete(f"/api/rs/launch/{launch_id}")
async def close_launch(self, launch_id: int) -> Any:
"""Close a launch"""
return await self.post(f"/api/rs/launch/{launch_id}/close")
# Test Plan CRUD
async def get_test_plans(self, project_id: str, params: Optional[Dict[str, Any]] = None) -> Any:
"""Get all test plans for a project"""
query_params = {"projectId": project_id}
if params:
query_params.update(params)
return await self.get("/api/rs/testplan", query_params)
async def get_test_plan(self, test_plan_id: int) -> Any:
"""Get a specific test plan by ID"""
return await self.get(f"/api/rs/testplan/{test_plan_id}")
async def create_test_plan(self, project_id: str, test_plan: Dict[str, Any]) -> Any:
"""Create a new test plan"""
body = {**test_plan, "projectId": int(project_id)}
return await self.post("/api/rs/testplan", body)
async def update_test_plan(self, test_plan_id: int, test_plan: Dict[str, Any]) -> Any:
"""Update an existing test plan"""
return await self.patch(f"/api/rs/testplan/{test_plan_id}", test_plan)
async def delete_test_plan(self, test_plan_id: int) -> None:
"""Delete a test plan"""
await self.delete(f"/api/rs/testplan/{test_plan_id}")
# Comments
async def get_comments(self, test_case_id: int, page: Optional[int] = None, size: Optional[int] = None) -> Any:
"""Get comments for a test case
GET /api/comment?testCaseId=:id&page=0&size=25
page and size are optional, default size=10
"""
params = {"testCaseId": test_case_id}
if page is not None:
params["page"] = page
if size is not None:
params["size"] = size
return await self.get("/api/comment", params)
async def create_comment(self, test_case_id: int, body: str) -> Any:
"""Create a comment for a test case
POST /api/comment
Body: {"testCaseId": int, "body": str}
"""
comment_body = {
"testCaseId": test_case_id,
"body": body
}
return await self.post("/api/comment", comment_body)
async def delete_comment(self, comment_id: int) -> None:
"""Delete a comment
DELETE /api/comment/:id
"""
await self.delete(f"/api/comment/{comment_id}")
def create_allure_client(base_url: str, token: str) -> AllureClient:
"""Create and return AllureClient instance"""
return AllureClient(base_url, token)