ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m38s
ci / test (pull_request) Successful in 14m21s
ci / build (pull_request) Successful in 16m36s
ci / image (pull_request) Successful in 3m37s
Organizations add statuses of their own, with a name and a color, next to the built-in ones (Settings, incident statuses; API under /org/incident-statuses, managed with org.manage). Updates pick them by ID; the console and status pages show them in their color. Renaming or recoloring carries into the history; a status in use cannot be deleted (409 status_in_use). API responses give status "custom" with status_id, status_name and status_color. Status pages now list recent incidents before planned maintenance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
158 lines
4.5 KiB
Go
158 lines
4.5 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
|
)
|
|
|
|
// An organization's own incident statuses, next to the built-in ones.
|
|
|
|
var hexColor = regexp.MustCompile(`^#[0-9a-f]{6}$`)
|
|
|
|
type incidentStatusRequest struct {
|
|
Name string `json:"name" binding:"required,max=40"`
|
|
Color string `json:"color" binding:"required,max=7"`
|
|
}
|
|
|
|
type incidentStatusView struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Color string `json:"color"`
|
|
}
|
|
|
|
// validStatusRequest normalizes req and reports whether it is usable.
|
|
func validStatusRequest(c *gin.Context, req *incidentStatusRequest) bool {
|
|
req.Name, req.Color = strings.TrimSpace(req.Name), strings.ToLower(strings.TrimSpace(req.Color))
|
|
if req.Name == "" || !hexColor.MatchString(req.Color) {
|
|
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) listIncidentStatuses(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var rows []store.IncidentStatus
|
|
if err := s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.ListIncidentStatuses(ctx)
|
|
return err
|
|
}); err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
out := make([]incidentStatusView, len(rows))
|
|
for i, r := range rows {
|
|
out[i] = incidentStatusView{ID: r.ID, Name: r.Name, Color: r.Color}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"statuses": out})
|
|
}
|
|
|
|
func (s *Server) createIncidentStatus(c *gin.Context) {
|
|
var req incidentStatusRequest
|
|
if !bind(c, &req) || !validStatusRequest(c, &req) {
|
|
return
|
|
}
|
|
p := current(c)
|
|
ctx := c.Request.Context()
|
|
var row store.IncidentStatus
|
|
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
var err error
|
|
row, err = q.CreateIncidentStatus(ctx, store.CreateIncidentStatusParams{OrgID: p.Org.ID, Name: req.Name, Color: req.Color})
|
|
return err
|
|
})
|
|
if isUniqueViolation(err, "incident_statuses_org_id_name_key") {
|
|
fail(c, http.StatusConflict, "status_name_taken")
|
|
return
|
|
}
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
auditDetail(c, "name", req.Name)
|
|
c.JSON(http.StatusCreated, incidentStatusView{ID: row.ID, Name: row.Name, Color: row.Color})
|
|
}
|
|
|
|
// updateIncidentStatus renames or recolors a status, also in the history
|
|
// of incidents that used it.
|
|
func (s *Server) updateIncidentStatus(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
var req incidentStatusRequest
|
|
if !bind(c, &req) || !validStatusRequest(c, &req) {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
var n int64
|
|
err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
|
n, err = q.UpdateIncidentStatus(ctx, store.UpdateIncidentStatusParams{ID: id, Name: req.Name, Color: req.Color})
|
|
return err
|
|
})
|
|
switch {
|
|
case isUniqueViolation(err, "incident_statuses_org_id_name_key"):
|
|
fail(c, http.StatusConflict, "status_name_taken")
|
|
case err != nil:
|
|
internalError(c, err)
|
|
case n == 0:
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
default:
|
|
auditDetail(c, "name", req.Name)
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
// deleteIncidentStatus removes a status nobody used yet; history keeps
|
|
// the ones in use.
|
|
func (s *Server) deleteIncidentStatus(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
var n int64
|
|
err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
|
n, err = q.DeleteIncidentStatus(ctx, id)
|
|
return err
|
|
})
|
|
switch {
|
|
case isForeignKeyViolation(err):
|
|
fail(c, http.StatusConflict, "status_in_use")
|
|
case err != nil:
|
|
internalError(c, err)
|
|
case n == 0:
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
default:
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func (s *Server) incidentStatusesView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
|
ctx := c.Request.Context()
|
|
var rows []store.IncidentStatus
|
|
if err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.ListIncidentStatuses(ctx)
|
|
return err
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
v := make([]web.CustomIncidentStatus, len(rows))
|
|
for i, r := range rows {
|
|
v[i] = web.CustomIncidentStatus{ID: r.ID.String(), Name: r.Name, Color: r.Color}
|
|
}
|
|
return web.IncidentStatusesPage(page, me, v, p.Grants.Can(authz.OrgManage)), nil
|
|
}
|