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

Get all functions endpoint #1373

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 21 additions & 16 deletions models/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,30 @@ package models
import "time"

type FunctionsResult struct {
FunctionName string `json:"function_name"`
Description string `json:"description"`
Tags []string `json:"tags"`
RunTime string `json:"runtime"`
Memory int `json:"memory"`
Storgae int `json:"storgae"`
LastCommit time.Time `json:"last_commit"`
Link string `json:"link"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Owner string `json:"owner"`
EnvironmentVars map[string]string `json:"environment_vars"`
ScmType string `json:"scm_type"`
Language string `json:"language"`
FunctionName string `json:"function_name"`
Description string `json:"description"`
Tags []string `json:"tags"`
RunTime string `json:"runtime"`
Memory int `json:"memory"`
Storgae int `json:"storgae"`
LastCommit time.Time `json:"last_commit"`
Link *string `json:"link"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Owner string `json:"owner"`
EnvironmentVars map[string]string `json:"environment_vars"`
ScmType string `json:"scm_type"`
Language string `json:"language"`
InstallInProgress bool `json:"install_in_progress"`
UpdatesAvailable bool `json:"updates_available"`
ByMemphis bool `json:"by_memphis"`
}

type FunctionsRes struct {
Functions []FunctionsResult `json:"functions"`
ScmIntegrated bool `json:"scm_integrated"`
InstalledFunctions []FunctionsResult `json:"installed_functions"`
OtherFunctions []FunctionsResult `json:"other_functions"`
ScmIntegrated bool `json:"scm_integrated"`
ConnectedRepos []map[string]interface{} `json:"connected_repos"`
}

type GetFunctionDetails struct {
Expand Down
145 changes: 86 additions & 59 deletions server/memphis_handlers_functions_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (fh FunctionsHandler) GetAllFunctions(c *gin.Context) {
}
}

c.IndentedJSON(200, gin.H{"scm_integrated": functionsResult.ScmIntegrated, "functions": functionsResult.Functions})
c.IndentedJSON(200, gin.H{"scm_integrated": functionsResult.ScmIntegrated, "other": functionsResult.OtherFunctions, "installed": functionsResult.InstalledFunctions, "connected_repos": functionsResult.ConnectedRepos})
}

