@@ -6133,6 +6133,150 @@ func diffWorkflowWrapper(parentWorkflow Workflow) Workflow {
61336133 return parentWorkflow
61346134}
61356135
6136+ func subflowPropagationWrapper(parentWorkflow Workflow, childWorkflow Workflow, parentTrigger Trigger) Trigger {
6137+ // remember: when this function is used, the parent trigger is passed to
6138+ // create the new child trigger.
6139+ trigger := parentTrigger
6140+
6141+ for paramIndex, param := range trigger.Parameters {
6142+ // since this is an added subflow, the workflow being referred
6143+ // is most likely not already distributed. let's do that.
6144+ if param.Name == "workflow" {
6145+ parentSubflowPointedId := param.Value
6146+
6147+ if len(parentSubflowPointedId) == 0 {
6148+ continue
6149+ }
6150+
6151+ ctx := context.Background()
6152+
6153+ // propagate reference workflow to child org
6154+ // check first if the workflow has been propagated before
6155+ // to the suborg (ParentWorkflowId is this workflow's ID)
6156+ childOrg, err := GetOrg(ctx, childWorkflow.OrgId)
6157+ if err != nil {
6158+ log.Printf("[WARNING] Failed getting org: %s", err)
6159+ continue
6160+ }
6161+
6162+ user := User{
6163+ Role: "admin",
6164+ ActiveOrg: OrgMini{
6165+ Id: childOrg.Id,
6166+ },
6167+ }
6168+
6169+ childOrgWorkflows, err := GetAllWorkflowsByQuery(ctx, user, 250, "")
6170+ if err != nil {
6171+ log.Printf("[WARNING] Failed getting org workflows: %s", err)
6172+ continue
6173+ }
6174+
6175+ propagatedEarlier := false
6176+ alreadyPropagatedSubflow := ""
6177+
6178+ for _, workflow := range childOrgWorkflows {
6179+ // this means that the subflow has been propagated to
6180+ // child workflow already. no need to complicate things further.
6181+ if workflow.ParentWorkflowId == parentSubflowPointedId {
6182+ propagatedEarlier = true
6183+ alreadyPropagatedSubflow = workflow.ID
6184+ break
6185+ }
6186+ }
6187+
6188+ if propagatedEarlier {
6189+ // just make sure that it now points to that workflow
6190+ trigger.Parameters[paramIndex].Value = alreadyPropagatedSubflow
6191+
6192+ // get workflow
6193+ workflow, err := GetWorkflow(ctx, alreadyPropagatedSubflow)
6194+ if err != nil {
6195+ log.Printf("[WARNING] Failed getting propagated subflow: %s", err)
6196+ continue
6197+ }
6198+
6199+ startNodeIndexToOverwrite := -1
6200+ currentStartNode := ""
6201+
6202+ // taking the right startnode is important
6203+ for startNodeIndex, startNode := range trigger.Parameters {
6204+ if startNode.Name == "startnode" {
6205+ startNodeIndexToOverwrite = startNodeIndex
6206+ currentStartNode = startNode.Value
6207+ }
6208+ }
6209+
6210+ if len(currentStartNode) == 0 {
6211+ continue
6212+ }
6213+
6214+ for _, action := range workflow.Actions {
6215+ if action.ID == currentStartNode {
6216+ trigger.Parameters[startNodeIndexToOverwrite].Value = action.ID
6217+ break
6218+ }
6219+ }
6220+
6221+ continue
6222+ }
6223+
6224+ parentSubflowPointed, err := GetWorkflow(ctx, parentSubflowPointedId)
6225+ if err != nil {
6226+ log.Printf("[WARNING] Failed getting parent subflow: %s", err)
6227+ continue
6228+ }
6229+
6230+ parentSubflowPointed.SuborgDistribution = append(parentSubflowPointed.SuborgDistribution, childWorkflow.OrgId)
6231+
6232+ err = SetWorkflow(ctx, *parentSubflowPointed, parentSubflowPointedId)
6233+ if err != nil {
6234+ log.Printf("[WARNING] Failed setting parent subflow: %s", err)
6235+ continue
6236+ }
6237+
6238+ propagatedSubflow, err := GenerateWorkflowFromParent(ctx, *parentSubflowPointed, parentSubflowPointed.OrgId, childWorkflow.OrgId)
6239+ if err != nil {
6240+ log.Printf("[WARNING] Failed to generate child workflow %s (%s) for %s (%s): %s", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID, err)
6241+ } else {
6242+ log.Printf("[INFO] Generated child workflow %s (%s) for %s (%s)", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID)
6243+
6244+ trigger.Parameters[paramIndex].Value = propagatedSubflow.ID
6245+ }
6246+
6247+ startnode := ""
6248+ startNodeParamIndex := -1
6249+
6250+ // now handle startnode
6251+ for startNodeParamIndex_, param_ := range trigger.Parameters {
6252+ if param_.Name == "startnode" {
6253+ startnode = param_.Value
6254+ startNodeParamIndex = startNodeParamIndex_
6255+ }
6256+ }
6257+
6258+ if len(startnode) == 0 {
6259+ continue
6260+ }
6261+
6262+ // actions are always startnodes
6263+ // find the equivalent of the startnode in the new workflow
6264+ for _, action := range propagatedSubflow.Actions {
6265+ if action.ID == startnode {
6266+ trigger.Parameters[startNodeParamIndex].Value = action.ID
6267+ break
6268+ }
6269+ }
6270+
6271+ } else if param.Name != "startnode" && param.Name != "startnode" {
6272+ // just use it
6273+ trigger.Parameters[paramIndex].Value = param.Value
6274+ }
6275+ }
6276+
6277+ return trigger
6278+ }
6279+
61366280func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
61376281 // Check if there is a difference in actions, and what they are
61386282 // Check if there is a difference in triggers, and what they are
@@ -6626,119 +6770,9 @@ func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
66266770 }
66276771 }
66286772 } 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- ctx := context.Background()
6643-
6644- // propagate reference workflow to child org
6645- // check first if the workflow has been propagated before
6646- // to the suborg (ParentWorkflowId is this workflow's ID)
6647- childOrg, err := GetOrg(ctx, childWorkflow.OrgId)
6648- if err != nil {
6649- log.Printf("[WARNING] Failed getting org: %s", err)
6650- continue
6651- }
6652-
6653- user := User{
6654- Role: "admin",
6655- ActiveOrg: OrgMini{
6656- Id: childOrg.Id,
6657- },
6658- }
6659-
6660- childOrgWorkflows, err := GetAllWorkflowsByQuery(ctx, user, 250, "")
6661- if err != nil {
6662- log.Printf("[WARNING] Failed getting org workflows: %s", err)
6663- continue
6664- }
6665-
6666- propagatedEarlier := false
6667- alreadyPropagatedSubflow := ""
6668-
6669- for _, workflow := range childOrgWorkflows {
6670- if workflow.ParentWorkflowId == parentSubflowPointedId {
6671- propagatedEarlier = true
6672- alreadyPropagatedSubflow = workflow.ID
6673- break
6674- }
6675- }
6676-
6677- if propagatedEarlier {
6678- // just make sure that it now points to that workflow
6679- trigger.Parameters[paramIndex].Value = alreadyPropagatedSubflow
6680- continue
6681- }
6682-
6683- parentSubflowPointed, err := GetWorkflow(ctx, parentSubflowPointedId)
6684- if err != nil {
6685- log.Printf("[WARNING] Failed getting parent subflow: %s", err)
6686- continue
6687- }
6688-
6689- parentSubflowPointed.SuborgDistribution = append(parentSubflowPointed.SuborgDistribution, childWorkflow.OrgId)
6690-
6691- err = SetWorkflow(ctx, *parentSubflowPointed, parentSubflowPointedId)
6692- if err != nil {
6693- log.Printf("[WARNING] Failed setting parent subflow: %s", err)
6694- continue
6695- }
6696-
6697- propagatedSubflow, err := GenerateWorkflowFromParent(ctx, *parentSubflowPointed, parentSubflowPointed.OrgId, childWorkflow.OrgId)
6698- if err != nil {
6699- log.Printf("[WARNING] Failed to generate child workflow %s (%s) for %s (%s): %s", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID, err)
6700- } else {
6701- log.Printf("[INFO] Generated child workflow %s (%s) for %s (%s)", childWorkflow.Name, childWorkflow.ID, parentWorkflow.Name, parentWorkflow.ID)
6702-
6703- trigger.Parameters[paramIndex].Value = propagatedSubflow.ID
6704- }
6705-
6706- startnode := ""
6707- startNodeParamIndex := -1
6708-
6709- // now handle startnode
6710- for startNodeParamIndex_, param_ := range trigger.Parameters {
6711- if param_.Name == "startnode" {
6712- startnode = param_.Value
6713- startNodeParamIndex = startNodeParamIndex_
6714- }
6715- }
6716-
6717- if len(startnode) == 0 {
6718- continue
6719- }
6720-
6721- // find the equivalent of the startnode in the new workflow
6722- for _, action := range propagatedSubflow.Actions {
6723- if action.ID == startnode {
6724- trigger.Parameters[startNodeParamIndex].Value = action.ID
6725- break
6726- }
6727- }
6728-
6729- // sometimes, it can happen that it's a replaced trigger
6730- for _, trigger := range propagatedSubflow.Triggers {
6731- if trigger.ReplacementForTrigger == startnode {
6732- trigger.Parameters[startNodeParamIndex].Value = trigger.ID
6733- break
6734- }
6735- }
6736-
6737- } else if param.Name != "startnode" && param.Name != "startnode" {
6738- // just use it
6739- trigger.Parameters[paramIndex].Value = param.Value
6740- }
6741- }
6773+ // params: workflow, argument, user_apikey, startnode,
6774+ // check_result and auth_override
6775+ trigger = subflowPropagationWrapper(parentWorkflow, childWorkflow, trigger)
67426776 }
67436777
67446778 for branchIndex, branch := range childWorkflow.Branches {
@@ -6795,16 +6829,14 @@ func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
67956829 childWorkflow.Triggers[index].Label = action.Label
67966830 childWorkflow.Triggers[index].Position = action.Position
67976831 childWorkflow.Triggers[index].AppVersion = action.AppVersion
6798- childWorkflow.Triggers[index].IsStartNode = action.IsStartNode
67996832 break
68006833 } else if action.TriggerType == "SCHEDULE" {
68016834 // make sure to override: name, label, position,
6802- // app_version, startnode and parameters
6835+ // app_version and parameters
68036836 childWorkflow.Triggers[index].Name = action.Name
68046837 childWorkflow.Triggers[index].Label = action.Label
68056838 childWorkflow.Triggers[index].Position = action.Position
68066839 childWorkflow.Triggers[index].AppVersion = action.AppVersion
6807- childWorkflow.Triggers[index].IsStartNode = action.IsStartNode
68086840 // i don't want schedules to start or stop according to the parent workflow.
68096841 // thus, doing what i did here.
68106842 for paramIndex, param := range action.Parameters {
@@ -6818,6 +6850,21 @@ func diffWorkflows(oldWorkflow Workflow, parentWorkflow Workflow, update bool) {
68186850 }
68196851
68206852 break
6853+ } else if action.TriggerType == "SUBFLOW" {
6854+ // make sure to override: name, label, position,
6855+ // app_version, startnode and parameters
6856+ childWorkflow.Triggers[index].Name = action.Name
6857+ childWorkflow.Triggers[index].Label = action.Label
6858+ childWorkflow.Triggers[index].Position = action.Position
6859+ childWorkflow.Triggers[index].AppVersion = action.AppVersion
6860+
6861+ // essentially, now we try to verify:
6862+ // okay, new workflow? we see it's a subflow that's
6863+ // what changed? is it the workflow?
6864+
6865+ action = subflowPropagationWrapper(parentWorkflow, childWorkflow, action)
6866+ childWorkflow.Triggers[index].Parameters = action.Parameters
6867+ break
68216868 }
68226869
68236870 childWorkflow.Triggers[index] = action
@@ -9946,35 +9993,6 @@ func GenerateWorkflowFromParent(ctx context.Context, workflow Workflow, parentOr
99469993 newWf.Branches[branchIndex].DestinationID = newWf.Triggers[triggerIndex].ID
99479994 }
99489995 }
9949- } else if newWf.Triggers[triggerIndex].TriggerType == "SUBFLOW" {
9950- oldID := newWf.Triggers[triggerIndex].ID
9951- newWf.Triggers[triggerIndex].ID = uuid.NewV4().String()
9952-
9953- newWf.Triggers[triggerIndex].ReplacementForTrigger = oldID
9954-
9955- // loop through workflow trigger parameters
9956- for paramIndex, param := range newWf.Triggers[triggerIndex].Parameters {
9957- if param.Name == "workflow" {
9958- if len(param.Value) == 0 {
9959- break
9960- }
9961-
9962- subflow, err := GetWorkflow(ctx, param.Value)
9963- if err != nil {
9964- log.Printf("[ERROR] Failed getting subflow %s: %s", param.Value, err)
9965- break
9966- }
9967-
9968- // distribute the subflow further to this suborg, and then take the new id
9969- childWorkflow, err := GenerateWorkflowFromParent(ctx, *subflow, parentOrgId, subOrgId)
9970- if err != nil {
9971- log.Printf("[ERROR] Failed generating subflow %s: %s", subflow.ID, err)
9972- break
9973- }
9974-
9975- newWf.Triggers[triggerIndex].Parameters[paramIndex].Value = childWorkflow.ID
9976- }
9977- }
99789996 }
99799997 }
99809998
0 commit comments