Add display name for codespaces (#5044)

This commit is contained in:
Patrick Veverka 2022-02-09 10:15:03 -05:00 committed by GitHub
parent c3d451b9b6
commit f6f8ba8b7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 152 additions and 11 deletions

View file

@ -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",

View file

@ -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.

View file

@ -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)
}
})
}
}