Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get GitHub content from connected repo #1423

Merged
merged 9 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions server/memphis_handlers_functions_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand All @@ -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 = int(memoryInterface.(int64))
}

storage := 512 * 1024 * 1024
if storageInterface, ok := fucntionContentMap["storage"]; ok && storageInterface != nil {
storage = int(storageInterface.(int64))
}

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,
Expand Down
33 changes: 28 additions & 5 deletions server/source_code_management_github_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strings"

"github.com/memphisdev/memphis/models"
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -158,16 +174,23 @@ 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
functionsDetails["other"] = append(functionsDetails["other"], fileDetails)
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
Expand Down