From 6558bc134e5803712c5681715ecdd08ab2f4e230 Mon Sep 17 00:00:00 2001 From: Nathan Beals Date: Fri, 17 Nov 2023 14:01:38 -0500 Subject: [PATCH] Attempt to extract an IncidentID from a url - Maintains old behaviour where you can directly pass an incidentID - if a URL is passed, extracts the ID from it - Also fixed two typos --- cmd/incidents/acknowledge.go | 11 ++++++++--- cmd/incidents/delete.go | 9 +++++++-- cmd/incidents/resolve.go | 9 +++++++-- cmd/statuspages/resources.go | 2 +- pkg/betteruptime/incidents.go | 25 +++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/incidents/acknowledge.go b/cmd/incidents/acknowledge.go index 8197fba..cf29490 100644 --- a/cmd/incidents/acknowledge.go +++ b/cmd/incidents/acknowledge.go @@ -28,7 +28,7 @@ var acknowledgeCmd = &cobra.Command{ return err } for _, incident := range activeIncidents { - fmt.Printf("acknowleding incident %s (%s)\n", incident.Attributes.Name, incident.Id) + fmt.Printf("acknowledging incident %s (%s)\n", incident.Attributes.Name, incident.Id) err := client.AcknowledgeIncident(cmd.Context(), incident.Id, acknowledgedBy) if err != nil { return err @@ -37,8 +37,13 @@ var acknowledgeCmd = &cobra.Command{ return nil } - for _, incidentID := range args { - err := client.AcknowledgeIncident(cmd.Context(), incidentID, acknowledgedBy) + for _, possibleID := range args { + incidentID, err := betteruptime.IncidentIDFromURL(possibleID) + if err != nil { + return err + } + + err = client.AcknowledgeIncident(cmd.Context(), incidentID, acknowledgedBy) if err != nil { return err } diff --git a/cmd/incidents/delete.go b/cmd/incidents/delete.go index 96a10fc..b715a06 100644 --- a/cmd/incidents/delete.go +++ b/cmd/incidents/delete.go @@ -15,8 +15,13 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { client := betteruptime.NewClient() - for _, incidentID := range args { - err := client.DeleteIncident(incidentID) + for _, possibleID := range args { + incidentID, err := betteruptime.IncidentIDFromURL(possibleID) + if err != nil { + return err + } + + err = client.DeleteIncident(incidentID) if err != nil { return err } diff --git a/cmd/incidents/resolve.go b/cmd/incidents/resolve.go index 8a7ec4b..bf955ab 100644 --- a/cmd/incidents/resolve.go +++ b/cmd/incidents/resolve.go @@ -19,8 +19,13 @@ var resolveCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { client := betteruptime.NewClient() - for _, incidentID := range args { - err := client.ResolveIncident(incidentID, resolvedBy) + for _, possibleID := range args { + incidentID, err := betteruptime.IncidentIDFromURL(possibleID) + if err != nil { + return err + } + + err = client.ResolveIncident(incidentID, resolvedBy) if err != nil { return err } diff --git a/cmd/statuspages/resources.go b/cmd/statuspages/resources.go index 535b84f..603a8ac 100644 --- a/cmd/statuspages/resources.go +++ b/cmd/statuspages/resources.go @@ -42,7 +42,7 @@ var getResourcesCmd = &cobra.Command{ if noHeader { table.Print(nil, body) } else { - table.Print([]string{"ID", "Name", "Show History", "Explaination", "Type", "Widget"}, body) + table.Print([]string{"ID", "Name", "Show History", "Explanation", "Type", "Widget"}, body) } return nil }, diff --git a/pkg/betteruptime/incidents.go b/pkg/betteruptime/incidents.go index d5e3e09..7a1a996 100644 --- a/pkg/betteruptime/incidents.go +++ b/pkg/betteruptime/incidents.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "net/http" + "net/url" + "regexp" "time" "github.com/uptime-cli/uptimectl/pkg/contextmanager" @@ -11,8 +13,31 @@ import ( var ( incidentsEndpoint = "/api/v2/incidents" + incidentIDRegex = regexp.MustCompile(`incidents/(\d*)[/]?`) + numericCheckRegex = regexp.MustCompile(`^[0-9]+$`) ) +// 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) { + // Check if passed-in incidentStr is entirely numeric, if so, assume it's an incidentID and return it + if numericCheckRegex.MatchString(incidentStr) { + return incidentStr, nil + } + + incidentURL, err := url.Parse(incidentStr) + if err != nil { + return "", err + } + + matches := incidentIDRegex.FindStringSubmatch(incidentURL.Path) + if len(matches) != 2 { + return "", fmt.Errorf("invalid incident URL: %s", incidentURL) + } + + return matches[1], nil +} + func (c *client) ListIncidents(showResolved bool, daysInPast int, showMax int) ([]Incident, error) { incidents := []Incident{}