From 19d28047a5e794e4d566c59ad4ce094404df22c8 Mon Sep 17 00:00:00 2001 From: Aditya <60684641+0x0elliot@users.noreply.github.com> Date: Tue, 24 Sep 2024 03:56:53 +0530 Subject: [PATCH] fix: Hook status issues + other small issues during deletion and creation --- shared.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/shared.go b/shared.go index 2aa06f6..1b25d41 100755 --- a/shared.go +++ b/shared.go @@ -12270,7 +12270,6 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) { resp.WriteHeader(401) resp.Write([]byte(`{"success": false, "reason": "Required fields id and name can't be empty"}`)) return - } validTypes := []string{ @@ -12292,6 +12291,35 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) { return } + originalWorkflow, err := GetWorkflow(ctx, requestdata.Workflow) + if err != nil { + log.Printf("[WARNING] Failed getting workflow %s: %s", requestdata.Workflow, err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false, "reason": "Workflow doesn't exist"}`)) + return + } + + originalHook, err := GetHook(ctx, newId) + if err == nil { + log.Printf("[WARNING] Hook with ID %s doesn't exist", newId) + } + + if originalWorkflow.OrgId != user.ActiveOrg.Id { + log.Printf("[WARNING] User %s doesn't have access to workflow %s", user.Username, requestdata.Workflow) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + + if (user.Id != originalHook.Owner || len(user.Id) == 0) && originalHook.Id != "" { + if originalHook.OrgId != user.ActiveOrg.Id && originalHook.OrgId != "" { + log.Printf("[WARNING] User %s doesn't have access to hook %s", user.Username, newId) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + } + // Let remote endpoint handle access checks (shuffler.io) baseUrl := "https://shuffler.io" if len(os.Getenv("SHUFFLE_GCEPROJECT")) > 0 && len(os.Getenv("SHUFFLE_GCEPROJECT_LOCATION")) > 0 { @@ -12373,6 +12401,33 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) { return } + // set the same for the workflow + workflow, err := GetWorkflow(ctx, requestdata.Workflow) + if err != nil { + log.Printf("[WARNING] Failed getting workflow %s: %s", requestdata.Workflow, err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + + // get the webhook trigger with the same id + for triggerIndex, trigger := range workflow.Triggers { + if trigger.ID == newId { + workflow.Triggers[triggerIndex].Status = "running" + log.Printf("[INFO] Changed status of trigger %s to running", newId) + break + } + } + + // update the workflow + err = SetWorkflow(ctx, *workflow, workflow.ID) + if err != nil { + log.Printf("[WARNING] Failed setting workflow %s: %s", workflow.ID, err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + log.Printf("[INFO] Set up a new hook with ID %s and environment %s", newId, hook.Environment) resp.WriteHeader(200) resp.Write([]byte(`{"success": true}`)) @@ -12466,6 +12521,24 @@ func HandleDeleteHook(resp http.ResponseWriter, request *http.Request) { return } + // find workflow and set status to stopped + workflow, err := GetWorkflow(ctx, hook.Workflows[0]) + if err != nil { + log.Printf("[WARNING] Failed getting workflow %s: %s", hook.Workflows[0], err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + + if len(workflow.Triggers) > 0 { + for triggerIndex, trigger := range workflow.Triggers { + if trigger.ID == fileId { + workflow.Triggers[triggerIndex].Status = "stopped" + break + } + } + } + if hook.Environment == "cloud" && project.Environment != "cloud" { log.Printf("[INFO] Should STOP cloud webhook https://shuffler.io/api/v1/hooks/webhook_%s", hook.Id) org, err := GetOrg(ctx, user.ActiveOrg.Id)