added a new error handling when the display name flag exceeds 48 characters #8356

This commit is contained in:
Mateus Marquezini 2023-11-20 18:45:12 -03:00
parent 06e438b4b4
commit 7af33d090a
2 changed files with 24 additions and 1 deletions

View file

@ -110,7 +110,7 @@ func newCreateCmd(app *App) *cobra.Command {
createCmd.Flags().DurationVar(&opts.idleTimeout, "idle-timeout", 0, "allowed inactivity before codespace is stopped, e.g. \"10m\", \"1h\"")
createCmd.Flags().Var(&opts.retentionPeriod, "retention-period", "allowed time after shutting down before the codespace is automatically deleted (maximum 30 days), e.g. \"1h\", \"72h\"")
createCmd.Flags().StringVar(&opts.devContainerPath, "devcontainer-path", "", "path to the devcontainer.json file to use when creating codespace")
createCmd.Flags().StringVarP(&opts.displayName, "display-name", "d", "", "display name for the codespace")
createCmd.Flags().StringVarP(&opts.displayName, "display-name", "d", "", "display name for the codespace (48 characters or less)")
return createCmd
}
@ -282,6 +282,10 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
}
}
if len(opts.displayName) > 48 { // 48 is the max length of the display name in the API
return fmt.Errorf("error creating codespace: display name should contains 48 characters max or less")
}
createParams := &api.CreateCodespaceParams{
RepositoryID: repository.ID,
Branch: branch,

View file

@ -184,6 +184,25 @@ func TestApp_Create(t *testing.T) {
wantStderr: " ✓ Codespaces usage for this repository is paid for by monalisa\n",
wantErr: fmt.Errorf("error getting machine type: there is no such machine for the repository: %s\nAvailable machines: %v", "MEGA", []string{"GIGA", "TERA"}),
},
{
name: "create codespace with display name more than 48 characters results in error",
fields: fields{
apiClient: apiCreateDefaults(&apiClientMock{
CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) {
return &api.Codespace{
Name: "monalisa-dotfiles-abcd1234",
}, nil
},
}),
},
opts: createOptions{
repo: "monalisa/dotfiles",
machine: "GIGA",
displayName: "this-is-very-long-display-name-with-49-characters",
},
wantStderr: " ✓ Codespaces usage for this repository is paid for by monalisa\n",
wantErr: fmt.Errorf("error creating codespace: display name should contains 48 characters max or less"),
},
{
name: "create codespace with devcontainer path results in selecting the correct machine type",
fields: fields{