Merge pull request #8361 from mateusmarquezini/fix/issue_8356

Added a new error handling when the display name flag exceeds 48 characters
This commit is contained in:
David Gardiner 2023-11-28 12:03:32 -08:00 committed by GitHub
commit 36bbcdd663
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View file

@ -24,6 +24,10 @@ const (
permissionsPollingTimeout = 1 * time.Minute
)
const (
displayNameMaxLength = 48 // 48 is the max length of the display name in the API
)
var (
DEFAULT_DEVCONTAINER_DEFINITIONS = []string{".devcontainer.json", ".devcontainer/devcontainer.json"}
)
@ -110,7 +114,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", "", fmt.Sprintf("display name for the codespace (%d characters or less)", displayNameMaxLength))
return createCmd
}
@ -282,6 +286,10 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
}
}
if len(opts.displayName) > displayNameMaxLength {
return fmt.Errorf("error creating codespace: display name should contain a maximum of %d characters", displayNameMaxLength)
}
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 contain a maximum of %d characters", displayNameMaxLength),
},
{
name: "create codespace with devcontainer path results in selecting the correct machine type",
fields: fields{