From b87457381f3dd3949c626c06c80e8fd5f44c8e7e Mon Sep 17 00:00:00 2001 From: Dylan Ratcliffe Date: Fri, 8 Sep 2023 08:58:13 +0000 Subject: [PATCH 1/2] Improve errors when looking for an existing change --- cmd/root.go | 68 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5b0fa11a..bc32578c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -244,50 +244,64 @@ func getChangeUuid(ctx context.Context, expectedStatus sdp.ChangeStatus, errNotF var changeUuid uuid.UUID var err error - if viper.GetString("uuid") != "" { - changeUuid, err = uuid.Parse(viper.GetString("uuid")) + uuidString := viper.GetString("uuid") + changeUrlString := viper.GetString("change") + ticketLink := viper.GetString("ticket-link") + + // If no arguments are specified then return an error + if uuidString == "" && changeUrlString == "" && ticketLink == "" { + return uuid.Nil, errors.New("no change specified; use one of --change, --ticket-link or --uuid") + } + + // Check UUID first if more than one is set + if uuidString != "" { + changeUuid, err = uuid.Parse(uuidString) if err != nil { - return uuid.Nil, fmt.Errorf("invalid --uuid value '%v', error: %w", viper.GetString("uuid"), err) + return uuid.Nil, fmt.Errorf("invalid --uuid value '%v', error: %w", uuidString, err) } + + return changeUuid, nil } - if viper.GetString("change") != "" { - changeUrl, err := url.ParseRequestURI(viper.GetString("change")) + // Then check for a change URL + if changeUrlString != "" { + changeUrl, err := url.ParseRequestURI(changeUrlString) if err != nil { - return uuid.Nil, fmt.Errorf("invalid --change value '%v', error: %w", viper.GetString("change"), err) + return uuid.Nil, fmt.Errorf("invalid --change value '%v', error: %w", changeUrlString, err) } changeUuid, err = uuid.Parse(path.Base(changeUrl.Path)) if err != nil { - return uuid.Nil, fmt.Errorf("invalid --change value '%v', couldn't parse: %w", viper.GetString("change"), err) + return uuid.Nil, fmt.Errorf("invalid --change value '%v', couldn't parse: %w", changeUrlString, err) } + + return changeUuid, nil } - if viper.GetString("ticket-link") != "" { - client := AuthenticatedChangesClient(ctx) + // Finally look through all open changes to find one with a matching ticket link + client := AuthenticatedChangesClient(ctx) - var maybeChangeUuid *uuid.UUID - changesList, err := client.ListChangesByStatus(ctx, &connect.Request[sdp.ListChangesByStatusRequest]{ - Msg: &sdp.ListChangesByStatusRequest{ - Status: expectedStatus, - }, - }) - if err != nil { - return uuid.Nil, errors.New("failed to searching for existing changes") - } + var maybeChangeUuid *uuid.UUID + changesList, err := client.ListChangesByStatus(ctx, &connect.Request[sdp.ListChangesByStatusRequest]{ + Msg: &sdp.ListChangesByStatusRequest{ + Status: expectedStatus, + }, + }) + if err != nil { + return uuid.Nil, fmt.Errorf("failed to searching for existing changes: %w", err) + } - for _, c := range changesList.Msg.Changes { - if c.Properties.TicketLink == viper.GetString("ticket-link") { - maybeChangeUuid = c.Metadata.GetUUIDParsed() - if maybeChangeUuid != nil { - changeUuid = *maybeChangeUuid - break - } + for _, c := range changesList.Msg.Changes { + if c.Properties.TicketLink == ticketLink { + maybeChangeUuid = c.Metadata.GetUUIDParsed() + if maybeChangeUuid != nil { + changeUuid = *maybeChangeUuid + break } } } - if errNotFound && changeUuid == uuid.Nil { - return uuid.Nil, errors.New("no change specified; use one of --change, --ticket-link or --uuid") + if changeUuid == uuid.Nil { + return uuid.Nil, fmt.Errorf("no change found with ticket link %v", ticketLink) } return changeUuid, nil From 424cc624c7eb47d13915fab54c87ee688ff61a37 Mon Sep 17 00:00:00 2001 From: Dylan Ratcliffe Date: Fri, 8 Sep 2023 08:59:10 +0000 Subject: [PATCH 2/2] Reimplement error condition --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index bc32578c..0e8c7a69 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -300,7 +300,7 @@ func getChangeUuid(ctx context.Context, expectedStatus sdp.ChangeStatus, errNotF } } - if changeUuid == uuid.Nil { + if errNotFound && changeUuid == uuid.Nil { return uuid.Nil, fmt.Errorf("no change found with ticket link %v", ticketLink) }