@@ -14,7 +14,7 @@ async def run_service(
14
14
metadata : service_create_params .ServiceMetadataParam ,
15
15
spec : service_create_params .ServiceSpecParam
16
16
) -> AsyncIterator [str ]:
17
- reference = metadata [ "reference" ]
17
+ reference = metadata . get ( "reference" )
18
18
if not reference :
19
19
raise ValueError ("metadata.reference is required" )
20
20
@@ -25,15 +25,18 @@ async def run_service(
25
25
}
26
26
)).services
27
27
28
- service_id = ""
28
+ service_id : Optional [ str ] = None
29
29
if not services :
30
- service_id = (await client .environments .automations .services .create (
30
+ service = (await client .environments .automations .services .create (
31
31
environment_id = environment_id ,
32
32
spec = spec ,
33
33
metadata = metadata
34
- )).service .id
34
+ )).service
35
+ assert service is not None
36
+ service_id = service .id
35
37
else :
36
38
service_id = services [0 ].id
39
+ assert service_id is not None
37
40
38
41
await client .environments .automations .services .start (id = service_id )
39
42
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) ->
47
50
}
48
51
)).tasks
49
52
50
- task_id = ""
53
+ task_id : Optional [ str ] = None
51
54
if not tasks :
52
- task_id = (await client .environments .automations .tasks .create (
55
+ task = (await client .environments .automations .tasks .create (
53
56
spec = {
54
57
"command" : command ,
55
58
},
@@ -59,30 +62,40 @@ async def run_command(client: AsyncGitpod, environment_id: str, command: str) ->
59
62
"description" : "Gitpod Python SDK Task" ,
60
63
"reference" : TASK_REFERENCE ,
61
64
},
62
- )).task .id
65
+ )).task
66
+ assert task is not None
67
+ task_id = task .id
63
68
else :
64
69
task_id = tasks [0 ].id
70
+ assert task_id is not None
65
71
await client .environments .automations .tasks .update (
66
72
id = task_id ,
67
73
spec = {
68
74
"command" : command ,
69
75
},
70
76
)
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
73
82
log_url = await wait_for_task_log_url (client , environment_id , task_execution_id )
74
83
return stream_logs (client , environment_id , log_url )
75
84
76
85
async 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 ] :
78
87
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
79
90
return execution .status .log_url
80
91
81
92
return await wait_for_log_url (client , environment_id , task_execution_id , get_log_url , "RESOURCE_TYPE_TASK_EXECUTION" )
82
93
83
94
async 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 ] :
85
96
service = (await client .environments .automations .services .retrieve (id = service_id )).service
97
+ if not service or not service .status :
98
+ return None
86
99
if service .status .phase != "SERVICE_PHASE_RUNNING" :
87
100
return None
88
101
return service .status .log_url
@@ -108,6 +121,8 @@ async def wait_for_log_url(client: AsyncGitpod, environment_id: str, resource_id
108
121
finally :
109
122
await event_stream .http_response .aclose ()
110
123
124
+ raise Exception ("Failed to get log URL" )
125
+
111
126
async def stream_logs (client : AsyncGitpod , environment_id : str , log_url : str ) -> AsyncIterator [str ]:
112
127
logs_access_token = (await client .environments .create_logs_token (environment_id = environment_id )).access_token
113
128
async with httpx .AsyncClient () as http_client :
0 commit comments