Semaphore/api/projects/webhook.go

174 lines
4.0 KiB
Go
Raw Normal View History

2023-07-03 01:41:13 +02:00
package projects
import (
2024-01-11 14:03:09 +01:00
"fmt"
"net/http"
2023-07-03 01:41:13 +02:00
log "github.com/Sirupsen/logrus"
2024-01-11 14:03:09 +01:00
2023-07-03 01:41:13 +02:00
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"github.com/gorilla/context"
)
func WebhookMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
webhook_id, err := helpers.GetIntParam("webhook_id", w, r)
project := context.Get(r, "project").(db.Project)
2024-01-11 14:03:09 +01:00
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid webhook ID",
2024-01-11 14:03:09 +01:00
})
return
2023-07-03 01:41:13 +02:00
}
webhook, err := helpers.Store(r).GetWebhook(project.ID, webhook_id)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
context.Set(r, "webhook", webhook)
next.ServeHTTP(w, r)
})
}
func GetWebhook(w http.ResponseWriter, r *http.Request) {
webhook := context.Get(r, "webhook").(db.Webhook)
helpers.WriteJSON(w, http.StatusOK, webhook)
}
func GetWebhooks(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
webhooks, err := helpers.Store(r).GetWebhooks(project.ID, helpers.QueryParams(r.URL))
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, webhooks)
}
2024-01-11 14:03:09 +01:00
func GetWebhookRefs(w http.ResponseWriter, r *http.Request) {
2023-07-03 01:41:13 +02:00
webhook_id, err := helpers.GetIntParam("webhook_id", w, r)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Webhook ID",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
project := context.Get(r, "project").(db.Project)
if err != nil {
helpers.WriteError(w, err)
return
}
refs, err := helpers.Store(r).GetWebhookRefs(project.ID, webhook_id)
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, refs)
}
func AddWebhook(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
var webhook db.Webhook
2024-01-11 14:03:09 +01:00
log.Info(fmt.Sprintf("Found Project: %v", project.ID))
2023-07-03 01:41:13 +02:00
if !helpers.Bind(w, r, &webhook) {
2024-01-11 14:03:09 +01:00
log.Info("Failed to bind for webhook uploads")
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Project ID in body and URL must be the same",
})
2023-07-03 01:41:13 +02:00
return
}
if webhook.ProjectID != project.ID {
2024-01-11 14:03:09 +01:00
log.Error(fmt.Sprintf("Project ID in body and URL must be the same: %v vs. %v", webhook.ProjectID, project.ID))
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2023-07-03 01:41:13 +02:00
"error": "Project ID in body and URL must be the same",
})
return
}
err := webhook.Validate()
2024-01-11 14:03:09 +01:00
if err != nil {
log.Error(err)
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2023-07-03 01:41:13 +02:00
"error": err.Error(),
2024-01-11 14:03:09 +01:00
})
2023-07-03 01:41:13 +02:00
return
}
newWebhook, errWebhook := helpers.Store(r).CreateWebhook(webhook)
2023-07-03 01:41:13 +02:00
if errWebhook != nil {
2024-01-11 14:03:09 +01:00
log.Error(errWebhook)
2023-07-03 01:41:13 +02:00
helpers.WriteError(w, errWebhook)
return
}
helpers.WriteJSON(w, http.StatusCreated, newWebhook)
2023-07-03 01:41:13 +02:00
}
func UpdateWebhook(w http.ResponseWriter, r *http.Request) {
oldWebhook := context.Get(r, "webhook").(db.Webhook)
var webhook db.Webhook
if !helpers.Bind(w, r, &webhook) {
return
}
if webhook.ID != oldWebhook.ID {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Webhook ID in body and URL must be the same",
})
return
}
if webhook.ProjectID != oldWebhook.ProjectID {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Project ID in body and URL must be the same",
})
return
}
err := helpers.Store(r).UpdateWebhook(webhook)
if err != nil {
helpers.WriteError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func DeleteWebhook(w http.ResponseWriter, r *http.Request) {
2024-01-11 14:03:09 +01:00
webhook_id, err := helpers.GetIntParam("webhook_id", w, r)
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
project := context.Get(r, "project").(db.Project)
2024-01-04 15:18:59 +01:00
err = helpers.Store(r).DeleteWebhook(project.ID, webhook_id)
2023-07-03 01:41:13 +02:00
if err == db.ErrInvalidOperation {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
"error": "Webhook failed to be deleted",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
}