From 0de3168797297e5dbb288004117bdf7232653228 Mon Sep 17 00:00:00 2001 From: Thomas Kooi Date: Wed, 24 Jan 2024 11:21:16 +0100 Subject: [PATCH] allow incident resolve and acknowledge to not error on conflicts Signed-off-by: Thomas Kooi --- cmd/incidents/acknowledge.go | 2 +- cmd/incidents/resolve.go | 2 +- pkg/betteruptime/incidents.go | 51 ++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/cmd/incidents/acknowledge.go b/cmd/incidents/acknowledge.go index cf29490..370b79d 100644 --- a/cmd/incidents/acknowledge.go +++ b/cmd/incidents/acknowledge.go @@ -44,7 +44,7 @@ var acknowledgeCmd = &cobra.Command{ } err = client.AcknowledgeIncident(cmd.Context(), incidentID, acknowledgedBy) - if err != nil { + if err != nil && err != betteruptime.ErrIncidentAlreadyAcknowledged { return err } } diff --git a/cmd/incidents/resolve.go b/cmd/incidents/resolve.go index bf955ab..cb17595 100644 --- a/cmd/incidents/resolve.go +++ b/cmd/incidents/resolve.go @@ -26,7 +26,7 @@ var resolveCmd = &cobra.Command{ } err = client.ResolveIncident(incidentID, resolvedBy) - if err != nil { + if err != nil && err != betteruptime.ErrIncidentAlreadyResolved { return err } } diff --git a/pkg/betteruptime/incidents.go b/pkg/betteruptime/incidents.go index 7a1a996..3f19766 100644 --- a/pkg/betteruptime/incidents.go +++ b/pkg/betteruptime/incidents.go @@ -2,6 +2,7 @@ package betteruptime import ( "context" + "errors" "fmt" "net/http" "net/url" @@ -17,6 +18,13 @@ var ( numericCheckRegex = regexp.MustCompile(`^[0-9]+$`) ) +var ( + ErrIncidentAlreadyAcknowledged = errors.New("incident is already acknowledged") + ErrIncidentAlreadyResolved = errors.New("incident is already resolved") + ErrIncidentNotFound = errors.New("incident not found") + ErrUnexpectedStatusCodeFromAPI = errors.New("unexpected status received from better-uptime API") +) + // Extracts a betteruptime incident ID from the URL. // If an incidentID is provided (and not a URL), it returns that without any further processing func IncidentIDFromURL(incidentStr string) (string, error) { @@ -88,14 +96,15 @@ func (c *client) DeleteIncident(incidentID string) error { if err != nil { return err } - if resp.StatusCode() == http.StatusNotFound { - return fmt.Errorf("incident not found") - } - if resp.StatusCode() != http.StatusNoContent { - return fmt.Errorf("incorrect status response from api") + switch resp.StatusCode() { + case http.StatusNotFound: + return ErrIncidentNotFound + case http.StatusNoContent: + return nil + default: + return ErrUnexpectedStatusCodeFromAPI } - return nil } func (c *client) AcknowledgeIncident(ctx context.Context, incidentID, acknowledgedBy string) error { @@ -110,14 +119,17 @@ func (c *client) AcknowledgeIncident(ctx context.Context, incidentID, acknowledg if err != nil { return err } - if resp.StatusCode() == http.StatusNotFound { - return fmt.Errorf("incident not found") - } - if resp.StatusCode() != http.StatusNoContent { - return fmt.Errorf("incorrect status response from api") + switch resp.StatusCode() { + case http.StatusNotFound: + return ErrIncidentNotFound + case http.StatusConflict: + return ErrIncidentAlreadyAcknowledged + case http.StatusOK: + return nil // incident resolved successfully + default: + return ErrUnexpectedStatusCodeFromAPI } - return nil } func (c *client) ResolveIncident(incidentID string, resolvedBy string) error { @@ -131,14 +143,17 @@ func (c *client) ResolveIncident(incidentID string, resolvedBy string) error { if err != nil { return err } - if resp.StatusCode() == http.StatusNotFound { - return fmt.Errorf("incident not found") - } - if resp.StatusCode() != http.StatusOK { - return fmt.Errorf("incorrect status response from api") + switch resp.StatusCode() { + case http.StatusNotFound: + return ErrIncidentNotFound + case http.StatusConflict: + return ErrIncidentAlreadyResolved + case http.StatusOK: + return nil // incident resolved successfully + default: + return ErrUnexpectedStatusCodeFromAPI } - return nil } type Resolve struct {