Semaphore/api/projects/integration.go

174 lines
4.3 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"
2024-03-03 11:57:39 +01: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"
)
2024-02-11 20:52:14 +01:00
func IntegrationMiddleware(next http.Handler) http.Handler {
2023-07-03 01:41:13 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-03-06 22:17:34 +01:00
integrationId, err := helpers.GetIntParam("integration_id", w, r)
projectId, err := helpers.GetIntParam("project_id", w, r)
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{
2024-02-11 20:52:14 +01:00
"error": "Invalid integration ID",
2024-01-11 14:03:09 +01:00
})
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 22:17:34 +01:00
integration, err := helpers.Store(r).GetIntegration(projectId, integrationId)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
2024-02-11 20:52:14 +01:00
context.Set(r, "integration", integration)
2023-07-03 01:41:13 +02:00
next.ServeHTTP(w, r)
})
}
2024-02-11 20:52:14 +01:00
func GetIntegration(w http.ResponseWriter, r *http.Request) {
integration := context.Get(r, "integration").(db.Integration)
helpers.WriteJSON(w, http.StatusOK, integration)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func GetIntegrations(w http.ResponseWriter, r *http.Request) {
2023-07-03 01:41:13 +02:00
project := context.Get(r, "project").(db.Project)
2024-02-11 20:52:14 +01:00
integrations, err := helpers.Store(r).GetIntegrations(project.ID, helpers.QueryParams(r.URL))
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
2024-02-11 20:52:14 +01:00
helpers.WriteJSON(w, http.StatusOK, integrations)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func GetIntegrationRefs(w http.ResponseWriter, r *http.Request) {
integration_id, err := helpers.GetIntParam("integration_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2024-02-11 20:52:14 +01:00
"error": "Invalid Integration ID",
2023-07-03 01:41:13 +02:00
})
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
}
2024-02-11 20:52:14 +01:00
refs, err := helpers.Store(r).GetIntegrationRefs(project.ID, integration_id)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, refs)
}
2024-02-11 20:52:14 +01:00
func AddIntegration(w http.ResponseWriter, r *http.Request) {
2023-07-03 01:41:13 +02:00
project := context.Get(r, "project").(db.Project)
2024-02-11 20:52:14 +01:00
var integration db.Integration
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
2024-02-11 20:52:14 +01:00
if !helpers.Bind(w, r, &integration) {
log.Info("Failed to bind for integration 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
}
2024-02-11 20:52:14 +01:00
if integration.ProjectID != project.ID {
log.Error(fmt.Sprintf("Project ID in body and URL must be the same: %v vs. %v", integration.ProjectID, project.ID))
2024-01-11 14:03:09 +01:00
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
}
2024-02-11 20:52:14 +01:00
err := integration.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
}
2024-02-11 20:52:14 +01:00
newIntegration, errIntegration := helpers.Store(r).CreateIntegration(integration)
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
if errIntegration != nil {
log.Error(errIntegration)
helpers.WriteError(w, errIntegration)
2023-07-03 01:41:13 +02:00
return
}
2024-02-11 20:52:14 +01:00
helpers.WriteJSON(w, http.StatusCreated, newIntegration)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func UpdateIntegration(w http.ResponseWriter, r *http.Request) {
oldIntegration := context.Get(r, "integration").(db.Integration)
var integration db.Integration
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
if !helpers.Bind(w, r, &integration) {
2023-07-03 01:41:13 +02:00
return
}
2024-02-11 20:52:14 +01:00
if integration.ID != oldIntegration.ID {
2023-07-03 01:41:13 +02:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2024-02-11 20:52:14 +01:00
"error": "Integration ID in body and URL must be the same",
2023-07-03 01:41:13 +02:00
})
return
}
2024-02-11 20:52:14 +01:00
if integration.ProjectID != oldIntegration.ProjectID {
2023-07-03 01:41:13 +02:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Project ID in body and URL must be the same",
})
return
}
2024-02-11 20:52:14 +01:00
err := helpers.Store(r).UpdateIntegration(integration)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
2024-02-11 20:52:14 +01:00
func DeleteIntegration(w http.ResponseWriter, r *http.Request) {
integration_id, err := helpers.GetIntParam("integration_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-02-11 20:52:14 +01:00
err = helpers.Store(r).DeleteIntegration(project.ID, integration_id)
2023-07-03 01:41:13 +02:00
if err == db.ErrInvalidOperation {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
2024-02-11 20:52:14 +01:00
"error": "Integration failed to be deleted",
2023-07-03 01:41:13 +02:00
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
}