Skip to content

Commit 10a02ba

Browse files
Merge pull request #70 from overmindtech/improve-error-messages
Improve errors when looking for an existing change
2 parents a05a30d + 424cc62 commit 10a02ba

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

cmd/root.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,50 +244,64 @@ func getChangeUuid(ctx context.Context, expectedStatus sdp.ChangeStatus, errNotF
244244
var changeUuid uuid.UUID
245245
var err error
246246

247-
if viper.GetString("uuid") != "" {
248-
changeUuid, err = uuid.Parse(viper.GetString("uuid"))
247+
uuidString := viper.GetString("uuid")
248+
changeUrlString := viper.GetString("change")
249+
ticketLink := viper.GetString("ticket-link")
250+
251+
// If no arguments are specified then return an error
252+
if uuidString == "" && changeUrlString == "" && ticketLink == "" {
253+
return uuid.Nil, errors.New("no change specified; use one of --change, --ticket-link or --uuid")
254+
}
255+
256+
// Check UUID first if more than one is set
257+
if uuidString != "" {
258+
changeUuid, err = uuid.Parse(uuidString)
249259
if err != nil {
250-
return uuid.Nil, fmt.Errorf("invalid --uuid value '%v', error: %w", viper.GetString("uuid"), err)
260+
return uuid.Nil, fmt.Errorf("invalid --uuid value '%v', error: %w", uuidString, err)
251261
}
262+
263+
return changeUuid, nil
252264
}
253265

254-
if viper.GetString("change") != "" {
255-
changeUrl, err := url.ParseRequestURI(viper.GetString("change"))
266+
// Then check for a change URL
267+
if changeUrlString != "" {
268+
changeUrl, err := url.ParseRequestURI(changeUrlString)
256269
if err != nil {
257-
return uuid.Nil, fmt.Errorf("invalid --change value '%v', error: %w", viper.GetString("change"), err)
270+
return uuid.Nil, fmt.Errorf("invalid --change value '%v', error: %w", changeUrlString, err)
258271
}
259272
changeUuid, err = uuid.Parse(path.Base(changeUrl.Path))
260273
if err != nil {
261-
return uuid.Nil, fmt.Errorf("invalid --change value '%v', couldn't parse: %w", viper.GetString("change"), err)
274+
return uuid.Nil, fmt.Errorf("invalid --change value '%v', couldn't parse: %w", changeUrlString, err)
262275
}
276+
277+
return changeUuid, nil
263278
}
264279

265-
if viper.GetString("ticket-link") != "" {
266-
client := AuthenticatedChangesClient(ctx)
280+
// Finally look through all open changes to find one with a matching ticket link
281+
client := AuthenticatedChangesClient(ctx)
267282

268-
var maybeChangeUuid *uuid.UUID
269-
changesList, err := client.ListChangesByStatus(ctx, &connect.Request[sdp.ListChangesByStatusRequest]{
270-
Msg: &sdp.ListChangesByStatusRequest{
271-
Status: expectedStatus,
272-
},
273-
})
274-
if err != nil {
275-
return uuid.Nil, errors.New("failed to searching for existing changes")
276-
}
283+
var maybeChangeUuid *uuid.UUID
284+
changesList, err := client.ListChangesByStatus(ctx, &connect.Request[sdp.ListChangesByStatusRequest]{
285+
Msg: &sdp.ListChangesByStatusRequest{
286+
Status: expectedStatus,
287+
},
288+
})
289+
if err != nil {
290+
return uuid.Nil, fmt.Errorf("failed to searching for existing changes: %w", err)
291+
}
277292

278-
for _, c := range changesList.Msg.Changes {
279-
if c.Properties.TicketLink == viper.GetString("ticket-link") {
280-
maybeChangeUuid = c.Metadata.GetUUIDParsed()
281-
if maybeChangeUuid != nil {
282-
changeUuid = *maybeChangeUuid
283-
break
284-
}
293+
for _, c := range changesList.Msg.Changes {
294+
if c.Properties.TicketLink == ticketLink {
295+
maybeChangeUuid = c.Metadata.GetUUIDParsed()
296+
if maybeChangeUuid != nil {
297+
changeUuid = *maybeChangeUuid
298+
break
285299
}
286300
}
287301
}
288302

289303
if errNotFound && changeUuid == uuid.Nil {
290-
return uuid.Nil, errors.New("no change specified; use one of --change, --ticket-link or --uuid")
304+
return uuid.Nil, fmt.Errorf("no change found with ticket link %v", ticketLink)
291305
}
292306

293307
return changeUuid, nil

0 commit comments

Comments
 (0)