Skip to content

Commit

Permalink
Merge pull request #2 from ndbeals/incident-id-from-url
Browse files Browse the repository at this point in the history
Parse IncidentID from betteruptime URLs
  • Loading branch information
thojkooi authored Jan 24, 2024
2 parents 2e8d12c + 6558bc1 commit 2a13099
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
11 changes: 8 additions & 3 deletions cmd/incidents/acknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/incidents/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/incidents/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/statuspages/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
25 changes: 25 additions & 0 deletions pkg/betteruptime/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"regexp"
"time"

"github.com/uptime-cli/uptimectl/pkg/contextmanager"
)

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{}

Expand Down

0 comments on commit 2a13099

Please sign in to comment.