feat(alert): add Opsgenie provider
CI / test (pull_request) Successful in 2m30s
CI / lint (pull_request) Successful in 1m1s
CI / vulncheck (pull_request) Successful in 51s

Support Opsgenie Alert API v2 with US/EU endpoint selection,
configurable priority (P1-P5), and GenieKey auth. TUI form
includes API key, priority picker, and EU instance toggle.
This commit is contained in:
2026-06-04 13:09:06 -04:00
parent 9e15b369d3
commit 3218af474e
3 changed files with 149 additions and 1 deletions
+32
View File
@@ -110,6 +110,24 @@ func gotifyPayload(priority string) PayloadFunc {
}
}
func opsgeniePayload(priority string) PayloadFunc {
return func(title, message string) ([]byte, error) {
return json.Marshal(map[string]any{
"message": limitMessage(title, 130),
"description": message,
"source": "uptop",
"priority": priority,
})
}
}
func limitMessage(s string, max int) string {
if len(s) <= max {
return s
}
return s[:max]
}
func GetProvider(cfg models.AlertConfig) Provider {
switch cfg.Type {
case "discord":
@@ -173,6 +191,20 @@ func GetProvider(cfg models.AlertConfig) Provider {
Payload: gotifyPayload(priority),
Headers: map[string]string{"X-Gotify-Key": cfg.Settings["token"]},
}
case "opsgenie":
priority := "P3"
if p, ok := cfg.Settings["priority"]; ok && p != "" {
priority = p
}
apiURL := "https://api.opsgenie.com/v2/alerts"
if eu, ok := cfg.Settings["eu"]; ok && eu == "true" {
apiURL = "https://api.eu.opsgenie.com/v2/alerts"
}
return &HTTPProvider{
URL: apiURL,
Payload: opsgeniePayload(priority),
Headers: map[string]string{"Authorization": "GenieKey " + cfg.Settings["api_key"]},
}
default:
return nil
}
+69 -1
View File
@@ -196,8 +196,76 @@ func TestHTTPProviderGotify(t *testing.T) {
}
}
func TestHTTPProviderOpsgenie(t *testing.T) {
var received map[string]any
var authHeader string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader = r.Header.Get("Authorization")
json.NewDecoder(r.Body).Decode(&received)
w.WriteHeader(202)
}))
defer srv.Close()
p := GetProvider(models.AlertConfig{Type: "opsgenie", Settings: map[string]string{
"api_key": "test-genie-key",
"priority": "P1",
}})
hp := p.(*HTTPProvider)
hp.URL = srv.URL
if err := p.Send(context.Background(), "Site Down", "mysite.com is unreachable"); err != nil {
t.Fatalf("Send: %v", err)
}
if authHeader != "GenieKey test-genie-key" {
t.Errorf("expected auth 'GenieKey test-genie-key', got '%s'", authHeader)
}
if received["message"] != "Site Down" {
t.Errorf("unexpected message: %v", received["message"])
}
if received["description"] != "mysite.com is unreachable" {
t.Errorf("unexpected description: %v", received["description"])
}
if received["source"] != "uptop" {
t.Errorf("expected source 'uptop', got '%v'", received["source"])
}
if received["priority"] != "P1" {
t.Errorf("expected priority 'P1', got '%v'", received["priority"])
}
}
func TestOpsgenieEUEndpoint(t *testing.T) {
p := GetProvider(models.AlertConfig{Type: "opsgenie", Settings: map[string]string{
"api_key": "key", "eu": "true",
}})
hp := p.(*HTTPProvider)
if hp.URL != "https://api.eu.opsgenie.com/v2/alerts" {
t.Errorf("expected EU URL, got '%s'", hp.URL)
}
}
func TestOpsgenieUSEndpoint(t *testing.T) {
p := GetProvider(models.AlertConfig{Type: "opsgenie", Settings: map[string]string{
"api_key": "key",
}})
hp := p.(*HTTPProvider)
if hp.URL != "https://api.opsgenie.com/v2/alerts" {
t.Errorf("expected US URL, got '%s'", hp.URL)
}
}
func TestLimitMessage(t *testing.T) {
short := "short"
if got := limitMessage(short, 130); got != short {
t.Errorf("expected '%s', got '%s'", short, got)
}
long := string(make([]byte, 200))
if got := limitMessage(long, 130); len(got) != 130 {
t.Errorf("expected length 130, got %d", len(got))
}
}
func TestGetProviderNewTypes(t *testing.T) {
for _, typ := range []string{"telegram", "pagerduty", "pushover", "gotify"} {
for _, typ := range []string{"telegram", "pagerduty", "pushover", "gotify", "opsgenie"} {
p := GetProvider(models.AlertConfig{Type: typ, Settings: map[string]string{
"token": "x", "chat_id": "1", "routing_key": "k", "user": "u", "url": "http://localhost",
}})