Skip to content

Commit f4ee2c7

Browse files
committed
fix: first draft for subflow distribution
1 parent 572b51e commit f4ee2c7

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

shared.go

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6140,7 +6140,6 @@ func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
61406140

61416141
// We create a new ID for each trigger.
61426142
// Older ID is stored in trigger.ReplacementForTrigger
6143-
61446143
nameChanged := false
61456144
descriptionChanged := false
61466145
tagsChanged := false
@@ -6626,6 +6625,106 @@ func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
66266625
trigger.Parameters[paramIndex].Value = ""
66276626
}
66286627
}
6628+
} else if trigger.TriggerType == "SUBFLOW" {
6629+
// params: workflow, argument, user_apikey, startnode,
6630+
// check_result and auth_override
6631+
6632+
for paramIndex, param := range trigger.Parameters {
6633+
// since this is an added subflow, the workflow being referred
6634+
// is most likely not already distributed. let's do that.
6635+
if param.Name == "workflow" {
6636+
parentSubflowPointedId := param.Value
6637+
6638+
if len(parentSubflowPointedId) == 0 {
6639+
continue
6640+
}
6641+
6642+
// propagate reference workflow to child org
6643+
// check first if the workflow has been propagated before
6644+
// to the suborg (ParentWorkflowId is this workflow's ID)
6645+
childOrg, err := GetOrg(ctx, childWorkflow.OrgId)
6646+
if err != nil {
6647+
log.Printf("[WARNING] Failed getting org: %s", err)
6648+
continue
6649+
}
6650+
6651+
childOrgWorkflows, err := GetAllWorkflowsByQuery(ctx, childOrg, 0, "")
6652+
if err != nil {
6653+
log.Printf("[WARNING] Failed getting org workflows: %s", err)
6654+
continue
6655+
}
6656+
6657+
propagatedEarlier := false
6658+
for _, workflow := range childOrgWorkflows {
6659+
if workflow.ParentWorkflowId == parentSubflowPointedId {
6660+
propagatedEarlier = true
6661+
break
6662+
}
6663+
}
6664+
6665+
if propagatedEarlier {
6666+
continue
6667+
}
6668+
6669+
parentSubflowPointed, err := GetWorkflow(ctx, parentSubflowPointedId)
6670+
if err != nil {
6671+
log.Printf("[WARNING] Failed getting parent subflow: %s", err)
6672+
continue
6673+
}
6674+
6675+
parentSubflowPointed.SuborgDistribution = append(parentSubflowPointed.SuborgDistribution, childWorkflow.OrgId)
6676+
6677+
parentSubflowPointed, err = SetWorkflow(ctx, parentSubflowPointed, parentSubflowPointedId)
6678+
if err != nil {
6679+
log.Printf("[WARNING] Failed setting parent subflow: %s", err)
6680+
continue
6681+
}
6682+
6683+
propagatedSubflow, err := GenerateWorkflowFromParent(ctx, parentSubflowPointed, parentSubflowPointed.OrgId, suborgId)
6684+
if err != nil {
6685+
log.Printf("[WARNING] Failed to generate child workflow %s (%s) for %s (%s): %s", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID, err)
6686+
} else {
6687+
log.Printf("[INFO] Generated child workflow %s (%s) for %s (%s)", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID)
6688+
6689+
triggers[index].Parameters[paramIndex].Value = propagatedSubflow.ID
6690+
}
6691+
6692+
startnode := ""
6693+
startNodeParamIndex := -1
6694+
6695+
// now handle startnode
6696+
for startNodeParamIndex_, param_ := range trigger.Parameters {
6697+
if param_.Name == "startnode" {
6698+
startnode = param_.Value
6699+
startNodeParamIndex = startNodeParamIndex_
6700+
}
6701+
}
6702+
6703+
if len(startnode) == 0 {
6704+
continue
6705+
}
6706+
6707+
// find the equivalent of the startnode in the new workflow
6708+
for _, action := range propagatedSubflow.Actions {
6709+
if action.ID == startnode {
6710+
triggers[index].Parameters[startNodeParamIndex].Value = action.ID
6711+
break
6712+
}
6713+
}
6714+
6715+
// sometimes, it can happen that it's a replaced trigger
6716+
for _, trigger := range propagatedSubflow.Triggers {
6717+
if trigger.ReplacementForTrigger == startnode {
6718+
triggers[index].Parameters[startNodeParamIndex].Value = trigger.ID
6719+
break
6720+
}
6721+
}
6722+
6723+
} else if param.Name != "startnode" && param.Name != "startnode" {
6724+
// just use it
6725+
triggers[index].Parameters[paramIndex].Value = param.Value
6726+
}
6727+
}
66296728
}
66306729

66316730
for branchIndex, branch := range childWorkflow.Branches {

0 commit comments

Comments
 (0)