Skip to content

Commit

Permalink
Add lookup by status and ticket link to getChangeUuid and extend *-ch…
Browse files Browse the repository at this point in the history
…ange commands to use it
  • Loading branch information
DavidS-ovm committed Aug 3, 2023
1 parent 84c818d commit 7082126
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 70 deletions.
27 changes: 13 additions & 14 deletions cmd/endchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,33 @@ func EndChange(signals chan os.Signal, ready chan bool) int {
return 1
}

changeUuid, err := getChangeUuid()
if err != nil {
log.WithError(err).WithFields(log.Fields{
"url": viper.GetString("url"),
}).Error("failed to identify change")
return 1
}

ctx := context.Background()
ctx, span := tracing.Tracer().Start(ctx, "CLI EndChange", trace.WithAttributes(
attribute.String("om.config", fmt.Sprintf("%v", viper.AllSettings())),
))
defer span.End()

lf := log.Fields{
"uuid": changeUuid.String(),
}

ctx, err = ensureToken(ctx, signals)
if err != nil {
log.WithContext(ctx).WithFields(lf).WithError(err).Error("failed to authenticate")
log.WithContext(ctx).WithFields(log.Fields{

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to apiKey
flows to a logging call.
"url": viper.GetString("url"),
}).WithError(err).Error("failed to authenticate")
return 1
}

// apply a timeout to the main body of processing
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

lf := log.Fields{}
changeUuid, err := getChangeUuid(ctx, sdp.ChangeStatus_CHANGE_STATUS_HAPPENING)
if err != nil {
log.WithError(err).WithFields(lf).Error("failed to identify change")
return 1
}

lf["uuid"] = changeUuid.String()

