Skip to content

Commit

Permalink
(feat) prefer app over frontend flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Sep 9, 2024
1 parent 61c740f commit 4992b4d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
13 changes: 10 additions & 3 deletions cmd/changes_get_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const assetVersion = "17c7fd2c365d4f4cdd8e414ca5148f825fa4febd"
func GetChange(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app, err := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))
if err != nil {
log.Fatalf("Error: %v", err)
}

riskLevels := []sdp.Risk_Severity{}
for _, level := range viper.GetStringSlice("risk-levels") {
switch level {
Expand Down Expand Up @@ -262,9 +267,9 @@ fetch:
SeverityText: "High",
},
}
frontend, _ := strings.CutSuffix(viper.GetString("frontend"), "/")
app, _ = strings.CutSuffix(app, "/")
data := TemplateData{
ChangeUrl: fmt.Sprintf("%v/changes/%v", frontend, changeUuid.String()),
ChangeUrl: fmt.Sprintf("%v/changes/%v", app, changeUuid.String()),
ExpectedChanges: []TemplateItem{},
UnmappedChanges: []TemplateItem{},
BlastItems: int(changeRes.Msg.GetChange().GetMetadata().GetNumAffectedItems()),
Expand Down Expand Up @@ -386,11 +391,13 @@ func renderRiskFilter(levels []sdp.Risk_Severity) string {

func init() {
changesCmd.AddCommand(getChangeCmd)
addAPIFlags(getChangeCmd)

addChangeUuidFlags(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("frontend", "", "The frontend base URL")
_ = submitPlanCmd.PersistentFlags().MarkDeprecated("frontend", "This flag is no longer used and will be removed in a future release. Use the '--app' flag instead.")
getChangeCmd.PersistentFlags().String("format", "json", "How to render the change. Possible values: json, markdown")
getChangeCmd.PersistentFlags().StringSlice("risk-levels", []string{"high", "medium", "low"}, "Only show changes with the specified risk levels. Allowed values: high, medium, low")
}
17 changes: 11 additions & 6 deletions cmd/changes_manual_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var manualChangeCmd = &cobra.Command{
func ManualChange(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app, err := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))
if err != nil {
log.Fatalf("Error: %v", err)
}

method, err := methodFromString(viper.GetString("query-method"))
if err != nil {
return flagError{fmt.Sprintf("can't parse --query-method: %v\n\n%v", err, cmd.UsageString())}
Expand Down Expand Up @@ -156,8 +161,8 @@ func ManualChange(cmd *cobra.Command, args []string) error {
}
}

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

Expand All @@ -183,7 +188,7 @@ func ManualChange(cmd *cobra.Command, args []string) error {
log.WithContext(ctx).WithFields(lf).WithFields(log.Fields{
"change-url": changeUrl,
"app": appUuid,
"app-url": fmt.Sprintf("%v/apps/%v", frontend, appUuid),
"app-url": fmt.Sprintf("%v/apps/%v", app, appUuid),
}).Info("affected app")
}

Expand All @@ -192,9 +197,9 @@ func ManualChange(cmd *cobra.Command, args []string) error {

func init() {
changesCmd.AddCommand(manualChangeCmd)

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

addAPIFlags(manualChangeCmd)
manualChangeCmd.PersistentFlags().String("frontend", "", "The frontend base URL")
_ = submitPlanCmd.PersistentFlags().MarkDeprecated("frontend", "This flag is no longer used and will be removed in a future release. Use the '--app' flag instead.")
manualChangeCmd.PersistentFlags().String("title", "", "Short title for this change.")
manualChangeCmd.PersistentFlags().String("description", "", "Quick description of the change.")
manualChangeCmd.PersistentFlags().String("ticket-link", "*", "Link to the ticket for this change.")
Expand Down
17 changes: 12 additions & 5 deletions cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func tryLoadText(ctx context.Context, fileName string) string {
func SubmitPlan(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app, err := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))
if err != nil {
log.Fatalf("Error: %v", err)
}

ctx, oi, _, err := login(ctx, cmd, []string{"changes:write"})
if err != nil {
return err
Expand Down Expand Up @@ -298,8 +303,8 @@ func SubmitPlan(cmd *cobra.Command, args []string) error {
}
}

frontend, _ := strings.CutSuffix(viper.GetString("frontend"), "/")
changeUrl := fmt.Sprintf("%v/changes/%v/blast-radius", frontend, changeUuid)
app, _ = strings.CutSuffix(app, "/")
changeUrl := fmt.Sprintf("%v/changes/%v/blast-radius", app, changeUuid)
log.WithContext(ctx).WithFields(lf).WithField("change-url", changeUrl).Info("Change ready")
fmt.Println(changeUrl)

Expand All @@ -326,7 +331,7 @@ func SubmitPlan(cmd *cobra.Command, args []string) error {
log.WithContext(ctx).WithFields(lf).WithFields(log.Fields{
"change-url": changeUrl,
"app": appUuid,
"app-url": fmt.Sprintf("%v/apps/%v", frontend, appUuid),
"app-url": fmt.Sprintf("%v/apps/%v", app, appUuid),
}).Info("Affected app")
}

Expand All @@ -336,7 +341,9 @@ func SubmitPlan(cmd *cobra.Command, args []string) error {
func init() {
changesCmd.AddCommand(submitPlanCmd)

submitPlanCmd.PersistentFlags().String("frontend", "https://app.overmind.tech", "The frontend base URL")
addAPIFlags(submitPlanCmd)
submitPlanCmd.PersistentFlags().String("frontend", "", "The frontend base URL")
_ = submitPlanCmd.PersistentFlags().MarkDeprecated("frontend", "This flag is no longer used and will be removed in a future release. Use the '--app' flag instead.")

submitPlanCmd.PersistentFlags().String("title", "", "Short title for this change. If this is not specified, overmind will try to come up with one for you.")
submitPlanCmd.PersistentFlags().String("description", "", "Quick description of the change.")
Expand All @@ -345,7 +352,7 @@ func init() {
// 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("code-changes-diff", "", "Filename of the code diff of this change.")
submitPlanCmd.PersistentFlags().Int32("blast-radius-link-depth", 0, "Used in combination with '--blast-radius-max-items' to customise how many levels are traversed when calculating the blast radius. Larger numbers will result in a more comprehensive blast radius, but may take longer to calculate. Defaults to the account level settings.")
submitPlanCmd.PersistentFlags().Int32("blast-radius-max-items", 0, "Used in combination with '--blast-radius-link-depth' to customise how many items are included in the blast radius. Larger numbers will result in a more comprehensive blast radius, but may take longer to calculate. Defaults to the account level settings.")
}
16 changes: 16 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,19 @@ func login(ctx context.Context, cmd *cobra.Command, scopes []string) (context.Co

return ctx, oi, token, nil
}

func getAppUrl(frontend, app string) (string, error) {
if frontend == "" && app == "" {
return "", fmt.Errorf("'--app' flag must be set")
}
if frontend != "" && app != "" {
log.WithError(fmt.Errorf("both '--frontend' and '--app' are set, using --frontend %q. Please only Use '--app'", frontend)).Warn("both '--frontend' and '--app' are set")
return frontend, nil
}
// if frontend is set and app is not set, set app to frontend
if frontend != "" {
return frontend, nil
} else {
return app, nil
}
}
32 changes: 32 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -83,3 +84,34 @@ func TestHasScopesFlexible(t *testing.T) {
})
}
}

func Test_getAppUrl(t *testing.T) {
type args struct {
frontend string
app string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "empty", args: args{frontend: "", app: ""}, want: "", wantErr: true},
{name: "empty app", args: args{frontend: "https://app.overmind.tech/", app: ""}, want: "https://app.overmind.tech/", wantErr: false},
{name: "empty frontend", args: args{frontend: "", app: "https://app.overmind.tech/"}, want: "https://app.overmind.tech/", wantErr: false},
{name: "same", args: args{frontend: "https://app.overmind.tech/", app: "https://app.overmind.tech/"}, want: "https://app.overmind.tech/", wantErr: false},
{name: "different", args: args{frontend: "https://app.overmind.tech/", app: "https://app.overmind.tech/changes/123"}, want: "https://app.overmind.tech/", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getAppUrl(tt.args.frontend, tt.args.app)
if (err != nil) != tt.wantErr {
t.Errorf("getAppUrl() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getAppUrl() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 4992b4d

Please sign in to comment.