File tree Expand file tree Collapse file tree 2 files changed +22
-14
lines changed Expand file tree Collapse file tree 2 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -286,3 +286,20 @@ export const hasJobError = (workspace: TypesGen.Workspace) => {
286
286
export const paramsUsedToCreateWorkspace = (
287
287
param : TypesGen . TemplateVersionParameter ,
288
288
) => ! param . ephemeral ;
289
+
290
+ export const getMatchingAgentOrFirst = (
291
+ workspace : TypesGen . Workspace ,
292
+ agentName : string | undefined ,
293
+ ) : TypesGen . WorkspaceAgent | undefined => {
294
+ return workspace . latest_build . resources
295
+ . map ( ( resource ) => {
296
+ if ( ! resource . agents || resource . agents . length === 0 ) {
297
+ return ;
298
+ }
299
+ if ( ! agentName ) {
300
+ return resource . agents [ 0 ] ;
301
+ }
302
+ return resource . agents . find ( ( agent ) => agent . name === agentName ) ;
303
+ } )
304
+ . filter ( ( a ) => a ) [ 0 ] ;
305
+ } ;
Original file line number Diff line number Diff line change 1
1
import { assign , createMachine } from "xstate" ;
2
2
import * as API from "api/api" ;
3
3
import * as TypesGen from "api/typesGenerated" ;
4
+ import { getMatchingAgentOrFirst } from "utils/workspace" ;
4
5
5
6
interface ReconnectingPTYRequest {
6
7
readonly data ?: string ;
@@ -196,20 +197,10 @@ export const terminalMachine =
196
197
if ( ! context . workspace || ! context . workspaceName ) {
197
198
throw new Error ( "workspace or workspace name is not set" ) ;
198
199
}
199
-
200
- const agent = context . workspace . latest_build . resources
201
- . map ( ( resource ) => {
202
- if ( ! resource . agents || resource . agents . length === 0 ) {
203
- return ;
204
- }
205
- if ( ! context . agentName ) {
206
- return resource . agents [ 0 ] ;
207
- }
208
- return resource . agents . find (
209
- ( agent ) => agent . name === context . agentName ,
210
- ) ;
211
- } )
212
- . filter ( ( a ) => a ) [ 0 ] ;
200
+ const agent = getMatchingAgentOrFirst (
201
+ context . workspace ,
202
+ context . agentName ,
203
+ ) ;
213
204
if ( ! agent ) {
214
205
throw new Error ( "no agent found with id" ) ;
215
206
}
You can’t perform that action at this time.
0 commit comments