// snapClient := AuthenticatedSnapshotsClient(ctx)
client := AuthenticatedChangesClient(ctx)
stream, err := client.EndChange(ctx, &connect.Request[sdp.EndChangeRequest]{
Expand Down Expand Up @@ -101,7 +100,7 @@ func EndChange(signals chan os.Signal, ready chan bool) int {
func init() {
rootCmd.AddCommand(endChangeCmd)

withChangeUuid(endChangeCmd)
withChangeUuidFlags(endChangeCmd)

endChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

Expand Down
24 changes: 13 additions & 11 deletions cmd/getchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ func GetChange(signals chan os.Signal, ready chan bool) int {
return 1
}

changeUuid, err := getChangeUuid()
if err != nil {
log.WithError(err).WithFields(log.Fields{
"url": viper.GetString("url"),
}).Error("failed to identify change")
return 1
}

ctx := context.Background()
ctx, span := tracing.Tracer().Start(ctx, "CLI GetChange", trace.WithAttributes(
attribute.String("om.config", fmt.Sprintf("%v", viper.AllSettings())),
Expand All @@ -64,16 +56,25 @@ func GetChange(signals chan os.Signal, ready chan bool) int {

ctx, err = ensureToken(ctx, signals)
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
log.WithContext(ctx).WithFields(log.Fields{

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to apiKey
flows to a logging call.
"url": viper.GetString("url"),
}).Error("failed to authenticate")
}).WithError(err).Error("failed to authenticate")
return 1
}

// apply a timeout to the main body of processing
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

lf := log.Fields{}
changeUuid, err := getChangeUuid(ctx, sdp.ChangeStatus(sdp.ChangeStatus_value[viper.GetString("status")]))
if err != nil {
log.WithError(err).WithFields(lf).Error("failed to identify change")
return 1
}

lf["uuid"] = changeUuid.String()

client := AuthenticatedChangesClient(ctx)
response, err := client.GetChange(ctx, &connect.Request[sdp.GetChangeRequest]{
Msg: &sdp.GetChangeRequest{
Expand Down Expand Up @@ -128,7 +129,8 @@ func GetChange(signals chan os.Signal, ready chan bool) int {
func init() {
rootCmd.AddCommand(getChangeCmd)

withChangeUuid(getChangeCmd)
withChangeUuidFlags(getChangeCmd)
getChangeCmd.PersistentFlags().String("status", "", "The expected status of the change. Use this with --ticket-link. Allowed values: CHANGE_STATUS_UNSPECIFIED, CHANGE_STATUS_DEFINING, CHANGE_STATUS_HAPPENING, CHANGE_STATUS_PROCESSING, CHANGE_STATUS_DONE")

getChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")
getChangeCmd.PersistentFlags().String("format", "json", "How to render the change. Possible values: json, markdown")
Expand Down
38 changes: 31 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func ensureToken(ctx context.Context, signals chan os.Signal) (context.Context,
return ctx, fmt.Errorf("no --api-key configured and target URL (%v) is insecure", parsed)
}

func getChangeUuid() (uuid.UUID, error) {
// getChangeUuid returns the UUID of a change, as selected by --uuid or --change, or a state with the specified status and having --ticket-link
func getChangeUuid(ctx context.Context, expectedStatus sdp.ChangeStatus) (uuid.UUID, error) {
var changeUuid uuid.UUID
var err error

Expand All @@ -206,19 +207,42 @@ func getChangeUuid() (uuid.UUID, error) {
}
}

if changeUuid == uuid.Nil {
return uuid.Nil, errors.New("no change specified; use one of --uuid or --change")
if viper.GetString("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")
}

for _, c := range changesList.Msg.Changes {
if c.Properties.TicketLink == viper.GetString("ticket-link") {
maybeChangeUuid = c.Metadata.GetUUIDParsed()
if maybeChangeUuid != nil {
changeUuid = *maybeChangeUuid
break
}
}
}
}

// if changeUuid == uuid.Nil {
// return uuid.Nil, errors.New("no change specified; use one of --change, --ticket-link or --uuid")
// }

return changeUuid, nil
}

func withChangeUuid(cmd *cobra.Command) {

func withChangeUuidFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String("change", "", "The frontend URL of the change to get")
cmd.PersistentFlags().String("ticket-link", "", "Link to the ticket for this change.")
cmd.PersistentFlags().String("uuid", "", "The UUID of the change that should be displayed.")
cmd.MarkFlagsMutuallyExclusive("change", "uuid")

cmd.MarkFlagsMutuallyExclusive("change", "ticket-link", "uuid")
}

func init() {
Expand Down
27 changes: 13 additions & 14 deletions cmd/startchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,33 @@ func StartChange(signals chan os.Signal, ready chan bool) int {
return 1
}

changeUuid, err := getChangeUuid()
if err != nil {
log.WithError(err).WithFields(log.Fields{
"url": viper.GetString("url"),
}).Error("failed to identify change")
return 1
}

ctx := context.Background()
ctx, span := tracing.Tracer().Start(ctx, "CLI StartChange", trace.WithAttributes(
attribute.String("om.config", fmt.Sprintf("%v", viper.AllSettings())),
))
defer span.End()

lf := log.Fields{
"uuid": changeUuid.String(),
}

ctx, err = ensureToken(ctx, signals)
if err != nil {
log.WithContext(ctx).WithFields(lf).WithError(err).Error("failed to authenticate")
log.WithContext(ctx).WithFields(log.Fields{

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to apiKey
flows to a logging call.
"url": viper.GetString("url"),
}).WithError(err).Error("failed to authenticate")
return 1
}

// apply a timeout to the main body of processing
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

lf := log.Fields{}
changeUuid, err := getChangeUuid(ctx, sdp.ChangeStatus_CHANGE_STATUS_DEFINING)
if err != nil {
log.WithError(err).WithFields(lf).Error("failed to identify change")
return 1
}

lf["uuid"] = changeUuid.String()

// snapClient := AuthenticatedSnapshotsClient(ctx)
client := AuthenticatedChangesClient(ctx)
stream, err := client.StartChange(ctx, &connect.Request[sdp.StartChangeRequest]{
Expand Down Expand Up @@ -101,7 +100,7 @@ func StartChange(signals chan os.Signal, ready chan bool) int {
func init() {
rootCmd.AddCommand(startChangeCmd)

withChangeUuid(startChangeCmd)
withChangeUuidFlags(startChangeCmd)

startChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

Expand Down
34 changes: 10 additions & 24 deletions cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,29 +221,13 @@ func SubmitPlan(signals chan os.Signal, ready chan bool) int {
}

client := AuthenticatedChangesClient(ctx)

var changeUUID *uuid.UUID

changesList, err := client.ListChangesByStatus(ctx, &connect.Request[sdp.ListChangesByStatusRequest]{
Msg: &sdp.ListChangesByStatusRequest{
Status: sdp.ChangeStatus_CHANGE_STATUS_DEFINING,
},
})
changeUuid, err := getChangeUuid(ctx, sdp.ChangeStatus_CHANGE_STATUS_DEFINING)
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(lf).Error("failed to searching for existing changes")
return 1
}

for _, c := range changesList.Msg.Changes {
if c.Properties.TicketLink == viper.GetString("ticket-link") {
changeUUID = c.Metadata.GetUUIDParsed()
if changeUUID != nil {
break
}
}
}

if changeUUID == nil {
if changeUuid == uuid.Nil {
createResponse, err := client.CreateChange(ctx, &connect.Request[sdp.CreateChangeRequest]{
Msg: &sdp.CreateChangeRequest{
Properties: &sdp.ChangeProperties{
Expand All @@ -260,14 +244,16 @@ func SubmitPlan(signals chan os.Signal, ready chan bool) int {
return 1
}

changeUUID = createResponse.Msg.Change.Metadata.GetUUIDParsed()
if changeUUID == nil {
maybeChangeUuid := createResponse.Msg.Change.Metadata.GetUUIDParsed()
if maybeChangeUuid == nil {
log.WithContext(ctx).WithError(err).WithFields(lf).Error("failed to read change id")
return 1
}

changeUuid = *maybeChangeUuid
}

lf["change"] = changeUUID
lf["change"] = changeUuid
log.WithContext(ctx).WithFields(lf).Info("created a new change")

receivedItems := []*sdp.Reference{}
Expand Down Expand Up @@ -449,7 +435,7 @@ func SubmitPlan(signals chan os.Signal, ready chan bool) int {
}
resultStream, err := client.UpdateChangingItems(ctx, &connect.Request[sdp.UpdateChangingItemsRequest]{
Msg: &sdp.UpdateChangingItemsRequest{
ChangeUUID: (*changeUUID)[:],
ChangeUUID: changeUuid[:],
ChangingItems: receivedItems,
},
})
Expand Down Expand Up @@ -478,13 +464,13 @@ func SubmitPlan(signals chan os.Signal, ready chan bool) int {
}
}

changeUrl := fmt.Sprintf("%v/changes/%v", viper.GetString("frontend"), changeUUID)
changeUrl := fmt.Sprintf("%v/changes/%v", viper.GetString("frontend"), changeUuid)
log.WithContext(ctx).WithFields(lf).WithField("change-url", changeUrl).Info("change ready")
fmt.Println(changeUrl)

fetchResponse, err := client.GetChange(ctx, &connect.Request[sdp.GetChangeRequest]{
Msg: &sdp.GetChangeRequest{
UUID: (*changeUUID)[:],
UUID: changeUuid[:],
},
})
if err != nil {
Expand Down

0 comments on commit 7082126

Please sign in to comment.