feat: incident updates with a status, public or internal
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m54s
ci / test (pull_request) Successful in 12m15s
ci / build (pull_request) Successful in 11m0s
ci / image (pull_request) Successful in 4m23s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m54s
ci / test (pull_request) Successful in 12m15s
ci / build (pull_request) Successful in 11m0s
ci / image (pull_request) Successful in 4m23s
Outages open and close with their monitor; now people can say what is going on. An incident gets an optional title and updates, each with a status (investigating, identified, monitoring, resolved) and a Markdown message, shown on status pages or kept as an internal note. The incident shows the latest update's status, resolved once its monitor recovered. The console lists title and status and has a page per incident with its timeline (outage, updates, recovery) and, for incidents.write in the monitor's group, forms to post updates and set the title. Status pages show title, status and public updates. API: GET/PATCH /org/incidents/:id, POST and DELETE /org/incidents/:id/updates; the incident list carries title and status; the organization export includes the updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
6f0cf59406
commit
43a04e99b1
27 files changed
+2753
-908
No files matched your search
@@ -0,0 +1,121 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
)
|
||||
|
||||
func TestIncidentUpdates(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
ctx := context.Background()
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("iu")+".test", uniq("iu-"))
|
||||
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Login", "type": "heartbeat", "interval_seconds": 3600})
|
||||
monitorID := m["id"].(string)
|
||||
slug := uniq("iup-")
|
||||
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme"})
|
||||
pageID := page["id"].(string)
|
||||
owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "Acme", "published": true})
|
||||
if st, body := owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/monitors", map[string]any{"monitors": []map[string]any{{"monitor_id": monitorID}}}); st != http.StatusNoContent && st != http.StatusOK {
|
||||
t.Fatalf("list monitor on page = %d %v", st, body)
|
||||
}
|
||||
var incident uuid.UUID
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
return tx.QueryRow(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, cause)
|
||||
SELECT org_id, id, now() - interval '10 minutes', '{}' FROM monitors WHERE id = $1 RETURNING id`, monitorID).Scan(&incident)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := "/api/v1/org/incidents/" + incident.String()
|
||||
status := func() string {
|
||||
_, body := owner.do("GET", base, nil)
|
||||
s, _ := body["status"].(string)
|
||||
return s
|
||||
}
|
||||
if s := status(); s != "" {
|
||||
t.Errorf("status before any update = %q", s)
|
||||
}
|
||||
|
||||
// Updates with a status, public or internal, and a title.
|
||||
for _, bad := range []map[string]any{
|
||||
{"status": "panicking", "message": "x"},
|
||||
{"status": "investigating", "message": " "},
|
||||
} {
|
||||
st, body := owner.do("POST", base+"/updates", bad)
|
||||
expect(t, "invalid update", st, body, http.StatusUnprocessableEntity, "invalid_input")
|
||||
}
|
||||
if st, body := owner.do("POST", base+"/updates", map[string]any{"status": "investigating", "message": "We are **looking** into it."}); st != http.StatusCreated {
|
||||
t.Fatalf("post update = %d %v", st, body)
|
||||
}
|
||||
if st, body := owner.do("PATCH", base, map[string]any{"title": "Sign-in is slow"}); st != http.StatusNoContent {
|
||||
t.Fatalf("set title = %d %v", st, body)
|
||||
}
|
||||
_, note := owner.do("POST", base+"/updates", map[string]any{"status": "identified", "message": "DB failover, ask Bob", "public": false})
|
||||
if s := status(); s != "identified" {
|
||||
t.Errorf("status = %q, want the latest update's", s)
|
||||
}
|
||||
_, list := owner.do("GET", "/api/v1/org/incidents", nil)
|
||||
if first := list["incidents"].([]any)[0].(map[string]any); first["title"] != "Sign-in is slow" || first["status"] != "identified" {
|
||||
t.Errorf("incident list = %v", first)
|
||||
}
|
||||
|
||||
// Status pages show the title, the status and only public updates.
|
||||
snap, err := e.statuspageClient(t, nil).Page(ctx, "", slug)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var shown agentapi.PageIncident
|
||||
for _, i := range snap.Incidents {
|
||||
if i.Component == "Login" {
|
||||
shown = i
|
||||
}
|
||||
}
|
||||
if shown.Title != "Sign-in is slow" || shown.Status != "identified" || len(shown.Updates) != 1 || !strings.Contains(shown.Updates[0].Message, "looking") {
|
||||
t.Errorf("status page incident = %+v", shown)
|
||||
}
|
||||
|
||||
// The console page tells the story, internal notes marked.
|
||||
_, _, html := owner.page("/incidents/" + incident.String())
|
||||
for _, want := range []string{"Sign-in is slow", "<strong>looking</strong>", "DB failover", ">internal<", "Post update", "Outage began"} {
|
||||
if !strings.Contains(html, want) {
|
||||
t.Errorf("incident page lacks %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
// Readers read, writers write.
|
||||
viewer := e.joinVia(t, owner.invite("v@"+uniq("iu")+".test", owner.roleID("viewer")))
|
||||
if st, _ := viewer.do("GET", base, nil); st != http.StatusOK {
|
||||
t.Errorf("viewer reads incident = %d", st)
|
||||
}
|
||||
st, body := viewer.do("POST", base+"/updates", map[string]any{"status": "monitoring", "message": "x"})
|
||||
expect(t, "viewer posts update", st, body, http.StatusForbidden, "forbidden")
|
||||
if _, _, html := viewer.page("/incidents/" + incident.String()); strings.Contains(html, "Post update") {
|
||||
t.Error("the viewer is offered the update form")
|
||||
}
|
||||
|
||||
// Deleting an update falls back to the previous status; the monitor
|
||||
// recovering resolves the incident whatever was said.
|
||||
if st, _ := owner.do("DELETE", base+"/updates/"+note["id"].(string), nil); st != http.StatusNoContent || status() != "investigating" {
|
||||
t.Errorf("after deleting the latest update status = %q", status())
|
||||
}
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE incidents SET resolved_at = $2 WHERE id = $1`, incident, time.Now())
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := status(); s != "resolved" {
|
||||
t.Errorf("status after recovery = %q", s)
|
||||
}
|
||||
if st, _ := owner.do("GET", "/api/v1/org/incidents/"+uuid.NewString(), nil); st != http.StatusNotFound {
|
||||
t.Errorf("unknown incident = %d", st)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user