2024-03-20 14:01:52 +01:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2024-04-12 10:00:44 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/api/helpers"
|
|
|
|
"github.com/semaphoreui/semaphore/db"
|
|
|
|
"github.com/semaphoreui/semaphore/pkg/random"
|
|
|
|
"github.com/semaphoreui/semaphore/util"
|
2024-03-20 14:01:52 +01:00
|
|
|
"github.com/gorilla/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type publicAlias struct {
|
2024-03-20 22:35:47 +01:00
|
|
|
ID int `json:"id"`
|
2024-03-20 14:01:52 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func getPublicAlias(alias db.IntegrationAlias) publicAlias {
|
2024-03-20 14:55:47 +01:00
|
|
|
|
2024-03-20 14:01:52 +01:00
|
|
|
aliasURL := util.Config.WebHost
|
|
|
|
|
|
|
|
if !strings.HasSuffix(aliasURL, "/") {
|
|
|
|
aliasURL += "/"
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
aliasURL += "api/integrations/" + alias.Alias
|
2024-03-20 14:01:52 +01:00
|
|
|
|
|
|
|
return publicAlias{
|
2024-03-20 22:35:47 +01:00
|
|
|
ID: alias.ID,
|
2024-03-20 14:01:52 +01:00
|
|
|
URL: aliasURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func getPublicAliases(aliases []db.IntegrationAlias) (res []publicAlias) {
|
2024-03-20 14:01:52 +01:00
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
res = make([]publicAlias, 0)
|
|
|
|
for _, alias := range aliases {
|
|
|
|
res = append(res, getPublicAlias(alias))
|
2024-03-20 14:01:52 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
return
|
2024-03-20 14:01:52 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func GetIntegrationAlias(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
integration, ok := context.Get(r, "integration").(db.Integration)
|
|
|
|
|
|
|
|
var integrationId *int
|
|
|
|
if ok {
|
|
|
|
integrationId = &integration.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases, err := helpers.Store(r).GetIntegrationAliases(project.ID, integrationId)
|
2024-03-20 14:01:52 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, getPublicAliases(aliases))
|
2024-03-20 14:01:52 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func AddIntegrationAlias(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
integration, ok := context.Get(r, "integration").(db.Integration)
|
|
|
|
|
|
|
|
var integrationId *int
|
|
|
|
if ok {
|
|
|
|
integrationId = &integration.ID
|
|
|
|
}
|
2024-03-20 16:30:55 +01:00
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
alias, err := helpers.Store(r).CreateIntegrationAlias(db.IntegrationAlias{
|
2024-04-12 10:00:44 +02:00
|
|
|
Alias: random.String(16),
|
2024-03-20 22:35:47 +01:00
|
|
|
ProjectID: project.ID,
|
|
|
|
IntegrationID: integrationId,
|
2024-03-20 16:30:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, getPublicAlias(alias))
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveIntegrationAlias(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
aliasID, err := helpers.GetIntParam("alias_id", w, r)
|
2024-03-20 16:30:55 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
err = helpers.Store(r).DeleteIntegrationAlias(project.ID, aliasID)
|
2024-03-20 14:01:52 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 16:30:55 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2024-03-20 14:01:52 +01:00
|
|
|
}
|