Skip to content

Commit

Permalink
fix: Hook status issues + other small issues during deletion and crea…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
0x0elliot committed Sep 23, 2024
1 parent e7cc655 commit 19d2804
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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}`))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 19d2804

Please sign in to comment.