fix(plugin-workflow): avoid revision with ghost nodes (#941)

This commit is contained in:
Junyi 2022-10-20 15:45:00 +08:00 committed by chenos
parent cadaa8a2c2
commit c7300d1100
2 changed files with 55 additions and 6 deletions

View File

@ -285,5 +285,43 @@ describe('workflow > Plugin', () => {
const [echo, calculation] = await execution.getJobs({ order: [['id', 'ASC']] }); const [echo, calculation] = await execution.getJobs({ order: [['id', 'ASC']] });
expect(calculation.result).toBe(2); expect(calculation.result).toBe(2);
}); });
it('revision with using of deleted nodes', async () => {
const w1 = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts'
}
});
const n2 = await w1.createNode({
type: 'calculation',
config: {
calculation: {
calculator: 'add',
operands: [
{
type: '$jobsMapByNodeId',
options: {
nodeId: 0,
path: 'data.read'
}
},
{
value: `{{$jobsMapByNodeId.0.data.read}}`
}
]
}
},
});
const { status } = await agent.resource(`workflows`).revision({
filterByTk: w1.id
});
expect(status).toBe(400);
});
}); });
}); });

View File

@ -31,11 +31,16 @@ function migrateConfig(config, oldToNew) {
case 'array': case 'array':
return value.map(item => migrate(item)); return value.map(item => migrate(item));
case 'string': case 'string':
return value const matcher = value.match(/(\{\{\$jobsMapByNodeId\.)([\w-]+)/);
.replace( if (!matcher) {
/(\{\{\$jobsMapByNodeId\.)([\w-]+)/, return value;
(_, prefix, id) => `${prefix}${oldToNew.get(Number.parseInt(id, 10)).id}` }
); const oldNodeId = Number.parseInt(matcher[2], 10);
const newNode = oldToNew.get(oldNodeId);
if (!newNode) {
throw new Error('node configurated for result is not existed');
}
return value.replace(matcher[0], `{{$jobsMapByNodeId.${newNode.id}`);
default: default:
return value; return value;
} }
@ -91,11 +96,17 @@ export async function revision(context: Context, next) {
const oldNode = originalNodesMap.get(oldId); const oldNode = originalNodesMap.get(oldId);
const newUpstream = oldNode.upstreamId ? oldToNew.get(oldNode.upstreamId) : null; const newUpstream = oldNode.upstreamId ? oldToNew.get(oldNode.upstreamId) : null;
const newDownstream = oldNode.downstreamId ? oldToNew.get(oldNode.downstreamId) : null; const newDownstream = oldNode.downstreamId ? oldToNew.get(oldNode.downstreamId) : null;
let migratedConfig;
try {
migratedConfig = migrateConfig(oldNode.config, oldToNew);
} catch (err) {
return context.throw(400, err.message);
}
await newNode.update({ await newNode.update({
upstreamId: newUpstream?.id ?? null, upstreamId: newUpstream?.id ?? null,
downstreamId: newDownstream?.id ?? null, downstreamId: newDownstream?.id ?? null,
config: migrateConfig(oldNode.config, oldToNew) config: migratedConfig
}, { transaction }); }, { transaction });
} }