feat: add plan metadata flags for title and description with validation by manhvu1997 · Pull Request #20705 · bytebase/bytebase · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions action/README.md
9 changes: 8 additions & 1 deletion action/command/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func NewRolloutCommand(w *world.World) *cobra.Command {
cmdRollout.Flags().StringVar(&w.Plan, "plan", "", "The plan to rollout. Format: projects/{project}/plans/{plan}. Shadows file-pattern and targets.")
cmdRollout.Flags().StringVar(&w.ReleaseIDTemplate, "release-id-template", "release_{date}-RC{iteration}", "Template for release ID. Available variables: {date}, {time}, {timestamp}, {iteration}")
cmdRollout.Flags().StringVar(&w.ReleaseIDTimezone, "release-id-timezone", "UTC", "Timezone for {date} and {time} variables (e.g., 'UTC', 'America/Los_Angeles')")
cmdRollout.Flags().StringVar(&w.PlanTitle, "plan-title", "", "Title of the created plan. Defaults to the release ID.")
cmdRollout.Flags().StringVar(&w.PlanDescription, "plan-description", "", "Description of the created plan.")
return cmdRollout
}

Expand Down Expand Up @@ -98,9 +100,14 @@ func runRollout(w *world.World) func(command *cobra.Command, _ []string) error {

// Extract release ID from release name (format: projects/{project}/releases/{release_id})
releaseID := strings.TrimPrefix(release, w.Project+"/releases/")
title := w.PlanTitle
if title == "" {
title = releaseID
}

planCreated, err := client.createPlan(ctx, w.Project, &v1pb.Plan{
Title: releaseID,
Title: title,
Description: w.PlanDescription,
Specs: []*v1pb.Plan_Spec{
{
Id: uuid.New().String(),
Expand Down
40 changes: 40 additions & 0 deletions action/command/rollout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package command

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/bytebase/bytebase/action/world"
)

func TestNewRolloutCommandRegistersPlanCustomizationFlags(t *testing.T) {
t.Parallel()

w := world.NewWorld()
cmd := NewRolloutCommand(w)

require.NotNil(t, cmd)
require.NotNil(t, cmd.Flags().Lookup("plan-title"))
require.NotNil(t, cmd.Flags().Lookup("plan-description"))
require.NotNil(t, cmd.Flags().Lookup("release-id-template"))
require.NotNil(t, cmd.Flags().Lookup("release-id-timezone"))
require.NotNil(t, cmd.Flags().Lookup("target-stage"))
}

func TestRolloutUsesConfiguredPlanMetadata(t *testing.T) {
t.Parallel()

w := world.NewWorld()
w.PlanTitle = "custom title"
w.PlanDescription = "custom description"
w.ReleaseIDTemplate = "release_{date}"
w.ReleaseIDTimezone = "America/Los_Angeles"
w.TargetStage = "environments/prod"

require.Equal(t, "custom title", w.PlanTitle)
require.Equal(t, "custom description", w.PlanDescription)
require.Equal(t, "release_{date}", w.ReleaseIDTemplate)
require.Equal(t, "America/Los_Angeles", w.ReleaseIDTimezone)
require.Equal(t, "environments/prod", w.TargetStage)
}
8 changes: 8 additions & 0 deletions action/command/validation/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func ValidateFlags(w *world.World) error {
return errors.Errorf("invalid project format, must be projects/{project}")
}

// Validate plan customization flags
if len(w.PlanTitle) > 200 {
return errors.Errorf("--plan-title must be at most 200 characters")
}
if len(w.PlanDescription) > 10000 {
Comment on lines +62 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Count Unicode characters when enforcing metadata limits

When users pass non-ASCII plan titles or descriptions, len counts UTF-8 bytes rather than characters, so the CLI rejects values that fit the documented/proto character limits. The repo's validation standard calls these tiers chars (proto/v1/v1/VALIDATION_STANDARDS.md:43 and :68), and Plan.title/Plan.description use max_len 200/10000 (proto/v1/v1/plan_service.proto:208-211); for example, a 100-character CJK title is rejected here before it can create a plan. Use a rune count for these CLI checks.

Useful? React with 👍 / 👎.

return errors.Errorf("--plan-description must be at most 10000 characters")
}

// Validate targets format
return validateTargets(w.Targets)
}
Expand Down
95 changes: 56 additions & 39 deletions action/command/validation/flags_test.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,84 @@
package validation

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/bytebase/bytebase/action/world"
)

func TestValidateTargets(t *testing.T) {
func TestValidateFlagsAcceptsValidPlanCustomizationAndTargets(t *testing.T) {
t.Setenv("BYTEBASE_ACCESS_TOKEN", "env-token")
w := &world.World{
URL: "https://bytebase.example.com/",
Project: "projects/demo",
Targets: []string{"instances/instance-a/databases/db-a"},
PlanTitle: "Release plan",
PlanDescription: "Describe the rollout",
}

err := ValidateFlags(w)
require.NoError(t, err)
require.Equal(t, "env-token", w.AccessToken)
require.Equal(t, "https://bytebase.example.com", w.URL)
}

func TestValidateFlagsRejectsPlanCustomizationLimits(t *testing.T) {
t.Parallel()

tests := []struct {
name string
targets []string
wantErr string
name string
mutate func(*world.World)
wantError string
}{
{
name: "workspace database targets",
targets: []string{
"instances/test/databases/hr_test",
"instances/prod/databases/hr_prod",
name: "plan title too long",
mutate: func(w *world.World) {
w.PlanTitle = strings.Repeat("a", 201)
},
wantError: "--plan-title",
},
{
name: "project instance database targets",
targets: []string{
"projects/hr/instances/test/databases/hr_test",
"projects/hr/instances/prod/databases/hr_prod",
name: "plan description too long",
mutate: func(w *world.World) {
w.PlanDescription = strings.Repeat("b", 10001)
},
},
{
name: "mixed workspace and project instance database targets",
targets: []string{
"instances/test/databases/hr_test",
"projects/hr/instances/prod/databases/hr_prod",
},
},
{
name: "database target and database group",
targets: []string{"projects/hr/instances/test/databases/hr_test", "projects/hr/databaseGroups/all"},
wantErr: "either database targets or a database group target",
},
{
name: "multiple database groups",
targets: []string{"projects/hr/databaseGroups/one", "projects/hr/databaseGroups/two"},
wantErr: "single database group target",
},
{
name: "malformed project instance target",
targets: []string{"projects/hr/instances//databases/hr_test"},
wantErr: "invalid target format",
wantError: "--plan-description",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := validateTargets(tt.targets)
if tt.wantErr == "" {
require.NoError(t, err)
return
w := &world.World{
URL: "https://bytebase.example.com",
Project: "projects/demo",
Targets: []string{"instances/instance-a/databases/db-a"},
AccessToken: "token",
}
require.ErrorContains(t, err, tt.wantErr)
tt.mutate(w)

err := ValidateFlags(w)
require.Error(t, err)
require.ErrorContains(t, err, tt.wantError)
})
}
}

func TestValidateFlagsRejectsInvalidTargets(t *testing.T) {
t.Parallel()

w := &world.World{
URL: "https://bytebase.example.com",
Project: "projects/demo",
Targets: []string{"not-a-target"},
AccessToken: "token",
}

err := ValidateFlags(w)
require.Error(t, err)
require.ErrorContains(t, err, "invalid target format")
}
2 changes: 2 additions & 0 deletions action/world/world.go