func (fh FunctionsHandler) GetFunctions(tenantName string) (models.FunctionsRes, error) {
Expand All @@ -62,9 +62,25 @@ func (fh FunctionsHandler) GetFunctions(tenantName string) (models.FunctionsRes,
if err != nil {
return models.FunctionsRes{}, err
}

installedFunctions := functions["installed"]
OtherFunctions := functions["other"]
if len(installedFunctions) == 0 {
installedFunctions = []models.FunctionsResult{}
}

if len(OtherFunctions) == 0 {
OtherFunctions = []models.FunctionsResult{}
}

memphisDevFucntions := []map[string]interface{}{}
memphisDevFucntions = append(memphisDevFucntions, memphisFunctions)

allFunctions := models.FunctionsRes{
Functions: functions,
ScmIntegrated: scmIntegrated,
InstalledFunctions: installedFunctions,
OtherFunctions: OtherFunctions,
ScmIntegrated: scmIntegrated,
ConnectedRepos: memphisDevFucntions,
}

return allFunctions, nil
Expand Down Expand Up @@ -204,72 +220,83 @@ func getRepoContent(url, accessToken string, body models.GetFunctionDetails) (in
return response, nil
}

func GetFunctionsDetails(functionsDetails []functionDetails) ([]models.FunctionsResult, error) {
functions := []models.FunctionsResult{}
for _, functionDetails := range functionsDetails {
fucntionContentMap := functionDetails.ContentMap
commit := functionDetails.Commit
fileContent := functionDetails.Content
repo := functionDetails.RepoName
branch := functionDetails.Branch
owner := functionDetails.Owner
tagsInterfaceSlice, ok := fucntionContentMap["tags"].([]interface{})
tagsStrings := []string{}
if ok {
tagsStrings = make([]string, len(fucntionContentMap["tags"].([]interface{})))
for i, tag := range tagsInterfaceSlice {
tagMap := tag.(map[interface{}]interface{})
for _, v := range tagMap {
if str, ok := v.(string); ok {
tagsStrings[i] = str
func GetFunctionsDetails(functionsDetails map[string][]functionDetails) (map[string][]models.FunctionsResult, error) {
functions := map[string][]models.FunctionsResult{}
for key, functionDetails := range functionsDetails {
for _, funcDetailsPerInstalled := range functionDetails {
fucntionContentMap := funcDetailsPerInstalled.ContentMap
commit := funcDetailsPerInstalled.Commit
link := funcDetailsPerInstalled.DirectoryUrl
repo := funcDetailsPerInstalled.RepoName
branch := funcDetailsPerInstalled.Branch
owner := funcDetailsPerInstalled.Owner
tagsInterfaceSlice, ok := fucntionContentMap["tags"].([]interface{})
tagsStrings := []string{}
if ok {
tagsStrings = make([]string, len(fucntionContentMap["tags"].([]interface{})))
for i, tag := range tagsInterfaceSlice {
tagMap := tag.(map[interface{}]interface{})
for _, v := range tagMap {
if str, ok := v.(string); ok {
tagsStrings[i] = str
}
}
}
}
}

var environmentVarsStrings map[string]string
environmentVarsInterfaceSlice, ok := fucntionContentMap["environment_vars"].([]interface{})
if ok {
environmentVarsStrings = make(map[string]string, len(fucntionContentMap["environment_vars"].([]interface{})))
for _, environmentVar := range environmentVarsInterfaceSlice {
environmentVarMap := environmentVar.(map[interface{}]interface{})
for k, v := range environmentVarMap {
if str, ok := v.(string); ok {
environmentVarsStrings[k.(string)] = str
var environmentVarsStrings map[string]string
environmentVarsInterfaceSlice, ok := fucntionContentMap["environment_vars"].([]interface{})
if ok {
environmentVarsStrings = make(map[string]string, len(fucntionContentMap["environment_vars"].([]interface{})))
for _, environmentVar := range environmentVarsInterfaceSlice {
environmentVarMap := environmentVar.(map[interface{}]interface{})
for k, v := range environmentVarMap {
if str, ok := v.(string); ok {
environmentVarsStrings[k.(string)] = str
}
}
}
}
}
description, ok := fucntionContentMap["description"].(string)
if !ok {
description = ""
}
description, ok := fucntionContentMap["description"].(string)
if !ok {
description = ""
}

runtime := fucntionContentMap["runtime"].(string)
regex := regexp.MustCompile(`[0-9]+|\\.$`)
language := regex.ReplaceAllString(runtime, "")
language = strings.TrimRight(language, ".")
if strings.Contains(language, "-edge") {
language = strings.Trim(language, ".-edge")
}
runtime := fucntionContentMap["runtime"].(string)
regex := regexp.MustCompile(`[0-9]+|\\.$`)
language := regex.ReplaceAllString(runtime, "")
language = strings.TrimRight(language, ".")
if strings.Contains(language, "-edge") {
language = strings.Trim(language, ".-edge")
}

functionDetails := models.FunctionsResult{
FunctionName: fucntionContentMap["function_name"].(string),
Description: description,
Tags: tagsStrings,
RunTime: runtime,
LastCommit: *commit.Commit.Committer.Date,
Link: *fileContent.HTMLURL,
Repository: repo,
Branch: branch,
Owner: owner,
Memory: fucntionContentMap["memory"].(int),
Storgae: fucntionContentMap["storage"].(int),
EnvironmentVars: environmentVarsStrings,
Language: language,
}
byMemphis := false
if repo == memphisDevFunctionsRepoName && owner == memphisDevFunctionsOwnerName {
byMemphis = true
}

functionDetails := models.FunctionsResult{
FunctionName: fucntionContentMap["function_name"].(string),
Description: description,
Tags: tagsStrings,
RunTime: runtime,
LastCommit: *commit.Commit.Committer.Date,
Link: link,
Repository: repo,
Branch: branch,
Owner: owner,
Memory: fucntionContentMap["memory"].(int),
Storgae: fucntionContentMap["storage"].(int),
EnvironmentVars: environmentVarsStrings,
Language: language,
ScmType: "github",
InstallInProgress: false,
UpdatesAvailable: false,
ByMemphis: byMemphis,
}

functions = append(functions, functionDetails)
functions[key] = append(functions[key], functionDetails)
}
}
return functions, nil
}
20 changes: 10 additions & 10 deletions server/source_code_management_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ type GetSourceCodeBranchesSchema struct {
}

type functionDetails struct {
Content *github.RepositoryContent `json:"content"`
Commit *github.RepositoryCommit `json:"commit"`
ContentMap map[string]interface{} `json:"content_map"`
RepoName string `json:"repo_name"`
Branch string `json:"branch"`
Scm string `json:"scm"`
Owner string `json:"owner"`
DirectoryUrl *string `json:"directory_content"`
Commit *github.RepositoryCommit `json:"commit"`
ContentMap map[string]interface{} `json:"content_map"`
RepoName string `json:"repo_name"`
Branch string `json:"branch"`
Scm string `json:"scm"`
Owner string `json:"owner"`
}

func getSourceCodeDetails(tenantName string, getAllReposSchema interface{}, actionType string) (models.Integration, interface{}, error) {
return models.Integration{}, map[string]string{}, nil
}

func GetContentOfSelectedRepo(connectedRepo map[string]interface{}, contentDetails []functionDetails) ([]functionDetails, error) {
func GetContentOfSelectedRepo(connectedRepo map[string]interface{}, contentDetails map[string][]functionDetails) (map[string][]functionDetails, error) {
var err error
contentDetails, err = GetGithubContentFromConnectedRepo(connectedRepo, contentDetails)
if err != nil {
Expand All @@ -55,8 +55,8 @@ func getConnectedSourceCodeRepos(tenantName string) (map[string][]interface{}, b
return selectedReposPerSourceCodeIntegration, scmIntegrated
}

func GetContentOfSelectedRepos(tenantName string) ([]functionDetails, bool, error) {
contentDetails := []functionDetails{}
func GetContentOfSelectedRepos(tenantName string) (map[string][]functionDetails, bool, error) {
contentDetails := map[string][]functionDetails{}
connectedRepos, scmIntegrated := getConnectedSourceCodeRepos(tenantName)
var err error
for _, connectedRepoPerIntegration := range connectedRepos {
Expand Down
46 changes: 37 additions & 9 deletions server/source_code_management_github_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func containsElement(arr []string, val string) bool {
return false
}

func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, functionsDetails []functionDetails) ([]functionDetails, error) {
func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, functionsDetails map[string][]functionDetails) (map[string][]functionDetails, error) {
branch := connectedRepo["branch"].(string)
repo := connectedRepo["repo_name"].(string)
owner := connectedRepo["repo_owner"].(string)
Expand All @@ -87,7 +87,11 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun
return functionsDetails, err
}

for _, directoryContent := range repoContent {
for k, directoryContent := range repoContent {
// In order to restrict the api calls per repo
if k == 10 {
shohamroditimemphis marked this conversation as resolved.
Show resolved Hide resolved
break
}
if directoryContent.GetType() == "dir" {
_, filesContent, _, err := client.Repositories.GetContents(context.Background(), owner, repo, *directoryContent.Path, &github.RepositoryContentGetOptions{
Ref: branch})
Expand Down Expand Up @@ -125,6 +129,30 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun
contentMap["storage"] = int64(512) * 1024 * 1024
}

// TODO: need to add according to the cloud changes
shohamroditimemphis marked this conversation as resolved.
Show resolved Hide resolved
// if contentMap["dependencies"].(string) == "" {
// switch contentMap["language"] {
// case "go":
// contentMap["dependencies"] = "go.mod"
// case "nodejs":
// contentMap["dependencies"] = "package.json"
// case "python":
// contentMap["dependencies"] = "req.txt"
// }
// }

// splitPath := strings.Split(*fileContent.Path, "/")
// path := strings.TrimSpace(splitPath[0])
// if path != contentMap["function_name"].(string) {
// // errMsg := fmt.Sprintf("In the repository %s, there was an incompatibility between the function name in the git %s and the function name in the YAML file %s", repo, splitPath[0], contentMap["function_name"].(string))
// continue
// }
// if strings.Contains(path, "") {
// // errMsg := fmt.Sprintf("In the repository %s, the function name in the yaml %s can't contains spaces", repo, contentMap["function_name"].(string))
// continue
// }
//

err = validateYamlContent(contentMap)
if err != nil {
isValidFileYaml = false
Expand All @@ -139,14 +167,14 @@ func GetGithubContentFromConnectedRepo(connectedRepo map[string]interface{}, fun

if isValidFileYaml {
fileDetails := functionDetails{
Content: content,
Commit: commit,
ContentMap: contentMap,
RepoName: repo,
Branch: branch,
Owner: owner,
Commit: commit,
ContentMap: contentMap,
RepoName: repo,
Branch: branch,
Owner: owner,
DirectoryUrl: directoryContent.HTMLURL,
}
functionsDetails = append(functionsDetails, fileDetails)
functionsDetails["other"] = append(functionsDetails["other"], fileDetails)
break
}
}
Expand Down