Skip to content

Commit

Permalink
Merge pull request #553 from overmindtech/add_app_flag
Browse files Browse the repository at this point in the history
(feat) prefer app over frontend flag.
  • Loading branch information
tphoney authored Sep 9, 2024
2 parents 61c740f + 92402c5 commit 504a2d8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
10 changes: 7 additions & 3 deletions cmd/changes_get_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const assetVersion = "17c7fd2c365d4f4cdd8e414ca5148f825fa4febd"
func GetChange(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))

riskLevels := []sdp.Risk_Severity{}
for _, level := range viper.GetStringSlice("risk-levels") {
switch level {
Expand Down Expand Up @@ -262,9 +264,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 +388,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.") // MarkDeprecated only errors if the flag doesn't exist, we fall back to using app
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")
}
14 changes: 8 additions & 6 deletions cmd/changes_manual_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var manualChangeCmd = &cobra.Command{
func ManualChange(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))

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 +158,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 +185,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 +194,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.") // MarkDeprecated only errors if the flag doesn't exist, we fall back to using app
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
15 changes: 9 additions & 6 deletions cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func tryLoadText(ctx context.Context, fileName string) string {
func SubmitPlan(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

app := getAppUrl(viper.GetString("frontend"), viper.GetString("app"))

ctx, oi, _, err := login(ctx, cmd, []string{"changes:write"})
if err != nil {
return err
Expand Down Expand Up @@ -298,8 +300,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 +328,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,16 +338,17 @@ 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.") // MarkDeprecated only errors if the flag doesn't exist, we fall back to using app
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.")
submitPlanCmd.PersistentFlags().String("ticket-link", "*", "Link to the ticket for this change. Usually this would be the link to something like the pull request, since the CLI uses this as a unique identifier for the change, meaning that multiple runs with the same ticket link will update the same change.")
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("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.")
}
13 changes: 13 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,16 @@ func login(ctx context.Context, cmd *cobra.Command, scopes []string) (context.Co

return ctx, oi, token, nil
}

func getAppUrl(frontend, app string) string {
if frontend == "" && app == "" {
return "https://app.overmind.tech"
}
if frontend != "" && app == "" {
return frontend
}
if frontend != "" && app != "" {
log.Warnf("Both --frontend and --app are set, but they are different. Using --app: %v", app)
}
return app
}
27 changes: 27 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,29 @@ 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
}{
{name: "empty", args: args{frontend: "", app: ""}, want: "https://app.overmind.tech"},
{name: "empty app", args: args{frontend: "https://app.overmind.tech", app: ""}, want: "https://app.overmind.tech"},
{name: "empty frontend", args: args{frontend: "", app: "https://app.overmind.tech"}, want: "https://app.overmind.tech"},
{name: "same", args: args{frontend: "https://app.overmind.tech", app: "https://app.overmind.tech"}, want: "https://app.overmind.tech"},
{name: "different", args: args{frontend: "https://app.overmind.tech", app: "https://app.overmind.tech/changes/123"}, want: "https://app.overmind.tech/changes/123"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getAppUrl(tt.args.frontend, tt.args.app)
if got != tt.want {
t.Errorf("getAppUrl() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 504a2d8

Please sign in to comment.