13
13
from aio .core .functional import async_property
14
14
from aio .core .tasks import concurrent
15
15
16
- from envoy .ci .report import exceptions
16
+ from envoy .ci .report import exceptions , typing
17
17
18
18
URL_GH_REPO_ACTION_ENV_ARTIFACT = "actions/runs/{wfid}/artifacts?name=env"
19
19
URL_GH_REPO_ACTIONS = "actions/runs?per_page=100"
@@ -41,7 +41,7 @@ def __init__(
41
41
else sort_ascending )
42
42
43
43
@async_property
44
- async def as_dict (self ) -> dict :
44
+ async def as_dict (self ) -> typing . CIRunsDict :
45
45
return self ._sorted (await self ._to_dict ())
46
46
47
47
@async_property (cache = True )
@@ -57,7 +57,7 @@ async def check_runs(self) -> dict:
57
57
return result
58
58
59
59
@async_property (cache = True )
60
- async def envs (self ) -> dict :
60
+ async def envs (self ) -> typing . CIRequestEnvsDict :
61
61
artifacts : dict = {}
62
62
async for result in concurrent (self ._env_fetches ):
63
63
if not result :
@@ -214,40 +214,57 @@ async def _resolve_env_artifact_url(self, wfid: int) -> str | None:
214
214
except IndexError :
215
215
log .warning (f"Unable to find request artifact: { wfid } " )
216
216
217
- def _sorted (self , runs : dict ) -> dict :
217
+ def _sorted (self , runs : typing . CIRunsDict ) -> typing . CIRunsDict :
218
218
max_or_min = (
219
219
min
220
220
if self .sort_ascending
221
221
else max )
222
222
return dict (
223
223
sorted (
224
- ((k ,
225
- sorted (
226
- v ,
227
- key = lambda event : event ["request" ]["started" ],
228
- reverse = not self .sort_ascending ))
229
- for k , v in runs .items ()),
224
+ runs .items (),
230
225
key = lambda item : max_or_min (
231
- x ["request" ]["started" ]
232
- for x in item [1 ]),
226
+ request ["started" ]
227
+ for request
228
+ in item [1 ]["requests" ].values ()),
233
229
reverse = not self .sort_ascending ))
234
230
235
- async def _to_dict (self ) -> dict :
236
- return {
237
- commit : requests
238
- for commit , request in (await self .workflow_requests ).items ()
239
- if (requests := await self ._to_list_request (commit , request ))}
231
+ async def _to_dict (self ) -> typing .CIRunsDict :
232
+ wf_requests = await self .workflow_requests
233
+ result : dict = {}
234
+ for commit , _requests in wf_requests .items ():
235
+ requests = await self ._to_list_request (commit , _requests )
236
+ if not requests :
237
+ continue
238
+ result [commit ] = {}
239
+ result [commit ]["head" ] = dict (
240
+ message = requests [0 ]["request" ]["message" ],
241
+ target_branch = requests [0 ]["request" ]["target-branch" ])
242
+ result [commit ]["requests" ] = {}
243
+ for request in requests :
244
+ result [commit ]["requests" ][request ["request_id" ]] = result [
245
+ commit ]["requests" ].get (
246
+ request ["request_id" ],
247
+ dict (event = request ["event" ],
248
+ started = request ["request" ]["started" ],
249
+ workflows = {}))
250
+ result [commit ]["requests" ][
251
+ request ["request_id" ]]["workflows" ][
252
+ request ["workflow_id" ]] = request ["workflow" ]
253
+ return result
240
254
241
255
async def _to_list_request (self , commit : str , request : dict ) -> list [dict ]:
242
- return [
243
- {"event" : event ,
244
- "request" : (await self .envs )[commit ][event ][req ]["request" ],
245
- "request_id" : req ,
246
- "check_name" : check_run ["name" ],
247
- "workflow_id" : check_run ["external_id" ],
248
- "workflow" : (await self .workflows )[int (check_run ["external_id" ])]}
249
- for event , requests in request .items ()
250
- for check_run in (
251
- await self .check_runs ).get (
252
- commit , {}).get (event , [])
253
- for req in requests ]
256
+ return sorted (
257
+ [{"event" : event ,
258
+ "request" : (await self .envs )[commit ][event ][req ]["request" ],
259
+ "request_id" : req ,
260
+ "check_name" : check_run ["name" ],
261
+ "workflow_id" : check_run ["external_id" ],
262
+ "workflow" : (await self .workflows )[
263
+ int (check_run ["external_id" ])]}
264
+ for event , requests in request .items ()
265
+ for check_run in (
266
+ await self .check_runs ).get (
267
+ commit , {}).get (event , [])
268
+ for req in requests ],
269
+ key = lambda item : item ["request" ]["started" ],
270
+ reverse = not self .sort_ascending )
0 commit comments