Skip to content

Commit cad7497

Browse files
committed
feat: auto-handle array payloads for /api/incidents
- Detect JSON arrays and create one incident per item; return total created - Keep single-object payloads unchanged; reject empty or invalid JSON bodies
1 parent 686d58b commit cad7497

1 file changed

Lines changed: 42 additions & 112 deletions

File tree

pkg/controllers/incident.go

Lines changed: 42 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"strings"
87

98
"github.com/VersusControl/versus-incident/pkg/config"
109
"github.com/VersusControl/versus-incident/pkg/services"
@@ -14,143 +13,74 @@ import (
1413

1514
func CreateIncident(c *fiber.Ctx) error {
1615
cfg := config.GetConfig()
17-
format := strings.ToLower(c.Query("format", "json"))
16+
raw := c.Body()
1817

1918
if cfg.Alert.DebugBody {
20-
rawBody := c.Body()
21-
2219
// Log the raw request body for debugging purposes
23-
fmt.Println("Raw Request Body:", string(rawBody))
20+
fmt.Println("Raw Request Body:", string(raw))
2421
}
2522

26-
switch format {
27-
case "json_stream":
28-
decoder := json.NewDecoder(bytes.NewReader(c.Body()))
29-
count := 0
30-
var merged map[string]interface{}
31-
var logs []string
32-
33-
for decoder.More() {
34-
record := map[string]interface{}{}
35-
if err := decoder.Decode(&record); err != nil {
36-
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid json_stream input"})
37-
}
38-
39-
count++
40-
41-
if merged == nil {
42-
merged = map[string]interface{}{}
43-
for k, v := range record {
44-
merged[k] = v
45-
}
46-
}
47-
48-
if logVal, ok := record["Logs"].(string); ok && logVal != "" {
49-
logs = append(logs, logVal)
50-
} else if logVal, ok := record["log"].(string); ok && logVal != "" {
51-
logs = append(logs, logVal)
52-
} else {
53-
if raw, err := json.Marshal(record); err == nil {
54-
logs = append(logs, string(raw))
55-
}
56-
}
57-
}
58-
59-
if count == 0 {
60-
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "No incidents found in json_stream"})
61-
}
62-
63-
if len(logs) > 0 {
64-
merged["Logs"] = strings.Join(logs, "\n")
65-
merged["LogsList"] = logs
66-
}
67-
68-
var err error
69-
if len(c.Queries()) > 0 {
70-
overwriteVaule := c.Queries()
71-
err = services.CreateIncident("", &merged, &overwriteVaule)
72-
} else {
73-
err = services.CreateIncident("", &merged)
74-
}
75-
76-
if err != nil {
77-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
78-
}
23+
// Detect if payload is an array; otherwise treat as single JSON object
24+
trimmed := bytes.TrimSpace(raw)
25+
if len(trimmed) == 0 {
26+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Empty body"})
27+
}
7928

80-
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
81-
"status": "Incident created",
82-
"count": count,
83-
})
84-
case "json_array":
29+
// Handle JSON array
30+
if trimmed[0] == '[' {
8531
var records []map[string]interface{}
86-
if err := json.Unmarshal(c.Body(), &records); err != nil {
87-
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid json_array input"})
32+
if err := json.Unmarshal(raw, &records); err != nil {
33+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid JSON array input"})
8834
}
89-
9035
if len(records) == 0 {
91-
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "No incidents found in json_array"})
92-
}
93-
94-
merged := map[string]interface{}{}
95-
for k, v := range records[0] {
96-
merged[k] = v
97-
}
98-
99-
var logs []string
100-
for _, record := range records {
101-
if logVal, ok := record["Logs"].(string); ok && logVal != "" {
102-
logs = append(logs, logVal)
103-
} else if logVal, ok := record["log"].(string); ok && logVal != "" {
104-
logs = append(logs, logVal)
105-
} else {
106-
if raw, err := json.Marshal(record); err == nil {
107-
logs = append(logs, string(raw))
108-
}
109-
}
110-
}
111-
112-
if len(logs) > 0 {
113-
merged["Logs"] = strings.Join(logs, "\n")
114-
merged["LogsList"] = logs
36+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "No incidents found in array"})
11537
}
11638

11739
var err error
11840
if len(c.Queries()) > 0 {
11941
overwriteVaule := c.Queries()
120-
err = services.CreateIncident("", &merged, &overwriteVaule)
42+
for _, record := range records {
43+
// capture pointer per iteration
44+
rec := record
45+
if err = services.CreateIncident("", &rec, &overwriteVaule); err != nil {
46+
break
47+
}
48+
}
12149
} else {
122-
err = services.CreateIncident("", &merged)
50+
for _, record := range records {
51+
rec := record
52+
if err = services.CreateIncident("", &rec); err != nil {
53+
break
54+
}
55+
}
12356
}
12457

12558
if err != nil {
12659
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
12760
}
12861

12962
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
130-
"status": "Incident created",
63+
"status": "Incidents created",
13164
"count": len(records),
13265
})
133-
default:
134-
body := &map[string]interface{}{}
135-
136-
if err := c.BodyParser(body); err != nil {
137-
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"})
138-
}
139-
140-
var err error
66+
}
14167

142-
// If query parameters exist, get the value to overwrite the default configuration
143-
if len(c.Queries()) > 0 {
144-
overwriteVaule := c.Queries()
145-
err = services.CreateIncident("", body, &overwriteVaule)
146-
} else {
147-
err = services.CreateIncident("", body)
148-
}
68+
body := &map[string]interface{}{}
69+
if err := c.BodyParser(body); err != nil {
70+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"})
71+
}
14972

150-
if err != nil {
151-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
152-
}
73+
var err error
74+
if len(c.Queries()) > 0 {
75+
overwriteVaule := c.Queries()
76+
err = services.CreateIncident("", body, &overwriteVaule)
77+
} else {
78+
err = services.CreateIncident("", body)
79+
}
15380

154-
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"status": "Incident created"})
81+
if err != nil {
82+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
15583
}
84+
85+
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"status": "Incident created"})
15686
}

0 commit comments

Comments
 (0)