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

remove all unnecessary logic of github & function in oss #1323

Merged
merged 13 commits into from
Oct 1, 2023
21 changes: 12 additions & 9 deletions models/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ package models
import "time"

type FunctionsResult struct {
FunctionName string `json:"function_name"`
Description string `json:"description"`
Tags []string `json:"tags"`
Language string `json:"language"`
LastCommit time.Time `json:"last_commit"`
Link string `json:"link"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Owner string `json:"owner"`
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"`
}

type FunctionsRes struct {
Expand Down
6 changes: 6 additions & 0 deletions server/memphis_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,3 +2190,9 @@ func validateProducersCount(stationId int, tenantName string) error {

func InitializeCloudFunctionRoutes(functionsHandler FunctionsHandler, functionsRoutes *gin.RouterGroup) {
}

// Integrations

func (it IntegrationsHandler) GetSourecCodeBranches(c *gin.Context) {
c.IndentedJSON(401, "Unautorized")
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (fh FunctionsHandler) GetAllFunctions(c *gin.Context) {
return
}

c.JSON(200, gin.H{"scm_integrated": functionsResult.ScmIntegrated, "functions": functionsResult.Functions})
c.IndentedJSON(200, gin.H{"scm_integrated": functionsResult.ScmIntegrated, "functions": functionsResult.Functions})
}

func (fh FunctionsHandler) GetFunctions(tenantName string) (models.FunctionsRes, error) {
Expand All @@ -54,7 +54,7 @@ func (fh FunctionsHandler) GetFunctions(tenantName string) (models.FunctionsRes,
}

func validateYamlContent(yamlMap map[string]interface{}) error {
requiredFields := []string{"function_name", "language"}
requiredFields := []string{"function_name", "runtime", "dependencies"}
missingFields := make([]string, 0)
for _, field := range requiredFields {
if _, exists := yamlMap[field]; !exists {
Expand Down Expand Up @@ -91,21 +91,37 @@ func GetFunctionsDetails(functionsDetails []functionDetails) ([]models.Functions
}
}

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 = ""
}

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

functions = append(functions, functionDetails)
Expand Down
222 changes: 105 additions & 117 deletions server/memphis_handlers_integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (it IntegrationsHandler) UpdateIntegration(c *gin.Context) {
return
}
integration = githubIntegration

default:
serv.Warnf("[tenant: %v]UpdateIntegration: Unsupported integration type - %v", user.TenantName, body.Name)
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": "Unsupported integration type - " + body.Name})
Expand All @@ -198,7 +197,92 @@ func (it IntegrationsHandler) UpdateIntegration(c *gin.Context) {
c.IndentedJSON(200, integration)
}

func createIntegrationsKeysAndProperties(integrationType, authToken string, channelID string, pmAlert bool, svfAlert bool, disconnectAlert bool, accessKey, secretKey, bucketName, region, url, forceS3PathStyle, token, repo, branch, repoType, repoOwner string) (map[string]interface{}, map[string]bool) {
func (it IntegrationsHandler) DisconnectIntegration(c *gin.Context) {
var body models.DisconnectIntegrationSchema
ok := utils.Validate(c, &body, false, nil)
if !ok {
return
}
user, err := getUserDetailsFromMiddleware(c)
if err != nil {
serv.Errorf("DisconnectIntegration at getUserDetailsFromMiddleware: Integration %v: %v", err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

exist, _, err := db.GetTenantByName(user.TenantName)
if err != nil {
serv.Errorf("[tenant:%v]DisconnectIntegration at GetTenantByName: %v", user.TenantName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
if !exist {
serv.Warnf("[tenant: %v]DisconnectIntegration : tenant %v does not exist", user.TenantName, user.TenantName)
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

integrationType := strings.ToLower(body.Name)
if integrationType == "github" {
err = deleteInstallationForAuthenticatedGithubApp(user.TenantName)
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
serv.Warnf("[tenant:%v]DisconnectIntegration at deleteInstallationForAuthenticatedGithubApp: %v", user.TenantName, err.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": err.Error()})
return
}
serv.Errorf("[tenant:%v]DisconnectIntegration at deleteInstallationForAuthenticatedGithubApp: %v", user.TenantName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
}

err = db.DeleteIntegration(integrationType, user.TenantName)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at db.DeleteIntegration: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

integrationUpdate := models.Integration{
Name: strings.ToLower(body.Name),
Keys: nil,
Properties: nil,
TenantName: user.TenantName,
}

msg, err := json.Marshal(integrationUpdate)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at json.Marshal: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
err = serv.sendInternalAccountMsgWithReply(serv.MemphisGlobalAccount(), INTEGRATIONS_UPDATES_SUBJ, _EMPTY_, nil, msg, true)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at sendInternalAccountMsgWithReply: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

switch body.Name {
case "slack":
update := models.SdkClientsUpdates{
Type: sendNotificationType,
Update: false,
}
serv.SendUpdateToClients(update)
}

shouldSendAnalytics, _ := shouldSendAnalytics()
if shouldSendAnalytics {
user, _ := getUserDetailsFromMiddleware(c)
analyticsParams := make(map[string]interface{})
analytics.SendEvent(user.TenantName, user.Username, analyticsParams, "user-disconnect-integration-"+integrationType)
}
c.IndentedJSON(200, gin.H{})
}

func createIntegrationsKeysAndProperties(integrationType, authToken string, channelID string, pmAlert bool, svfAlert bool, disconnectAlert bool, accessKey, secretKey, bucketName, region, url, forceS3PathStyle string, githubIntegrationDetails map[string]interface{}, repo, branch, repoType, repoOwner string) (map[string]interface{}, map[string]bool) {
keys := make(map[string]interface{})
properties := make(map[string]bool)
switch integrationType {
Expand All @@ -216,11 +300,7 @@ func createIntegrationsKeysAndProperties(integrationType, authToken string, chan
keys["region"] = region
keys["url"] = url
case "github":
keys["token"] = token
keys["connected_repos"] = []githubRepoDetails{}
if repoOwner != "" {
keys["connected_repos"] = []githubRepoDetails{{RepoName: repo, Branch: branch, Type: repoType, RepoOwner: repoOwner}}
}
keys = getGithubKeys(githubIntegrationDetails, repoOwner, repo, branch, repoType)
}

return keys, properties
Expand Down Expand Up @@ -250,14 +330,20 @@ func (it IntegrationsHandler) GetIntegrationDetails(c *gin.Context) {
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
applicationName := retrieveGithubAppName()
exist, integration, err := db.GetIntegration(strings.ToLower(body.Name), user.TenantName)
if err != nil {
serv.Errorf("[tenant: %v][user: %v]GetIntegrationDetails at db.GetIntegration: Integration %v: %v", user.TenantName, user.Username, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
} else if !exist {
c.IndentedJSON(200, nil)
return
if body.Name == "github" {
c.IndentedJSON(200, gin.H{"application_name": applicationName})
return
} else {
c.IndentedJSON(200, nil)
return
}
}

if integration.Name == "slack" && integration.Keys["auth_token"] != "" {
Expand All @@ -275,46 +361,19 @@ func (it IntegrationsHandler) GetIntegrationDetails(c *gin.Context) {
return
}
if integration.Name == "github" {
integration = sourceCodeIntegration
c.IndentedJSON(200, gin.H{"integration": integration, "repos": branchesMap})
githubIntegration := models.Integration{}
githubIntegration.Keys = map[string]interface{}{}
githubIntegration.Name = sourceCodeIntegration.Name
githubIntegration.TenantName = sourceCodeIntegration.TenantName
githubIntegration.Keys["connected_repos"] = sourceCodeIntegration.Keys["connected_repos"]
githubIntegration.Keys["memphis_functions"] = integration.Keys["memphis_functions"]
githubIntegration.Keys["application_name"] = applicationName
c.IndentedJSON(200, gin.H{"integration": githubIntegration, "repos": branchesMap})
return
}
c.IndentedJSON(200, integration)
}

func (it IntegrationsHandler) GetSourecCodeBranches(c *gin.Context) {
var body GetSourceCodeBranchesSchema
ok := utils.Validate(c, &body, false, nil)
if !ok {
return
}
user, err := getUserDetailsFromMiddleware(c)
if err != nil {
serv.Errorf("GetSourecCodeBranches at getUserDetailsFromMiddleware: Integration %v: %v", body.RepoName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

integration, branches, err := getSourceCodeDetails(user.TenantName, body, "get_all_branches")
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
serv.Warnf("[tenant: %v][user: %v]GetSourecCodeBranches at getSourceCodeDetails: Integration %v: %v", user.TenantName, user.Username, body.RepoName, err.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": err.Error()})
return
}
serv.Errorf("[tenant: %v][user: %v]GetSourecCodeBranches at getSourceCodeDetails: Integration %v: %v", user.TenantName, user.Username, body.RepoName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

if integration.Name == "" {
c.IndentedJSON(200, nil)
return
}

c.IndentedJSON(200, gin.H{"integration": integration, "branches": branches})
}

func (it IntegrationsHandler) GetAllIntegrations(c *gin.Context) {
user, err := getUserDetailsFromMiddleware(c)
if err != nil {
Expand All @@ -338,8 +397,8 @@ func (it IntegrationsHandler) GetAllIntegrations(c *gin.Context) {
if integrations[i].Name == "s3" && integrations[i].Keys["secret_key"] != "" {
integrations[i].Keys["secret_key"] = hideIntegrationSecretKey(integrations[i].Keys["secret_key"].(string))
}
if integrations[i].Name == "github" && integrations[i].Keys["token"] != "" {
integrations[i].Keys["token"] = hideIntegrationSecretKey(integrations[i].Keys["token"].(string))
if integrations[i].Name == "github" && integrations[i].Keys["installation_id"] != "" {
delete(integrations[i].Keys, "installation_id")
}
}

Expand All @@ -353,77 +412,6 @@ func (it IntegrationsHandler) GetAllIntegrations(c *gin.Context) {
c.IndentedJSON(200, integrations)
}

func (it IntegrationsHandler) DisconnectIntegration(c *gin.Context) {
var body models.DisconnectIntegrationSchema
ok := utils.Validate(c, &body, false, nil)
if !ok {
return
}
user, err := getUserDetailsFromMiddleware(c)
if err != nil {
serv.Errorf("DisconnectIntegration at getUserDetailsFromMiddleware: Integration %v: %v", err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

exist, _, err := db.GetTenantByName(user.TenantName)
if err != nil {
serv.Errorf("[tenant:%v]DisconnectIntegration at GetTenantByName: %v", user.TenantName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
if !exist {
serv.Warnf("[tenant: %v]DisconnectIntegration : tenant %v does not exist", user.TenantName, user.TenantName)
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

integrationType := strings.ToLower(body.Name)
err = db.DeleteIntegration(integrationType, user.TenantName)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at db.DeleteIntegration: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

integrationUpdate := models.Integration{
Name: strings.ToLower(body.Name),
Keys: nil,
Properties: nil,
TenantName: user.TenantName,
}

msg, err := json.Marshal(integrationUpdate)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at json.Marshal: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}
err = serv.sendInternalAccountMsgWithReply(serv.MemphisGlobalAccount(), INTEGRATIONS_UPDATES_SUBJ, _EMPTY_, nil, msg, true)
if err != nil {
serv.Errorf("[tenant: %v]DisconnectIntegration at sendInternalAccountMsgWithReply: Integration %v: %v", user.TenantName, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
return
}

switch body.Name {
case "slack":
update := models.SdkClientsUpdates{
Type: sendNotificationType,
Update: false,
}
serv.SendUpdateToClients(update)
}

shouldSendAnalytics, _ := shouldSendAnalytics()
if shouldSendAnalytics {
user, _ := getUserDetailsFromMiddleware(c)
analyticsParams := make(map[string]interface{})
analytics.SendEvent(user.TenantName, user.Username, analyticsParams, "user-disconnect-integration-"+integrationType)
}
c.IndentedJSON(200, gin.H{})
}

func (it IntegrationsHandler) RequestIntegration(c *gin.Context) {
var body models.RequestIntegrationSchema
ok := utils.Validate(c, &body, false, nil)
Expand Down
Loading