Skip to content

Commit

Permalink
Merge pull request #64 from satti-hari-krishna-reddy/webhook
Browse files Browse the repository at this point in the history
feat : added a handler function for returning all the active webhooks
  • Loading branch information
frikky authored May 20, 2024
2 parents e603f25 + 01a9f3c commit 5b02c4a
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4254,6 +4254,47 @@ func HandleGetSchedules(resp http.ResponseWriter, request *http.Request) {
resp.Write(newjson)
}

func HandleGetHooks(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
return
}

user, err := HandleApiAuthentication(resp, request)
if err != nil {
log.Printf("[WARNING] Api authentication failed in get hooks: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}

if user.Role != "admin" {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Admin required"}`))
return
}

ctx := GetContext(request)
hooks, err := GetHooks(ctx, user.ActiveOrg.Id)
if err != nil {
log.Printf("[WARNING] Failed getting hooks: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Couldn't get hooks"}`))
return
}

newjson, err := json.Marshal(hooks)
if err != nil {
log.Printf("Failed unmarshal: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed unpacking environments"}`)))
return
}

resp.WriteHeader(200)
resp.Write(newjson)
}

func HandleUpdateUser(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
Expand Down Expand Up @@ -10207,6 +10248,11 @@ func HandleDeleteHook(resp http.ResponseWriter, request *http.Request) {
fileId = location[4]
}

// Check if fileId has the prefix "webhook_"
if strings.HasPrefix(fileId, "webhook_") {
fileId = strings.TrimPrefix(fileId, "webhook_")
}

if len(fileId) != 36 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID when deleting hook is not valid"}`))
Expand Down

0 comments on commit 5b02c4a

Please sign in to comment.