1313from aio .core .functional import async_property
1414from aio .core .tasks import concurrent
1515
16- from envoy .ci .report import exceptions
16+ from envoy .ci .report import exceptions , typing
1717
1818URL_GH_REPO_ACTION_ENV_ARTIFACT = "actions/runs/{wfid}/artifacts?name=env"
1919URL_GH_REPO_ACTIONS = "actions/runs?per_page=100"
@@ -41,7 +41,7 @@ def __init__(
4141 else sort_ascending )
4242
4343 @async_property
44- async def as_dict (self ) -> dict :
44+ async def as_dict (self ) -> typing . CIRunsDict :
4545 return self ._sorted (await self ._to_dict ())
4646
4747 @async_property (cache = True )
@@ -57,7 +57,7 @@ async def check_runs(self) -> dict:
5757 return result
5858
5959 @async_property (cache = True )
60- async def envs (self ) -> dict :
60+ async def envs (self ) -> typing . CIRequestEnvsDict :
6161 artifacts : dict = {}
6262 async for result in concurrent (self ._env_fetches ):
6363 if not result :
@@ -214,40 +214,57 @@ async def _resolve_env_artifact_url(self, wfid: int) -> str | None:
214214 except IndexError :
215215 log .warning (f"Unable to find request artifact: { wfid } " )
216216
217- def _sorted (self , runs : dict ) -> dict :
217+ def _sorted (self , runs : typing . CIRunsDict ) -> typing . CIRunsDict :
218218 max_or_min = (
219219 min
220220 if self .sort_ascending
221221 else max )
222222 return dict (
223223 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 (),
230225 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 ()),
233229 reverse = not self .sort_ascending ))
234230
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
240254
241255 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