-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
271 lines (243 loc) · 8.71 KB
/
Copy pathmain.py
File metadata and controls
271 lines (243 loc) · 8.71 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
import asyncio
import httpx
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from app.models import (
AnalyzeRequest,
AnalyzeResponse,
AnalyzeSummary,
FailureWithJira,
JiraTicket,
ParseRequest,
)
from app.allure import fetch_allure_failures
from app.jira import search_jira_for_test, get_jira_ticket
from app.config import settings
app = FastAPI(
title="Allure Failure + Jira Analyzer",
description=(
"Fetches failed/broken MFTF tests from an Allure report "
"and searches Jira for open tickets related to those failures."
),
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/analyze", response_model=AnalyzeResponse)
async def analyze(request: AnalyzeRequest):
"""
Main endpoint:
1. Fetches the Allure report JSON from the provided URL.
2. Extracts all failed and broken tests.
3. Searches Jira for open tickets mentioning each MFTF test name
in title or description.
Pass jira_base_url and jira_token in the request body to override .env values.
"""
# --- Step 1: fetch Allure data ---
try:
failures, total_scanned = await fetch_allure_failures(
request.allure_url,
auth_token=request.allure_auth_token,
)
except httpx.HTTPStatusError as e:
raise HTTPException(
status_code=502,
detail=f"Failed to fetch Allure report: HTTP {e.response.status_code} from {request.allure_url}",
)
except httpx.RequestError as e:
raise HTTPException(
status_code=502,
detail=f"Network error fetching Allure report: {str(e)}",
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error parsing Allure report: {str(e)}",
)
if not failures:
return AnalyzeResponse(
summary=AnalyzeSummary(
total_tests_scanned=total_scanned,
total_failed=0,
total_broken=0,
total_with_jira_tickets=0,
allure_url=request.allure_url,
),
results=[],
)
# --- Step 2: search Jira concurrently for all failures ---
jira_configured = bool(
(request.jira_base_url or settings.jira_base_url)
and (request.jira_token or settings.jira_token)
)
async def enrich_with_jira(failure) -> FailureWithJira:
if not jira_configured:
return FailureWithJira(
test=failure,
jira_tickets=[],
jira_search_performed=False,
jira_error="Jira not configured — set JIRA_BASE_URL and JIRA_TOKEN",
)
try:
tickets = await search_jira_for_test(
mftf_test_name=failure.mftf_test_name,
base_url=request.jira_base_url,
token=request.jira_token,
)
return FailureWithJira(
test=failure,
jira_tickets=tickets,
jira_search_performed=True,
)
except httpx.HTTPStatusError as e:
return FailureWithJira(
test=failure,
jira_tickets=[],
jira_search_performed=True,
jira_error=f"Jira API error: HTTP {e.response.status_code}",
)
except ValueError as e:
return FailureWithJira(
test=failure,
jira_tickets=[],
jira_search_performed=False,
jira_error=str(e),
)
except Exception as e:
return FailureWithJira(
test=failure,
jira_tickets=[],
jira_search_performed=True,
jira_error=str(e),
)
results = await asyncio.gather(*[enrich_with_jira(f) for f in failures])
# --- Step 3: build summary ---
total_failed = sum(1 for r in results if r.test.status == "failed")
total_broken = sum(1 for r in results if r.test.status == "broken")
total_with_jira = sum(1 for r in results if r.jira_tickets)
return AnalyzeResponse(
summary=AnalyzeSummary(
total_tests_scanned=total_scanned,
total_failed=total_failed,
total_broken=total_broken,
total_with_jira_tickets=total_with_jira,
allure_url=request.allure_url,
),
results=list(results),
)
@app.get("/jira/ticket/{ticket_key}", response_model=JiraTicket)
async def fetch_jira_ticket(
ticket_key: str,
jira_base_url: str = Query(default=None),
jira_token: str = Query(default=None),
):
"""
Fetch a specific Jira ticket by key (e.g. MC-12345).
Returns summary, status, and description.
"""
try:
ticket = await get_jira_ticket(
ticket_key=ticket_key,
base_url=jira_base_url,
token=jira_token,
)
return ticket
except httpx.HTTPStatusError as e:
raise HTTPException(
status_code=e.response.status_code,
detail=f"Jira returned {e.response.status_code} for ticket {ticket_key}",
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/jira/search")
async def search_jira(
test_name: str = Query(..., description="MFTF test name to search for"),
jira_base_url: str = Query(default=None),
jira_token: str = Query(default=None),
):
"""
Search Jira for open tickets mentioning a specific MFTF test name
in their title or description.
"""
try:
tickets = await search_jira_for_test(
mftf_test_name=test_name,
base_url=jira_base_url,
token=jira_token,
)
return {"test_name": test_name, "tickets": tickets, "count": len(tickets)}
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except httpx.HTTPStatusError as e:
raise HTTPException(
status_code=502,
detail=f"Jira API error: HTTP {e.response.status_code}",
)
@app.post("/parse", response_model=AnalyzeResponse)
async def parse_allure_json(request: ParseRequest):
"""
Alternative to /analyze — accepts the raw Allure JSON directly instead of a URL.
Useful when the report is behind auth that can't be passed as a token,
or when the caller already has the JSON (e.g. downloaded locally).
Usage: POST the contents of suites.json or behaviors.json as { "allure_data": {...} }
"""
from app.allure import _walk, _count_nodes
from app.models import TestFailure
failures: list[TestFailure] = []
_walk(request.allure_data, failures)
total_scanned = _count_nodes(request.allure_data)
if not failures:
return AnalyzeResponse(
summary=AnalyzeSummary(
total_tests_scanned=total_scanned,
total_failed=0,
total_broken=0,
total_with_jira_tickets=0,
allure_url="(parsed from request body)",
),
results=[],
)
jira_configured = bool(
(request.jira_base_url or settings.jira_base_url)
and (request.jira_token or settings.jira_token)
)
async def enrich(failure) -> FailureWithJira:
if not jira_configured:
return FailureWithJira(
test=failure,
jira_tickets=[],
jira_search_performed=False,
jira_error="Jira not configured",
)
try:
tickets = await search_jira_for_test(
mftf_test_name=failure.mftf_test_name,
base_url=request.jira_base_url,
token=request.jira_token,
)
return FailureWithJira(test=failure, jira_tickets=tickets, jira_search_performed=True)
except Exception as e:
return FailureWithJira(
test=failure, jira_tickets=[], jira_search_performed=True, jira_error=str(e)
)
results = await asyncio.gather(*[enrich(f) for f in failures])
total_failed = sum(1 for r in results if r.test.status == "failed")
total_broken = sum(1 for r in results if r.test.status == "broken")
total_with_jira = sum(1 for r in results if r.jira_tickets)
return AnalyzeResponse(
summary=AnalyzeSummary(
total_tests_scanned=total_scanned,
total_failed=total_failed,
total_broken=total_broken,
total_with_jira_tickets=total_with_jira,
allure_url="(parsed from request body)",
),
results=list(results),
)