@@ -14,7 +14,7 @@ async def run_service(
1414 metadata : service_create_params .ServiceMetadataParam ,
1515 spec : service_create_params .ServiceSpecParam
1616) -> AsyncIterator [str ]:
17- reference = metadata [ "reference" ]
17+ reference = metadata . get ( "reference" )
1818 if not reference :
1919 raise ValueError ("metadata.reference is required" )
2020
@@ -25,15 +25,18 @@ async def run_service(
2525 }
2626 )).services
2727
28- service_id = ""
28+ service_id : Optional [ str ] = None
2929 if not services :
30- service_id = (await client .environments .automations .services .create (
30+ service = (await client .environments .automations .services .create (
3131 environment_id = environment_id ,
3232 spec = spec ,
3333 metadata = metadata
34- )).service .id
34+ )).service
35+ assert service is not None
36+ service_id = service .id
3537 else :
3638 service_id = services [0 ].id
39+ assert service_id is not None
3740
3841 await client .environments .automations .services .start (id = service_id )
3942 log_url = await wait_for_service_log_url (client , environment_id , service_id )
@@ -47,9 +50,9 @@ async def run_command(client: AsyncGitpod, environment_id: str, command: str) ->
4750 }
4851 )).tasks
4952
50- task_id = ""
53+ task_id : Optional [ str ] = None
5154 if not tasks :
52- task_id = (await client .environments .automations .tasks .create (
55+ task = (await client .environments .automations .tasks .create (
5356 spec = {
5457 "command" : command ,
5558 },
@@ -59,30 +62,40 @@ async def run_command(client: AsyncGitpod, environment_id: str, command: str) ->
5962 "description" : "Gitpod Python SDK Task" ,
6063 "reference" : TASK_REFERENCE ,
6164 },
62- )).task .id
65+ )).task
66+ assert task is not None
67+ task_id = task .id
6368 else :
6469 task_id = tasks [0 ].id
70+ assert task_id is not None
6571 await client .environments .automations .tasks .update (
6672 id = task_id ,
6773 spec = {
6874 "command" : command ,
6975 },
7076 )
71-
72- task_execution_id = (await client .environments .automations .tasks .start (id = task_id )).task_execution .id
77+ assert task_id is not None
78+ task_execution = (await client .environments .automations .tasks .start (id = task_id )).task_execution
79+ assert task_execution is not None
80+ task_execution_id = task_execution .id
81+ assert task_execution_id is not None
7382 log_url = await wait_for_task_log_url (client , environment_id , task_execution_id )
7483 return stream_logs (client , environment_id , log_url )
7584
7685async def wait_for_task_log_url (client : AsyncGitpod , environment_id : str , task_execution_id : str ) -> str :
77- async def get_log_url ():
86+ async def get_log_url () -> Optional [ str ] :
7887 execution = (await client .environments .automations .tasks .executions .retrieve (id = task_execution_id )).task_execution
88+ if not execution or not execution .status :
89+ return None
7990 return execution .status .log_url
8091
8192 return await wait_for_log_url (client , environment_id , task_execution_id , get_log_url , "RESOURCE_TYPE_TASK_EXECUTION" )
8293
8394async def wait_for_service_log_url (client : AsyncGitpod , environment_id : str , service_id : str ) -> str :
84- async def get_log_url ():
95+ async def get_log_url () -> Optional [ str ] :
8596 service = (await client .environments .automations .services .retrieve (id = service_id )).service
97+ if not service or not service .status :
98+ return None
8699 if service .status .phase != "SERVICE_PHASE_RUNNING" :
87100 return None
88101 return service .status .log_url
@@ -108,6 +121,8 @@ async def wait_for_log_url(client: AsyncGitpod, environment_id: str, resource_id
108121 finally :
109122 await event_stream .http_response .aclose ()
110123
124+ raise Exception ("Failed to get log URL" )
125+
111126async def stream_logs (client : AsyncGitpod , environment_id : str , log_url : str ) -> AsyncIterator [str ]:
112127 logs_access_token = (await client .environments .create_logs_token (environment_id = environment_id )).access_token
113128 async with httpx .AsyncClient () as http_client :
0 commit comments