Incidents stay separate until Alertmanager groups their alerts, but a bad
release can break the API, the connection pool and a dozen pods in
different groups, and three months later each incident on its own says
little. An event is the context they share: a release, a change, a
maintenance window or an outage. Incidents attach to it, many to many,
and one of them can be marked as the trigger. Incidents are never merged,
so each keeps its own timeline, assignee and metrics; merging would have
thrown that away and could not be undone.
Nothing attaches by itself. An incident's page and
GET /api/incidents/{id}/related-events suggest events within two hours of
it, and a person decides. Events are team-scoped like incidents, kind is
a closed set (a CHECK constraint, so the UI can colour and filter by it),
and they are hard-deleted: the incidents stay and each one's timeline
records the detach. A repeated external_id returns the event already
recorded, so CI can post a release marker and retry safely. Figures such
as time to first ack and the first alarm to last resolution are computed
when an event is read and never stored.
The web UI gets an Events page and an event page with a merged timeline,
an Events card on the incident, and a tag on queue rows. make dev seeds a
release and a maintenance window.
All of it is additive. Migration 004 only creates tables, so v0.53.1
reads the same database and ignores them. The incident JSON gains
event_count (always present) and events (omitted when empty, so clients
detect support from event_count). terdut-tui keeps working unchanged and
simply does not show events yet; no TUI version is required and none is
excluded. The operator's client is untouched. The Prometheus allowlist, the CSP and the
resource limits do not change.
465 lines
18 KiB
Go
465 lines
18 KiB
Go
package api_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ryuvia.com/niklas/terdut-server/internal/models"
|
|
)
|
|
|
|
// Events are contexts that incidents attach to. These tests cover the data
|
|
// model (one trigger, many to many, hard delete), the team boundary and the
|
|
// figures derived from the attached incidents.
|
|
|
|
func evPath(id any) string { return fmt.Sprintf("/api/events/%v", id) }
|
|
|
|
// mkEvent creates an event in the default team and returns it.
|
|
func mkEvent(t *testing.T, s *ts, over map[string]any) map[string]any {
|
|
t.Helper()
|
|
body := map[string]any{"team_id": 1, "kind": "release", "title": "release 1.0"}
|
|
for k, v := range over {
|
|
body[k] = v
|
|
}
|
|
resp := s.req(t, http.MethodPost, "/api/events", body)
|
|
if resp.StatusCode != http.StatusCreated {
|
|
resp.Body.Close()
|
|
t.Fatalf("create event: %d", resp.StatusCode)
|
|
}
|
|
var out map[string]any
|
|
decode(t, resp, &out)
|
|
return out
|
|
}
|
|
|
|
func getEvent(t *testing.T, s *ts, id any) map[string]any {
|
|
t.Helper()
|
|
var out map[string]any
|
|
decode(t, s.req(t, http.MethodGet, evPath(id), nil), &out)
|
|
return out
|
|
}
|
|
|
|
func expectStatus(t *testing.T, resp *http.Response, want int, what string) {
|
|
t.Helper()
|
|
resp.Body.Close()
|
|
if resp.StatusCode != want {
|
|
t.Errorf("%s: expected %d, got %d", what, want, resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func attach(t *testing.T, s *ts, eventID any, ids []int, trigger any) map[string]any {
|
|
t.Helper()
|
|
body := map[string]any{"incident_ids": ids}
|
|
if trigger != nil {
|
|
body["trigger_incident_id"] = trigger
|
|
}
|
|
resp := s.req(t, http.MethodPost, evPath(eventID)+"/incidents", body)
|
|
if resp.StatusCode != http.StatusOK {
|
|
resp.Body.Close()
|
|
t.Fatalf("attach: %d", resp.StatusCode)
|
|
}
|
|
var out map[string]any
|
|
decode(t, resp, &out)
|
|
return out
|
|
}
|
|
|
|
func TestEvents_CreateValidatesAndRecordsWhoMadeIt(t *testing.T) {
|
|
s := newTS(t)
|
|
|
|
e := mkEvent(t, s, map[string]any{
|
|
"summary": "new pool defaults", "external_url": "https://ci.example.com/runs/1841",
|
|
})
|
|
if e["kind"] != "release" || e["source"] != "manual" || e["created_by"] != "admin" {
|
|
t.Errorf("unexpected event: %v", e)
|
|
}
|
|
if e["incident_count"].(float64) != 0 {
|
|
t.Errorf("a new event has no incidents, got %v", e["incident_count"])
|
|
}
|
|
if e["ended_at"] != nil {
|
|
t.Errorf("a marker has no end, got %v", e["ended_at"])
|
|
}
|
|
|
|
started := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
|
|
ended := time.Now().Add(-2 * time.Hour).UTC().Format(time.RFC3339)
|
|
bad := []struct {
|
|
name string
|
|
body map[string]any
|
|
}{
|
|
{"unknown kind", map[string]any{"team_id": 1, "kind": "party", "title": "x"}},
|
|
{"no title", map[string]any{"team_id": 1, "kind": "change", "title": " "}},
|
|
{"no team", map[string]any{"kind": "change", "title": "x"}},
|
|
{"javascript url", map[string]any{"team_id": 1, "kind": "change", "title": "x", "external_url": "javascript:alert(1)"}},
|
|
{"ends before it starts", map[string]any{"team_id": 1, "kind": "change", "title": "x", "started_at": started, "ended_at": ended}},
|
|
}
|
|
for _, c := range bad {
|
|
expectStatus(t, s.req(t, http.MethodPost, "/api/events", c.body), http.StatusBadRequest, c.name)
|
|
}
|
|
}
|
|
|
|
func TestEvents_ExternalIDMakesCreateIdempotent(t *testing.T) {
|
|
s := newTS(t)
|
|
instanceKey := s.key
|
|
ciKey := createServiceAccount(t, s, instanceKey, "ci", models.ServiceAccountScopeTeam, 1)
|
|
|
|
body := map[string]any{"team_id": 1, "kind": "release", "title": "server 0.52.0", "external_id": "pipeline-1841"}
|
|
first := s.reqAs(t, ciKey, http.MethodPost, "/api/events", body)
|
|
if first.StatusCode != http.StatusCreated {
|
|
t.Fatalf("first post: %d", first.StatusCode)
|
|
}
|
|
var a map[string]any
|
|
decode(t, first, &a)
|
|
|
|
// A retry with a different title must not create a second event or change
|
|
// the first.
|
|
body["title"] = "something else"
|
|
second := s.reqAs(t, ciKey, http.MethodPost, "/api/events", body)
|
|
if second.StatusCode != http.StatusOK {
|
|
t.Fatalf("retry: expected 200, got %d", second.StatusCode)
|
|
}
|
|
var b map[string]any
|
|
decode(t, second, &b)
|
|
|
|
if a["id"] != b["id"] || b["title"] != "server 0.52.0" {
|
|
t.Errorf("the retry should return the original event, got %v then %v", a, b)
|
|
}
|
|
if a["source"] != "api" || a["created_by_service_account"] != "ci" {
|
|
t.Errorf("a service account's event is source api, got %v", a)
|
|
}
|
|
var n int
|
|
if err := s.db.QueryRow("SELECT COUNT(*) FROM events").Scan(&n); err != nil || n != 1 {
|
|
t.Errorf("expected 1 event row, got %d (%v)", n, err)
|
|
}
|
|
}
|
|
|
|
func TestEvents_AttachDetachAndDerivedFigures(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 3)
|
|
ev := mkEvent(t, s, nil)
|
|
id := ev["id"]
|
|
|
|
res := attach(t, s, id, []int{1, 2, 3}, 1)
|
|
if res["summary"].(map[string]any)["ok"].(float64) != 3 {
|
|
t.Fatalf("expected 3 attached, got %v", res["summary"])
|
|
}
|
|
again := attach(t, s, id, []int{2}, nil)
|
|
if again["summary"].(map[string]any)["noop"].(float64) != 1 {
|
|
t.Errorf("attaching twice is a no-op, got %v", again["summary"])
|
|
}
|
|
|
|
e := getEvent(t, s, id)
|
|
incs := e["incidents"].([]any)
|
|
if len(incs) != 3 || incs[0].(map[string]any)["is_trigger"] != true ||
|
|
incs[0].(map[string]any)["incident_id"].(float64) != 1 {
|
|
t.Errorf("the trigger should lead the incident list, got %v", incs)
|
|
}
|
|
if e["open_incident_count"].(float64) != 3 || e["all_resolved_at"] != nil || e["span_seconds"] != nil {
|
|
t.Errorf("nothing is resolved yet: %v", e)
|
|
}
|
|
|
|
// The incident knows about the event, and says so on its own timeline.
|
|
inc := getIncident(t, s, 2)
|
|
refs := inc["events"].([]any)
|
|
if inc["event_count"].(float64) != 1 || len(refs) != 1 || refs[0].(map[string]any)["title"] != "release 1.0" {
|
|
t.Errorf("incident should list its event, got %v", inc["events"])
|
|
}
|
|
if types := eventTypes(timeline(t, s, 2)); types[len(types)-1] != "event_attached" {
|
|
t.Errorf("expected event_attached last on the incident timeline, got %v", types)
|
|
}
|
|
if got := listIncidents(t, s, "?event_id="+fmt.Sprint(id)); len(got) != 3 {
|
|
t.Errorf("event_id filter: expected 3 incidents, got %d", len(got))
|
|
}
|
|
|
|
// Resolve everything: the figures appear.
|
|
expectStatus(t, s.req(t, http.MethodPost, "/api/incidents/2/acknowledge", nil), http.StatusOK, "ack")
|
|
for i := 1; i <= 3; i++ {
|
|
expectStatus(t, s.req(t, http.MethodPost, fmt.Sprintf("/api/incidents/%d/resolve", i), nil), http.StatusOK, "resolve")
|
|
}
|
|
e = getEvent(t, s, id)
|
|
if e["open_incident_count"].(float64) != 0 || e["all_resolved_at"] == nil || e["span_seconds"] == nil ||
|
|
e["time_to_first_ack_seconds"] == nil || e["first_triggered_at"] == nil {
|
|
t.Errorf("expected derived figures once resolved, got %v", e)
|
|
}
|
|
|
|
// Detach one; a second detach has nothing to remove.
|
|
expectStatus(t, s.req(t, http.MethodDelete, evPath(id)+"/incidents/3", nil), http.StatusNoContent, "detach")
|
|
expectStatus(t, s.req(t, http.MethodDelete, evPath(id)+"/incidents/3", nil), http.StatusNotFound, "detach again")
|
|
if types := eventTypes(timeline(t, s, 3)); types[len(types)-1] != "event_detached" {
|
|
t.Errorf("expected event_detached last, got %v", types)
|
|
}
|
|
if n := getEvent(t, s, id)["incident_count"].(float64); n != 2 {
|
|
t.Errorf("expected 2 incidents left, got %v", n)
|
|
}
|
|
}
|
|
|
|
func TestEvents_AttachRules(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1) // incident 1, default team
|
|
|
|
other := newTeam(t, s, "other")
|
|
postToIntegration(t, s, other.key, "fp-other", "OtherAlert") // incident 2, other team
|
|
ev := mkEvent(t, s, nil)
|
|
|
|
// The admin belongs to both teams, so incident 2 is visible to them but is
|
|
// not the event's team's.
|
|
res := attach(t, s, ev["id"], []int{1, 2, 99}, nil)
|
|
statuses := map[int]string{}
|
|
for _, r := range res["results"].([]any) {
|
|
m := r.(map[string]any)
|
|
statuses[int(m["id"].(float64))] = m["status"].(string)
|
|
}
|
|
want := map[int]string{1: "ok", 2: "team_mismatch", 99: "not_found"}
|
|
for id, st := range want {
|
|
if statuses[id] != st {
|
|
t.Errorf("incident %d: expected %s, got %s", id, st, statuses[id])
|
|
}
|
|
}
|
|
|
|
// A trigger that is not attached rejects the whole request, attaching nothing.
|
|
ev2 := mkEvent(t, s, map[string]any{"title": "second"})
|
|
resp := s.req(t, http.MethodPost, evPath(ev2["id"])+"/incidents",
|
|
map[string]any{"incident_ids": []int{1}, "trigger_incident_id": 2})
|
|
expectStatus(t, resp, http.StatusBadRequest, "trigger that is not attached")
|
|
if n := getEvent(t, s, ev2["id"])["incident_count"].(float64); n != 0 {
|
|
t.Errorf("a rejected request must attach nothing, got %v", n)
|
|
}
|
|
|
|
expectStatus(t, s.req(t, http.MethodPost, evPath(ev["id"])+"/incidents", map[string]any{"incident_ids": []int{}}),
|
|
http.StatusBadRequest, "no ids")
|
|
}
|
|
|
|
func TestEvents_OnlyOneTrigger(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 2)
|
|
ev := mkEvent(t, s, nil)
|
|
attach(t, s, ev["id"], []int{1, 2}, 1)
|
|
|
|
put := func(body map[string]any) *http.Response {
|
|
return s.req(t, http.MethodPut, evPath(ev["id"])+"/trigger", body)
|
|
}
|
|
expectStatus(t, put(map[string]any{"incident_id": 2}), http.StatusOK, "move the trigger")
|
|
var triggers []int
|
|
rows, err := s.db.Query("SELECT incident_id FROM event_incidents WHERE is_trigger")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for rows.Next() {
|
|
var id int
|
|
rows.Scan(&id)
|
|
triggers = append(triggers, id)
|
|
}
|
|
rows.Close()
|
|
if len(triggers) != 1 || triggers[0] != 2 {
|
|
t.Errorf("expected incident 2 as the only trigger, got %v", triggers)
|
|
}
|
|
|
|
expectStatus(t, put(map[string]any{"incident_id": nil}), http.StatusOK, "clear the trigger")
|
|
var n int
|
|
s.db.QueryRow("SELECT COUNT(*) FROM event_incidents WHERE is_trigger").Scan(&n)
|
|
if n != 0 {
|
|
t.Errorf("expected no trigger, got %d", n)
|
|
}
|
|
expectStatus(t, put(map[string]any{"incident_id": 77}), http.StatusNotFound, "trigger not attached")
|
|
}
|
|
|
|
func TestEvents_TeamBoundary(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1)
|
|
ev := mkEvent(t, s, nil)
|
|
attach(t, s, ev["id"], []int{1}, nil)
|
|
outsider := newTeam(t, s, "outsider")
|
|
|
|
check := func(method, path string, body any, want int, what string) {
|
|
t.Helper()
|
|
expectStatus(t, outsider.call(method, path, body), want, what)
|
|
}
|
|
check(http.MethodGet, evPath(ev["id"]), nil, http.StatusNotFound, "read another team's event")
|
|
check(http.MethodGet, evPath(ev["id"])+"/timeline", nil, http.StatusNotFound, "its timeline")
|
|
check(http.MethodPatch, evPath(ev["id"]), map[string]any{"title": "x"}, http.StatusNotFound, "edit it")
|
|
check(http.MethodDelete, evPath(ev["id"]), nil, http.StatusNotFound, "delete it")
|
|
check(http.MethodPost, evPath(ev["id"])+"/notes", map[string]any{"content": "hi"}, http.StatusNotFound, "note it")
|
|
check(http.MethodPost, evPath(ev["id"])+"/incidents", map[string]any{"incident_ids": []int{1}}, http.StatusNotFound, "attach to it")
|
|
check(http.MethodPost, "/api/events", map[string]any{"team_id": 1, "kind": "change", "title": "x"}, http.StatusNotFound, "create in another team")
|
|
|
|
var list []map[string]any
|
|
decode(t, outsider.call(http.MethodGet, "/api/events", nil), &list)
|
|
if len(list) != 0 {
|
|
t.Errorf("the outsider's list should be empty, got %v", list)
|
|
}
|
|
}
|
|
|
|
func TestEvents_NotesAndMergedTimeline(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1)
|
|
ev := mkEvent(t, s, map[string]any{"started_at": time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)})
|
|
attach(t, s, ev["id"], []int{1}, nil)
|
|
|
|
var note map[string]any
|
|
resp := s.req(t, http.MethodPost, evPath(ev["id"])+"/notes", map[string]any{"content": "pool max_conns looks wrong"})
|
|
if resp.StatusCode != http.StatusCreated {
|
|
t.Fatalf("add note: %d", resp.StatusCode)
|
|
}
|
|
decode(t, resp, ¬e)
|
|
expectStatus(t, s.req(t, http.MethodPost, evPath(ev["id"])+"/notes", map[string]any{"content": " "}), http.StatusBadRequest, "blank note")
|
|
|
|
var entries []map[string]any
|
|
decode(t, s.req(t, http.MethodGet, evPath(ev["id"])+"/timeline", nil), &entries)
|
|
var seen []string
|
|
for _, e := range entries {
|
|
seen = append(seen, fmt.Sprintf("%s/%s", e["source"], e["type"]))
|
|
}
|
|
got := strings.Join(seen, " ")
|
|
// The event started an hour ago, the alarm opened after, and the note came
|
|
// last. The attach record stays out of the merged timeline.
|
|
if got != "event/started incident/triggered event/note" && got != "event/started event/note incident/triggered" {
|
|
t.Errorf("unexpected merged timeline: %s", got)
|
|
}
|
|
if entries[0]["type"] != "started" {
|
|
t.Errorf("the event's own start leads, got %v", entries[0])
|
|
}
|
|
|
|
path := fmt.Sprintf("%s/notes/%v", evPath(ev["id"]), note["id"])
|
|
expectStatus(t, s.req(t, http.MethodDelete, path, nil), http.StatusNoContent, "delete own note")
|
|
expectStatus(t, s.req(t, http.MethodDelete, path, nil), http.StatusNotFound, "delete it again")
|
|
}
|
|
|
|
func TestEvents_PatchAndHardDelete(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1)
|
|
start := time.Now().Add(-time.Hour).UTC().Truncate(time.Second)
|
|
ev := mkEvent(t, s, map[string]any{
|
|
"kind": "maintenance", "started_at": start.Format(time.RFC3339),
|
|
"ended_at": start.Add(30 * time.Minute).Format(time.RFC3339),
|
|
})
|
|
id := ev["id"]
|
|
attach(t, s, id, []int{1}, nil)
|
|
|
|
var patched map[string]any
|
|
decode(t, s.req(t, http.MethodPatch, evPath(id),
|
|
map[string]any{"title": "Node pool upgrade", "kind": "change", "ended_at": nil}), &patched)
|
|
if patched["title"] != "Node pool upgrade" || patched["kind"] != "change" || patched["ended_at"] != nil {
|
|
t.Errorf("patch did not apply: %v", patched)
|
|
}
|
|
expectStatus(t, s.req(t, http.MethodPatch, evPath(id), map[string]any{"ended_at": start.Add(-time.Minute).Format(time.RFC3339)}),
|
|
http.StatusBadRequest, "end before start")
|
|
expectStatus(t, s.req(t, http.MethodPatch, evPath(id), map[string]any{"kind": "party"}), http.StatusBadRequest, "bad kind")
|
|
|
|
// A plain member cannot delete; the owner can.
|
|
var user struct {
|
|
ID int64 `json:"id"`
|
|
}
|
|
decode(t, s.req(t, http.MethodPost, "/api/users", map[string]string{"username": "plain", "email": "plain@test.com"}), &user)
|
|
expectStatus(t, s.req(t, http.MethodPost, "/api/teams/"+defaultTeam+"/members",
|
|
map[string]any{"user_id": user.ID, "role": "member"}), http.StatusNoContent, "add member")
|
|
var key struct {
|
|
Key string `json:"key"`
|
|
}
|
|
decode(t, s.req(t, http.MethodPost, fmt.Sprintf("/api/users/%d/api-keys", user.ID), map[string]string{"name": "t"}), &key)
|
|
expectStatus(t, s.reqAs(t, key.Key, http.MethodDelete, evPath(id), nil), http.StatusForbidden, "member deleting")
|
|
|
|
expectStatus(t, s.req(t, http.MethodDelete, evPath(id), nil), http.StatusNoContent, "owner deleting")
|
|
expectStatus(t, s.req(t, http.MethodGet, evPath(id), nil), http.StatusNotFound, "deleted event")
|
|
|
|
// The incident survives, detached, with a record of why.
|
|
inc := getIncident(t, s, 1)
|
|
if inc["event_count"].(float64) != 0 {
|
|
t.Errorf("the incident should have no events, got %v", inc["event_count"])
|
|
}
|
|
tl := timeline(t, s, 1)
|
|
last := tl[len(tl)-1]
|
|
if last["type"] != "event_detached" || !strings.Contains(last["detail"].(string), "event deleted") {
|
|
t.Errorf("expected a detach record, got %v", last)
|
|
}
|
|
var rows int
|
|
s.db.QueryRow("SELECT (SELECT COUNT(*) FROM event_incidents) + (SELECT COUNT(*) FROM event_notes)").Scan(&rows)
|
|
if rows != 0 {
|
|
t.Errorf("links and notes should be gone, found %d rows", rows)
|
|
}
|
|
}
|
|
|
|
func TestEvents_ListFilters(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 4)
|
|
old := time.Date(2026, 3, 1, 12, 0, 0, 0, time.UTC)
|
|
mkEvent(t, s, map[string]any{"title": "March release", "started_at": old.Format(time.RFC3339)})
|
|
busy := mkEvent(t, s, map[string]any{"kind": "outage", "title": "100% storage outage"})
|
|
mkEvent(t, s, map[string]any{"kind": "change", "title": "quiet change"})
|
|
attach(t, s, busy["id"], []int{1, 2, 3, 4}, 2)
|
|
|
|
list := func(q string) []map[string]any {
|
|
t.Helper()
|
|
var out []map[string]any
|
|
decode(t, s.req(t, http.MethodGet, "/api/events"+q, nil), &out)
|
|
return out
|
|
}
|
|
if all := list(""); len(all) != 3 || all[2]["title"] != "March release" {
|
|
t.Errorf("expected 3 events, newest first, got %v", all)
|
|
}
|
|
if got := list("?kind=outage"); len(got) != 1 || got[0]["title"] != "100% storage outage" {
|
|
t.Errorf("kind filter: %v", got)
|
|
}
|
|
if got := list("?has_incidents=true"); len(got) != 1 {
|
|
t.Errorf("has_incidents=true: %v", got)
|
|
}
|
|
if got := list("?has_incidents=false"); len(got) != 2 {
|
|
t.Errorf("has_incidents=false: %v", got)
|
|
}
|
|
if got := list("?from=2026-03-01&to=2026-03-01"); len(got) != 1 || got[0]["title"] != "March release" {
|
|
t.Errorf("date filter: %v", got)
|
|
}
|
|
// A literal percent sign must not act as a wildcard.
|
|
if got := list("?q=100%25"); len(got) != 1 {
|
|
t.Errorf("q=100%%: %v", got)
|
|
}
|
|
if got := list("?q=%25"); len(got) != 1 {
|
|
t.Errorf("a lone %% matches only titles that contain one, got %v", got)
|
|
}
|
|
|
|
row := list("?kind=outage")[0]
|
|
if row["incident_count"].(float64) != 4 || len(row["incidents"].([]any)) != 3 {
|
|
t.Errorf("a row counts all incidents but previews 3: %v", row)
|
|
}
|
|
if row["incidents"].([]any)[0].(map[string]any)["is_trigger"] != true {
|
|
t.Errorf("the preview leads with the trigger: %v", row["incidents"])
|
|
}
|
|
expectStatus(t, s.req(t, http.MethodGet, "/api/events?kind=party", nil), http.StatusBadRequest, "bad kind filter")
|
|
}
|
|
|
|
func TestEvents_RelatedEventsAreATimeWindowQuery(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1)
|
|
trig := time.Now().Add(-10 * time.Minute).UTC().Truncate(time.Second)
|
|
s.exec(t, "UPDATE incidents SET triggered_at = $1 WHERE id = 1", trig.Unix())
|
|
|
|
at := func(d time.Duration) string { return trig.Add(d).Format(time.RFC3339) }
|
|
mkEvent(t, s, map[string]any{"title": "30m before", "started_at": at(-30 * time.Minute)})
|
|
mkEvent(t, s, map[string]any{"title": "1h after", "started_at": at(time.Hour)})
|
|
mkEvent(t, s, map[string]any{"title": "5h before", "started_at": at(-5 * time.Hour)})
|
|
mkEvent(t, s, map[string]any{"title": "5h after", "started_at": at(5 * time.Hour)})
|
|
// Long ago, but still going when the incident opened.
|
|
mkEvent(t, s, map[string]any{"title": "long window", "kind": "maintenance",
|
|
"started_at": at(-6 * time.Hour), "ended_at": at(time.Hour)})
|
|
attached := mkEvent(t, s, map[string]any{"title": "already attached", "started_at": at(-time.Minute)})
|
|
attach(t, s, attached["id"], []int{1}, nil)
|
|
other := newTeam(t, s, "other")
|
|
expectStatus(t, other.call(http.MethodPost, "/api/events",
|
|
map[string]any{"team_id": other.id, "kind": "release", "title": "other team", "started_at": at(-time.Minute)}),
|
|
http.StatusCreated, "event in the other team")
|
|
|
|
var got []map[string]any
|
|
decode(t, s.req(t, http.MethodGet, "/api/incidents/1/related-events", nil), &got)
|
|
var titles []string
|
|
for _, e := range got {
|
|
titles = append(titles, e["title"].(string))
|
|
}
|
|
want := "30m before,1h after,long window"
|
|
if strings.Join(titles, ",") != want {
|
|
t.Errorf("expected %q (nearest first), got %q", want, strings.Join(titles, ","))
|
|
}
|
|
|
|
// It only suggests: nothing was attached by asking.
|
|
if n := getIncident(t, s, 1)["event_count"].(float64); n != 1 {
|
|
t.Errorf("only the explicit attach counts, got %v", n)
|
|
}
|
|
}
|