Skip to content

Commit

Permalink
allow incident resolve and acknowledge to not error on conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kooi <thomas@containerinfra.com>
  • Loading branch information
thojkooi committed Jan 24, 2024
1 parent ef5a52f commit 0de3168
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/incidents/acknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/incidents/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var resolveCmd = &cobra.Command{
}

err = client.ResolveIncident(incidentID, resolvedBy)
if err != nil {
if err != nil && err != betteruptime.ErrIncidentAlreadyResolved {
return err
}
}
Expand Down
51 changes: 33 additions & 18 deletions pkg/betteruptime/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package betteruptime

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 0de3168

Please sign in to comment.