Skip to content

Commit 1b201f0

Browse files
author
andypalmi
committed
feat: add automation/import-flow action
Closes #172
1 parent bf79995 commit 1b201f0

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

resources/expertAutomations.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const GET_NODES = 'automation/get-nodes'
66
const EDIT_NODE = 'automation/open-node-edit'
77
const SEARCH = 'automation/search'
88
const ADD_FLOW_TAB = 'automation/add-flow-tab'
9+
const IMPORT_FLOW = 'automation/import-flow'
910

1011
/**
11-
* @typedef {SELECT_NODES|GET_NODES|EDIT_NODE|SEARCH|ADD_FLOW_TAB} ExpertAutomationsActionsEnum
12+
* @typedef {SELECT_NODES|GET_NODES|EDIT_NODE|SEARCH|ADD_FLOW_TAB|IMPORT_FLOW} ExpertAutomationsActionsEnum
1213
*/
1314

1415
export class ExpertAutomations extends ExpertActionsInterface {
@@ -98,6 +99,28 @@ export class ExpertAutomations extends ExpertActionsInterface {
9899
}
99100
}
100101
}
102+
,
103+
[IMPORT_FLOW]: {
104+
params: {
105+
type: 'object',
106+
properties: {
107+
flow: {
108+
type: ['string', 'array'],
109+
description: 'Flow JSON string or array to import onto the canvas'
110+
},
111+
addFlow: {
112+
type: 'boolean',
113+
description: 'Whether to create a new tab for the imported nodes (true) or import into the current tab (false). Default: false'
114+
},
115+
generateIds: {
116+
type: 'boolean',
117+
description: 'Whether to regenerate node IDs during import. Default: true.',
118+
default: true
119+
}
120+
},
121+
required: ['flow']
122+
}
123+
}
101124
})
102125

103126
/**
@@ -200,7 +223,7 @@ export class ExpertAutomations extends ExpertActionsInterface {
200223
throw e
201224
}
202225
}
203-
this.RED.view.importNodes(newNodes, { generateIds, addFlow, notify })
226+
this.RED.view.importNodes(newNodes, { generateIds, addFlow, notify, touchImport: true })
204227
}
205228

206229
async addFlowTab (title) {
@@ -300,6 +323,10 @@ export class ExpertAutomations extends ExpertActionsInterface {
300323
}
301324
break
302325

326+
case IMPORT_FLOW:
327+
this.importFlow(params.flow, { addFlow: params.addFlow, generateIds: params.generateIds ?? true })
328+
result.success = true
329+
break
303330
default:
304331
result.handled = false
305332
result.success = false

test/unit/resources/expertAutomations.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describeMain('expertAutomations', () => {
6565
it('should have supported actions', () => {
6666
const supportedActions = expertAutomations.supportedActions
6767
supportedActions.should.be.an.Object()
68-
supportedActions.should.only.have.keys('automation/get-nodes', 'automation/select-nodes', 'automation/open-node-edit', 'automation/search', 'automation/add-flow-tab')
68+
supportedActions.should.only.have.keys('automation/get-nodes', 'automation/select-nodes', 'automation/open-node-edit', 'automation/search', 'automation/add-flow-tab', 'automation/import-flow')
6969
})
7070
it('should have hasAction method', () => {
7171
expertAutomations.should.have.property('hasAction').which.is.a.Function()
@@ -310,7 +310,7 @@ describeMain('expertAutomations', () => {
310310
mockRED.view.importNodes.calledOnce.should.be.true()
311311
const args = mockRED.view.importNodes.firstCall.args
312312
args[0].should.deepEqual([{ id: '', type: 'tab', label: 'My New Flow', disabled: false, info: '', env: [] }])
313-
args[1].should.deepEqual({ generateIds: true, addFlow: false, notify: false })
313+
args[1].should.deepEqual({ generateIds: true, addFlow: false, notify: false, touchImport: true })
314314
result.should.have.property('success', true)
315315
result.should.have.property('handled', true)
316316
})
@@ -323,5 +323,20 @@ describeMain('expertAutomations', () => {
323323
result.should.have.property('success', true)
324324
})
325325
})
326+
describe('importFlow action', () => {
327+
it('should import flow JSON', async () => {
328+
mockRED.view.importNodes = sinon.stub()
329+
mockRED._ = sinon.stub().returns('error')
330+
const flowJson = JSON.stringify([{ id: 'n1', type: 'inject' }])
331+
const result = {}
332+
await expertAutomations.invokeAction('automation/import-flow', {
333+
params: { flow: flowJson }
334+
}, result)
335+
mockRED.view.importNodes.calledOnce.should.be.true()
336+
const args = mockRED.view.importNodes.firstCall.args
337+
args[1].should.have.property('touchImport', true)
338+
result.should.have.property('success', true)
339+
})
340+
})
326341
})
327342
})

test/unit/resources/expertComms.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,8 @@ describeMain('expertComms', function () {
951951
mockRED.view.importNodes.firstCall.args[1].should.eql({
952952
generateIds: true,
953953
addFlow: false,
954-
notify: true
954+
notify: true,
955+
touchImport: true
955956
})
956957

957958
eventSource.postMessage.calledOnce.should.be.true()

0 commit comments

Comments
 (0)