@@ -17555,7 +17555,7 @@ func CheckHookAuth(request *http.Request, auth string) error {
1755517555}
1755617556
1755717557// Body = The action body received from the user to test.
17558- func PrepareSingleAction(ctx context.Context, user User, fileId string, body []byte, runValidationAction bool) (WorkflowExecution, error) {
17558+ func PrepareSingleAction(ctx context.Context, user User, appId string, body []byte, runValidationAction bool) (WorkflowExecution, error) {
1755917559 workflowExecution := WorkflowExecution{}
1756017560
1756117561 var action Action
@@ -17565,19 +17565,113 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b
1756517565 return workflowExecution, err
1756617566 }
1756717567
17568- if fileId != action.AppID {
17569- log.Printf("[WARNING] Bad appid in single execution of App %s", fileId )
17568+ if appId != action.AppID {
17569+ log.Printf("[WARNING] Bad appid in single execution of App %s", appId )
1757017570 return workflowExecution, err
1757117571 }
1757217572
1757317573 if len(action.ID) == 0 {
1757417574 action.ID = uuid.NewV4().String()
1757517575 }
1757617576
17577- app, err := GetApp(ctx, fileId, user, false)
17578- if err != nil {
17579- log.Printf("[WARNING] Error getting app (execute SINGLE app action): %s", fileId)
17580- return workflowExecution, err
17577+ app := WorkflowApp{}
17578+ if strings.ToLower(appId) == "http" {
17579+ // Find the app and the ID for it
17580+ apps, err := FindWorkflowAppByName(ctx, "http")
17581+ if err != nil {
17582+ log.Printf("[WARNING] Failed to find HTTP app in single action execution: %s", err)
17583+ return workflowExecution, err
17584+ } else {
17585+ if len(apps) > 0 {
17586+ // Just assuming we can use #1
17587+
17588+ // Find the highest version
17589+ app = apps[0]
17590+ latestVersion := ""
17591+ for _, innerApp := range apps {
17592+ // Semver check
17593+ if len(innerApp.AppVersion) == 0 {
17594+ continue
17595+ }
17596+
17597+ if len(latestVersion) == 0 {
17598+ latestVersion = innerApp.AppVersion
17599+ app = innerApp
17600+ continue
17601+ }
17602+
17603+ v2, err := semver.NewVersion(innerApp.AppVersion)
17604+ if err != nil {
17605+ log.Printf("[ERROR] Failed parsing original app version %s: %s", innerApp.AppVersion, err)
17606+ continue
17607+ }
17608+
17609+ appConstraint := fmt.Sprintf("> %s", latestVersion)
17610+ c, err := semver.NewConstraint(appConstraint)
17611+ if err != nil {
17612+ log.Printf("[ERROR] Failed preparing constraint %s: %s", appConstraint, err)
17613+ continue
17614+ }
17615+
17616+ if c.Check(v2) {
17617+ app = innerApp
17618+ latestVersion = innerApp.AppVersion
17619+ }
17620+ }
17621+
17622+ appId = app.ID
17623+ } else {
17624+ log.Printf("[WARNING] Failed to find HTTP app in single action execution")
17625+ return workflowExecution, errors.New("Failed to find HTTP app. Is it installed?")
17626+ }
17627+ }
17628+
17629+ // Check if incoming action is "custom_action" and map it to HTTP
17630+ if action.Name == "custom_action" || action.Name == "Custom Action" {
17631+ urlIndex := -1
17632+ path := ""
17633+ queries := ""
17634+ for paramIndex, param := range action.Parameters {
17635+ if strings.ToLower(param.Name) == "method" {
17636+ action.Name = strings.ToUpper(param.Value)
17637+ } else if strings.ToLower(param.Name) == "url" {
17638+ urlIndex = paramIndex
17639+ } else if strings.ToLower(param.Name) == "path" {
17640+ path = param.Value
17641+ } else if strings.ToLower(param.Name) == "queries" {
17642+ queries = param.Value
17643+ }
17644+ }
17645+
17646+ if len(path) > 0 && urlIndex >= 0 {
17647+ if strings.HasPrefix(path, "/") {
17648+ path = path[1:]
17649+ }
17650+
17651+ action.Parameters[urlIndex].Value = fmt.Sprintf("%s/%s", action.Parameters[urlIndex].Value, path)
17652+ }
17653+
17654+ if len(queries) > 0 && urlIndex >= 0 {
17655+ // Split them and add to the URL
17656+ if strings.Contains(action.Parameters[urlIndex].Value, "?") {
17657+ action.Parameters[urlIndex].Value = fmt.Sprintf("%s&%s", action.Parameters[urlIndex].Value, queries)
17658+ } else {
17659+ action.Parameters[urlIndex].Value = fmt.Sprintf("%s?%s", action.Parameters[urlIndex].Value, queries)
17660+ }
17661+ }
17662+
17663+ log.Printf("URL: %#v", action.Parameters[urlIndex].Value)
17664+
17665+ }
17666+
17667+ } else {
17668+ newApp, err := GetApp(ctx, appId, user, false)
17669+ if err != nil || len(newApp.ID) == 0 {
17670+ log.Printf("[WARNING] Error getting app (execute SINGLE app action): %s", appId)
17671+ return workflowExecution, err
17672+ }
17673+
17674+ app = *newApp
1758117675 }
1758217676
1758317677 // FIXME: We need to inject missing empty auth here in some cases
@@ -17629,7 +17723,7 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b
1762917723 } else {
1763017724 //latestTimestamp := int64(0)
1763117725 for _, auth := range auths {
17632- if auth.App.ID != fileId {
17726+ if auth.App.ID != appId {
1763317727 continue
1763417728 }
1763517729
@@ -17710,7 +17804,7 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b
1771017804 }
1771117805 }
1771217806
17713- action.AppID = fileId
17807+ action.AppID = appId
1771417808 workflow := Workflow{
1771517809 Actions: []Action{
1771617810 action,
0 commit comments