Skip to content

Commit

Permalink
submit-plan: add options to upload tf plan and git diff output
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS-ovm committed Nov 23, 2023
1 parent 8d6b266 commit 96fed46
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
46 changes: 45 additions & 1 deletion cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ func changeTitle(arg string) string {
return result
}

func tryLoadText(ctx context.Context, fileName string) string {
if fileName == "" {
return ""
}

bytes, err := os.ReadFile(fileName)
if err != nil {
log.WithContext(ctx).WithError(err).WithField("file", fileName).Warn("Failed to read file")
return ""
}

return strings.TrimSpace(string(bytes))
}

func SubmitPlan(ctx context.Context, files []string, ready chan bool) int {
timeout, err := time.ParseDuration(viper.GetString("timeout"))
if err != nil {
Expand Down Expand Up @@ -554,8 +568,12 @@ func SubmitPlan(ctx context.Context, files []string, ready chan bool) int {
return 1
}

title := changeTitle(viper.GetString("title"))
tfPlanOutput := tryLoadText(ctx, viper.GetString("terraform-plan-output"))
codeChangessOutput := tryLoadText(ctx, viper.GetString("code-changes-diff"))

if changeUuid == uuid.Nil {
title := changeTitle(viper.GetString("title"))
log.WithContext(ctx).WithFields(lf).Debug("Creating a new change")
createResponse, err := client.CreateChange(ctx, &connect.Request[sdp.CreateChangeRequest]{
Msg: &sdp.CreateChangeRequest{
Properties: &sdp.ChangeProperties{
Expand All @@ -564,6 +582,8 @@ func SubmitPlan(ctx context.Context, files []string, ready chan bool) int {
TicketLink: viper.GetString("ticket-link"),
Owner: viper.GetString("owner"),
// CcEmails: viper.GetString("cc-emails"),
RawPlan: tfPlanOutput,
CodeChanges: codeChangessOutput,
},
},
})
Expand All @@ -583,6 +603,27 @@ func SubmitPlan(ctx context.Context, files []string, ready chan bool) int {
log.WithContext(ctx).WithFields(lf).Info("Created a new change")
} else {
lf["change"] = changeUuid
log.WithContext(ctx).WithFields(lf).Debug("Updating an existing change")

_, err := client.UpdateChange(ctx, &connect.Request[sdp.UpdateChangeRequest]{
Msg: &sdp.UpdateChangeRequest{
UUID: changeUuid[:],
Properties: &sdp.ChangeProperties{
Title: title,
Description: viper.GetString("description"),
TicketLink: viper.GetString("ticket-link"),
Owner: viper.GetString("owner"),
// CcEmails: viper.GetString("cc-emails"),
RawPlan: tfPlanOutput,
CodeChanges: codeChangessOutput,
},
},
})
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(lf).Error("Failed to update change")
return 1
}

log.WithContext(ctx).WithFields(lf).Info("Re-using change")
}

Expand Down Expand Up @@ -659,5 +700,8 @@ func init() {
submitPlanCmd.PersistentFlags().String("owner", "", "The owner of this change.")
// submitPlanCmd.PersistentFlags().String("cc-emails", "", "A comma-separated list of emails to keep updated with the status of this change.")

submitPlanCmd.PersistentFlags().String("terraform-plan-output", "", "Filename of cached terraform plan output for this change.")
submitPlanCmd.PersistentFlags().String("code-changes-diff", "", "Fileame of the code diff of this change.")

submitPlanCmd.PersistentFlags().String("timeout", "3m", "How long to wait for responses")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hexops/gotextdiff v1.0.3
github.com/jedib0t/go-pretty/v6 v6.4.9
github.com/mattn/go-isatty v0.0.20
github.com/overmindtech/sdp-go v0.58.0
github.com/overmindtech/sdp-go v0.58.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ github.com/nats-io/nkeys v0.4.6 h1:IzVe95ru2CT6ta874rt9saQRkWfe2nFj1NtvYSLqMzY=
github.com/nats-io/nkeys v0.4.6/go.mod h1:4DxZNzenSVd1cYQoAa8948QY3QDjrHfcfVADymtkpts=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/overmindtech/sdp-go v0.58.0 h1:HtMQ2xxH8bL8YYpuNF/BD0YYVIQfEYzEbFScW9dRJ6s=
github.com/overmindtech/sdp-go v0.58.0/go.mod h1:G01shsRPrtJBMQb2HizIG1BIst7jG8Q6BZ+G8/9YPHM=
github.com/overmindtech/sdp-go v0.58.1 h1:BFCQmJlzCGtpT3TbybpLPR7FtlyabZ/LeeTG63r3wdM=
github.com/overmindtech/sdp-go v0.58.1/go.mod h1:0vmJ52cV7AxTBZ210tyDzhpxaJ1YW2WSjeyMpbXOJA8=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
Expand Down

0 comments on commit 96fed46

Please sign in to comment.