From 1704933ef4df0b2fbaa1396dcf77bacf7034ad56 Mon Sep 17 00:00:00 2001 From: shay23b Date: Sun, 19 Nov 2023 16:55:47 +0200 Subject: [PATCH 1/8] fix GetFunctionsDetails and GetGithubContentFromConnectedRepo --- server/memphis_handlers_functions_cloud.go | 73 +++++++++++++++---- server/source_code_management_github_cloud.go | 33 +++++++-- 2 files changed, 85 insertions(+), 21 deletions(-) diff --git a/server/memphis_handlers_functions_cloud.go b/server/memphis_handlers_functions_cloud.go index 08fcdc325..d864d3c58 100644 --- a/server/memphis_handlers_functions_cloud.go +++ b/server/memphis_handlers_functions_cloud.go @@ -185,19 +185,51 @@ func GetFunctionsDetails(functionsDetails map[string][]functionDetails) (map[str inputs = append(inputs, environmentVar) } } - description, ok := fucntionContentMap["description"].(string) - if !ok { - description = "" - } - runtime, ok := fucntionContentMap["runtime"].(string) - var language string + description := "" + descriptionInterface, ok := fucntionContentMap["description"] if ok { - regex := regexp.MustCompile(`[0-9]+|\\.$`) - language = regex.ReplaceAllString(runtime, "") - language = strings.TrimRight(language, ".") - if strings.Contains(language, "-edge") { - language = strings.Trim(language, ".-edge") + description = descriptionInterface.(string) + } + + functionName := "" + if functionNameInterface, ok := fucntionContentMap["function_name"]; !ok || functionNameInterface == nil || functionNameInterface.(string) == "" { + errMsg := fmt.Errorf("function in %s repository is invalid since its memphis.yaml file is missing the function_name field", repo) + return functions, errMsg + } else { + functionName = functionNameInterface.(string) + } + + runtime := "" + if runtimeInterface, ok := fucntionContentMap["runtime"]; !ok || runtimeInterface == nil || runtimeInterface.(string) == "" { + errMsg := fmt.Errorf("function %s placed in %s repository is invalid since its memphis.yaml file is missing the runtime field", repo, functionName) + return functions, errMsg + } else { + runtime = runtimeInterface.(string) + } + regex := regexp.MustCompile(`[0-9]+|\\.$`) + language := regex.ReplaceAllString(runtime, "") + language = strings.TrimRight(language, ".") + if strings.Contains(language, "-edge") { + language = strings.Trim(language, ".-edge") + } + + dependencies := "" + dependenciesMissing := false + if dependenciesInterface, ok := fucntionContentMap["dependencies"]; !ok || dependenciesInterface == nil || dependenciesInterface.(string) == "" { + dependenciesMissing = true + } else { + dependencies = dependenciesInterface.(string) + } + + if dependenciesMissing { + switch language { + case "go": + dependencies = "go.mod" + case "nodejs": + dependencies = "package.json" + case "python": + dependencies = "requirements.txt" } } @@ -214,16 +246,25 @@ func GetFunctionsDetails(functionsDetails map[string][]functionDetails) (map[str if commit != nil { lastCommit = commit.Commit.Committer.Date } + memory := 128 * 1024 * 1024 + if memoryInterface, ok := fucntionContentMap["memory"]; ok && memoryInterface != nil { + memory = memoryInterface.(int) + } + + storage := 512 * 1024 * 1024 + if storageInterface, ok := fucntionContentMap["storage"]; ok && storageInterface != nil { + storage = storageInterface.(int) + } functionDetails := models.FunctionResult{ - FunctionName: fucntionContentMap["function_name"].(string), + FunctionName: functionName, Description: description, Tags: tagsStrings, Runtime: runtime, - Dependencies: fucntionContentMap["dependencies"].(string), - Inputs: inputs, - Memory: int(fucntionContentMap["memory"].(int64)), - Storage: int(fucntionContentMap["storage"].(int64)), + Dependencies: dependencies, + Inputs: inputs, + Memory: memory, + Storage: storage, Handler: handler, Scm: "github", Repo: repo, diff --git a/server/source_code_management_github_cloud.go b/server/source_code_management_github_cloud.go index 836eef01d..34643504c 100644 --- a/server/source_code_management_github_cloud.go +++ b/server/source_code_management_github_cloud.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "fmt" + "regexp" "strings" "github.com/memphisdev/memphis/models" @@ -110,8 +111,23 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun contentMap["storage"] = int64(512) * 1024 * 1024 } - if contentMap["dependencies"].(string) == "" { - switch contentMap["language"] { + dependenciesMissing := false + if dependencies, ok := contentMap["dependencies"]; !ok || dependencies == nil || dependencies.(string) == "" { + dependenciesMissing = true + } + runtime := "" + if runtimeInterface, ok := contentMap["runtime"]; !ok || runtimeInterface == nil || runtimeInterface.(string) == "" { + continue + } else { + runtime = runtimeInterface.(string) + } + re := regexp.MustCompile("^[^0-9.]+") + lang := re.FindString(runtime) + if lang != "go" && lang != "python" && lang != "nodejs" { + continue + } + if dependenciesMissing { + switch lang { case "go": contentMap["dependencies"] = "go.mod" case "nodejs": @@ -158,8 +174,15 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun TenantName: tenantName, } - if path != contentMap["function_name"].(string) { - message := fmt.Sprintf("In the repository %s, function name %s in git doesn't match the function_name field %s in YAML file.", repo, splitPath[0], contentMap["function_name"].(string)) + functionName := "" + if functionNameInterface, ok := contentMap["function_name"]; !ok || functionNameInterface == nil || functionNameInterface.(string) == "" { + continue + } else { + functionName = functionNameInterface.(string) + } + + if path != functionName { + message := fmt.Sprintf("In the repository %s, function name %s in git doesn't match the function_name field %s in YAML file.", repo, splitPath[0], functionName) serv.Warnf("[tenant: %s]GetGithubContentFromConnectedRepo: %s", tenantName, message) fileDetails.IsValid = false fileDetails.InvalidReason = message @@ -167,7 +190,7 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun continue } if strings.Contains(path, " ") { - message := fmt.Sprintf("In the repository %s, the function name %s in the YAML file cannot contain spaces", repo, contentMap["function_name"].(string)) + message := fmt.Sprintf("In the repository %s, the function name %s in the YAML file cannot contain spaces", repo, functionName) serv.Warnf("[tenant: %s]GetGithubContentFromConnectedRepo: %s", tenantName, message) fileDetails.IsValid = false fileDetails.InvalidReason = message From 4033145daaeb13a45228fbb3b991b6090fa00c39 Mon Sep 17 00:00:00 2001 From: shay23b Date: Sun, 19 Nov 2023 20:13:27 +0200 Subject: [PATCH 2/8] int -> int64 --- server/memphis_handlers_functions_cloud.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/memphis_handlers_functions_cloud.go b/server/memphis_handlers_functions_cloud.go index d864d3c58..a7b057bb1 100644 --- a/server/memphis_handlers_functions_cloud.go +++ b/server/memphis_handlers_functions_cloud.go @@ -248,12 +248,12 @@ func GetFunctionsDetails(functionsDetails map[string][]functionDetails) (map[str } memory := 128 * 1024 * 1024 if memoryInterface, ok := fucntionContentMap["memory"]; ok && memoryInterface != nil { - memory = memoryInterface.(int) + memory = int(memoryInterface.(int64)) } storage := 512 * 1024 * 1024 if storageInterface, ok := fucntionContentMap["storage"]; ok && storageInterface != nil { - storage = storageInterface.(int) + storage = int(storageInterface.(int64)) } functionDetails := models.FunctionResult{ From 18de0ca5237426c1c31d200b19ce50510a4c59c2 Mon Sep 17 00:00:00 2001 From: Yaniv Ben Hemo <70286779+yanivbh1@users.noreply.github.com> Date: Sun, 19 Nov 2023 18:07:06 +0200 Subject: [PATCH 3/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c7da9fa7..3664aed95 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,7 @@ Schema deserialization | :white_check_mark: | :white_check_mark: | :white_check_ ## Support 🙋‍♂️🤝 -### Ask a question ❓ about Memphis.dev. or something related to us: +### Have any questions ❓ We welcome you to our discord server with your questions, doubts and feedback. From d0abc4f73bb367c5888fa9871010fe97d7d841f3 Mon Sep 17 00:00:00 2001 From: Yaniv Ben Hemo <70286779+yanivbh1@users.noreply.github.com> Date: Sun, 19 Nov 2023 18:27:11 +0200 Subject: [PATCH 4/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3664aed95..a3fbdd326 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@
- CNCF Silver Member - CNCF Silver Member + CNCF Silver Member + CNCF Silver Member
From 4e71e6e5e5c08f2e4bd322672b59ba5e60ca34d1 Mon Sep 17 00:00:00 2001 From: Yaniv Ben Hemo <70286779+yanivbh1@users.noreply.github.com> Date: Sun, 19 Nov 2023 18:30:40 +0200 Subject: [PATCH 5/8] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a3fbdd326..ddc8cddae 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@
- CNCF Silver Member CNCF Silver Member
From e86ee50866d87d689cc9be281e18904eb2270d95 Mon Sep 17 00:00:00 2001 From: shohamroditimemphis Date: Sun, 19 Nov 2023 17:03:48 +0200 Subject: [PATCH 6/8] RND-191-support-function-inputs --- models/functions.go | 108 ++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/models/functions.go b/models/functions.go index 227d9667a..4238b4115 100644 --- a/models/functions.go +++ b/models/functions.go @@ -14,63 +14,63 @@ package models import "time" type Function struct { - ID int `json:"id"` - FunctionName string `json:"function_name"` - Description string `json:"description"` - Tags []string `json:"tags"` - Runtime string `json:"runtime"` - Dependencies string `json:"dependencies"` - Inputs []map[string]interface{} `json:"inputs"` - Memory int `json:"memory"` - Storage int `json:"storage"` - Handler string `json:"handler"` - TenantName string `json:"tenant_name"` - Scm string `json:"scm"` - Owner string `json:"owner"` - Repo string `json:"repo"` - Branch string `json:"branch"` - UpdatedAt time.Time `json:"installed_updated_at"` - Version int `json:"installed_version"` - InProgress bool `json:"installed_in_progress"` - ComputeEngine string `json:"compute_engine"` - Installed bool `json:"installed"` - IsValid bool `json:"is_valid"` - InvalidReason string `json:"invalid_reason"` - UpdatesAvailable bool `json:"updates_available"` - ByMemphis bool `json:"by_memphis"` + ID int `json:"id"` + FunctionName string `json:"function_name"` + Description string `json:"description"` + Tags []string `json:"tags"` + Runtime string `json:"runtime"` + Dependencies string `json:"dependencies"` + Inputs []map[string]string `json:"inputs"` + Memory int `json:"memory"` + Storage int `json:"storage"` + Handler string `json:"handler"` + TenantName string `json:"tenant_name"` + Scm string `json:"scm"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Branch string `json:"branch"` + UpdatedAt time.Time `json:"installed_updated_at"` + Version int `json:"installed_version"` + InProgress bool `json:"installed_in_progress"` + ComputeEngine string `json:"compute_engine"` + Installed bool `json:"installed"` + IsValid bool `json:"is_valid"` + InvalidReason string `json:"invalid_reason"` + UpdatesAvailable bool `json:"updates_available"` + ByMemphis bool `json:"by_memphis"` } type FunctionResult struct { - ID int `json:"id"` - FunctionName string `json:"function_name"` - Description string `json:"description"` - Tags []string `json:"tags"` - Runtime string `json:"runtime"` - Dependencies string `json:"dependencies"` - Inputs []map[string]interface{} `json:"inputs"` - Memory int `json:"memory"` - Storage int `json:"storage"` - Handler string `json:"handler"` - TenantName string `json:"tenant_name"` - Scm string `json:"scm"` - Owner string `json:"owner"` - Repo string `json:"repo"` - Branch string `json:"branch"` - UpdatedAt time.Time `json:"installed_updated_at"` - Version int `json:"installed_version"` - InProgress bool `json:"installed_in_progress"` - ComputeEngine string `json:"compute_engine"` - Installed bool `json:"installed"` - IsValid bool `json:"is_valid"` - InvalidReason string `json:"invalid_reason"` - UpdatesAvailable bool `json:"updates_available"` - ByMemphis bool `json:"by_memphis"` - Language string `json:"language"` - Link *string `json:"link,omitempty"` - LastCommit *time.Time `json:"last_commit,omitempty"` - ClonedUpdatesAvailable bool `json:"cloned_updates_available"` - ClonedUpdatesIsValid bool `json:"cloned_updates_is_valid"` - ClonedUpdatesInvalidReason string `json:"cloned_updates_invalid_reason"` + ID int `json:"id"` + FunctionName string `json:"function_name"` + Description string `json:"description"` + Tags []string `json:"tags"` + Runtime string `json:"runtime"` + Dependencies string `json:"dependencies"` + Inputs []map[string]string `json:"inputs"` + Memory int `json:"memory"` + Storage int `json:"storage"` + Handler string `json:"handler"` + TenantName string `json:"tenant_name"` + Scm string `json:"scm"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Branch string `json:"branch"` + UpdatedAt time.Time `json:"installed_updated_at"` + Version int `json:"installed_version"` + InProgress bool `json:"installed_in_progress"` + ComputeEngine string `json:"compute_engine"` + Installed bool `json:"installed"` + IsValid bool `json:"is_valid"` + InvalidReason string `json:"invalid_reason"` + UpdatesAvailable bool `json:"updates_available"` + ByMemphis bool `json:"by_memphis"` + Language string `json:"language"` + Link *string `json:"link,omitempty"` + LastCommit *time.Time `json:"last_commit,omitempty"` + ClonedUpdatesAvailable bool `json:"cloned_updates_available"` + ClonedUpdatesIsValid bool `json:"cloned_updates_is_valid"` + ClonedUpdatesInvalidReason string `json:"cloned_updates_invalid_reason"` } type FunctionsRes struct { InstalledFunctions []FunctionResult `json:"installed_functions"` From 3a77cc95f5804b46def024bcf59930fe9482c35f Mon Sep 17 00:00:00 2001 From: shohamroditimemphis Date: Sun, 19 Nov 2023 18:46:10 +0200 Subject: [PATCH 7/8] fixes --- server/memphis_handlers_functions_cloud.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/memphis_handlers_functions_cloud.go b/server/memphis_handlers_functions_cloud.go index a7b057bb1..046ad1d30 100644 --- a/server/memphis_handlers_functions_cloud.go +++ b/server/memphis_handlers_functions_cloud.go @@ -169,12 +169,12 @@ func GetFunctionsDetails(functionsDetails map[string][]functionDetails) (map[str } } - inputs := []map[string]interface{}{} + inputs := []map[string]string{} inputsInterfaceSlice, ok := fucntionContentMap["inputs"].([]interface{}) if ok { for _, environmentVarInterface := range inputsInterfaceSlice { environmentVarMap, _ := environmentVarInterface.(map[interface{}]interface{}) - environmentVar := make(map[string]interface{}) + environmentVar := make(map[string]string) for k, v := range environmentVarMap { if key, ok := k.(string); ok { if val, ok := v.(string); ok { From 102da7ab3c8052ab7382c30554b37ee5723dfce0 Mon Sep 17 00:00:00 2001 From: shohamroditimemphis Date: Sun, 19 Nov 2023 19:44:36 +0200 Subject: [PATCH 8/8] fixes --- server/source_code_management_github_cloud.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/source_code_management_github_cloud.go b/server/source_code_management_github_cloud.go index 34643504c..b0058d273 100644 --- a/server/source_code_management_github_cloud.go +++ b/server/source_code_management_github_cloud.go @@ -104,11 +104,11 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun } if _, ok := contentMap["memory"]; !ok || contentMap["memory"] == "" { - contentMap["memory"] = int64(128) * 1024 * 1024 + contentMap["memory"] = 128 * 1024 * 1024 } if _, ok := contentMap["storage"]; !ok || contentMap["storage"] == "" { - contentMap["storage"] = int64(512) * 1024 * 1024 + contentMap["storage"] = 512 * 1024 * 1024 } dependenciesMissing := false