Skip to content

Commit dd5882d

Browse files
committed
Appname loading from api creator
1 parent ce0612e commit dd5882d

File tree

2 files changed

+132
-10
lines changed

2 files changed

+132
-10
lines changed

db-connector.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5204,6 +5204,20 @@ func FindWorkflowAppByName(ctx context.Context, appName string) ([]WorkflowApp,
52045204
var apps []WorkflowApp
52055205

52065206
nameKey := "workflowapp"
5207+
cacheKey := fmt.Sprintf("%s_appname_%s", appName)
5208+
if project.CacheDb {
5209+
cache, err := GetCache(ctx, cacheKey)
5210+
if err == nil {
5211+
cacheData := []byte(cache.([]uint8))
5212+
err = json.Unmarshal(cacheData, &apps)
5213+
if err == nil {
5214+
return apps, nil
5215+
}
5216+
} else {
5217+
//log.Printf("[DEBUG] Failed getting cache for user: %s", err)
5218+
}
5219+
}
5220+
52075221
if project.DbType == "opensearch" {
52085222
var buf bytes.Buffer
52095223
query := map[string]interface{}{
@@ -5272,14 +5286,27 @@ func FindWorkflowAppByName(ctx context.Context, appName string) ([]WorkflowApp,
52725286
}
52735287
} else {
52745288
//log.Printf("Looking for name %s in %s", appName, nameKey)
5275-
q := datastore.NewQuery(nameKey).Filter("name =", appName)
5289+
q := datastore.NewQuery(nameKey).Filter("Name =", appName).Limit(6)
52765290
_, err := project.Dbclient.GetAll(ctx, q, &apps)
52775291
if err != nil && len(apps) == 0 {
52785292
log.Printf("[WARNING] Failed getting apps for name: %s", appName)
52795293
return apps, err
52805294
}
52815295
}
52825296

5297+
if project.CacheDb {
5298+
data, err := json.Marshal(apps)
5299+
if err != nil {
5300+
log.Printf("[WARNING] Failed marshalling apps for appname %s: %s", appName, err)
5301+
return apps, nil
5302+
}
5303+
5304+
err = SetCache(ctx, cacheKey, data, 1440)
5305+
if err != nil {
5306+
log.Printf("[WARNING] Failed updating cache: %s", err)
5307+
}
5308+
}
5309+
52835310
log.Printf("[INFO] Found %d apps for name %s in db-connector", len(apps), appName)
52845311
return apps, nil
52855312
}
@@ -6273,6 +6300,7 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
62736300
}
62746301

62756302
if err != nil {
6303+
//log.Printf("[INFO] Failed fetching results: %v", err)
62766304
if strings.Contains(fmt.Sprintf("%s", err), "cannot load field") {
62776305
//log.Printf("[ERROR] Error in reference_org app load of %s (%s): %s.", innerApp.Name, innerApp.ID, err)
62786306
} else {

shared.go

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)