diff --git a/internal/codespaces/api/api.go b/internal/codespaces/api/api.go index 7ac1cb2fa..a207f562c 100644 --- a/internal/codespaces/api/api.go +++ b/internal/codespaces/api/api.go @@ -161,14 +161,15 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error // Codespace represents a codespace. type Codespace struct { - Name string `json:"name"` - CreatedAt string `json:"created_at"` - LastUsedAt string `json:"last_used_at"` - Owner User `json:"owner"` - Repository Repository `json:"repository"` - State string `json:"state"` - GitStatus CodespaceGitStatus `json:"git_status"` - Connection CodespaceConnection `json:"connection"` + Name string `json:"name"` + CreatedAt string `json:"created_at"` + DisplayName string `json:"display_name"` + LastUsedAt string `json:"last_used_at"` + Owner User `json:"owner"` + Repository Repository `json:"repository"` + State string `json:"state"` + GitStatus CodespaceGitStatus `json:"git_status"` + Connection CodespaceConnection `json:"connection"` } type CodespaceGitStatus struct { @@ -198,6 +199,7 @@ type CodespaceConnection struct { // CodespaceFields is the list of exportable fields for a codespace. var CodespaceFields = []string{ + "displayName", "name", "owner", "repository", diff --git a/pkg/cmd/codespace/common.go b/pkg/cmd/codespace/common.go index 22753ff39..d7273edb8 100644 --- a/pkg/cmd/codespace/common.go +++ b/pkg/cmd/codespace/common.go @@ -250,7 +250,7 @@ type codespace struct { } // displayName returns the repository nwo and branch. -// If includeName is true, the name of the codespace is included. +// If includeName is true, the name of the codespace (including displayName) is included. // If includeGitStatus is true, the branch will include a star if // the codespace has unsaved changes. func (c codespace) displayName(includeName, includeGitStatus bool) string { @@ -260,11 +260,18 @@ func (c codespace) displayName(includeName, includeGitStatus bool) string { } if includeName { + var displayName = c.Name + if c.DisplayName != "" { + displayName = c.DisplayName + } return fmt.Sprintf( - "%s: %s [%s]", c.Repository.FullName, branch, c.Name, + "%s: %s (%s)", c.Repository.FullName, displayName, branch, ) } - return c.Repository.FullName + ": " + branch + return fmt.Sprintf( + "%s: %s", c.Repository.FullName, branch, + ) + } // gitStatusDirty represents an unsaved changes status. diff --git a/pkg/cmd/codespace/common_test.go b/pkg/cmd/codespace/common_test.go new file mode 100644 index 000000000..c5fc01590 --- /dev/null +++ b/pkg/cmd/codespace/common_test.go @@ -0,0 +1,132 @@ +package codespace + +import ( + "testing" + + "github.com/cli/cli/v2/internal/codespaces/api" +) + +func Test_codespace_displayName(t *testing.T) { + type fields struct { + Codespace *api.Codespace + } + type args struct { + includeName bool + includeGitStatus bool + } + tests := []struct { + name string + fields fields + args args + want string + }{ + { + name: "No included name or gitstatus", + fields: fields{ + Codespace: &api.Codespace{ + GitStatus: api.CodespaceGitStatus{ + Ref: "trunk", + }, + Repository: api.Repository{ + FullName: "cli/cli", + }, + DisplayName: "scuba steve", + }, + }, + args: args{ + includeName: false, + includeGitStatus: false, + }, + want: "cli/cli: trunk", + }, + { + name: "No included name - included gitstatus - no unsaved changes", + fields: fields{ + Codespace: &api.Codespace{ + GitStatus: api.CodespaceGitStatus{ + Ref: "trunk", + }, + Repository: api.Repository{ + FullName: "cli/cli", + }, + DisplayName: "scuba steve", + }, + }, + args: args{ + includeName: false, + includeGitStatus: true, + }, + want: "cli/cli: trunk", + }, + { + name: "No included name - included gitstatus - unsaved changes", + fields: fields{ + Codespace: &api.Codespace{ + GitStatus: api.CodespaceGitStatus{ + Ref: "trunk", + HasUncommitedChanges: true, + }, + Repository: api.Repository{ + FullName: "cli/cli", + }, + DisplayName: "scuba steve", + }, + }, + args: args{ + includeName: false, + includeGitStatus: true, + }, + want: "cli/cli: trunk*", + }, + { + name: "Included name - included gitstatus - unsaved changes", + fields: fields{ + Codespace: &api.Codespace{ + GitStatus: api.CodespaceGitStatus{ + Ref: "trunk", + HasUncommitedChanges: true, + }, + Repository: api.Repository{ + FullName: "cli/cli", + }, + DisplayName: "scuba steve", + }, + }, + args: args{ + includeName: true, + includeGitStatus: true, + }, + want: "cli/cli: scuba steve (trunk*)", + }, + { + name: "Included name - included gitstatus - no unsaved changes", + fields: fields{ + Codespace: &api.Codespace{ + GitStatus: api.CodespaceGitStatus{ + Ref: "trunk", + HasUncommitedChanges: false, + }, + Repository: api.Repository{ + FullName: "cli/cli", + }, + DisplayName: "scuba steve", + }, + }, + args: args{ + includeName: true, + includeGitStatus: true, + }, + want: "cli/cli: scuba steve (trunk)", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := codespace{ + Codespace: tt.fields.Codespace, + } + if got := c.displayName(tt.args.includeName, tt.args.includeGitStatus); got != tt.want { + t.Errorf("codespace.displayName() = %v, want %v", got, tt.want) + } + }) + } +}