Skip to content

Commit f6f8ba8

Browse files
authored
Add display name for codespaces (cli#5044)
1 parent c3d451b commit f6f8ba8

File tree

3 files changed

+152
-11
lines changed

3 files changed

+152
-11
lines changed

internal/codespaces/api/api.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,15 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error
161161

162162
// Codespace represents a codespace.
163163
type Codespace struct {
164-
Name string `json:"name"`
165-
CreatedAt string `json:"created_at"`
166-
LastUsedAt string `json:"last_used_at"`
167-
Owner User `json:"owner"`
168-
Repository Repository `json:"repository"`
169-
State string `json:"state"`
170-
GitStatus CodespaceGitStatus `json:"git_status"`
171-
Connection CodespaceConnection `json:"connection"`
164+
Name string `json:"name"`
165+
CreatedAt string `json:"created_at"`
166+
DisplayName string `json:"display_name"`
167+
LastUsedAt string `json:"last_used_at"`
168+
Owner User `json:"owner"`
169+
Repository Repository `json:"repository"`
170+
State string `json:"state"`
171+
GitStatus CodespaceGitStatus `json:"git_status"`
172+
Connection CodespaceConnection `json:"connection"`
172173
}
173174

174175
type CodespaceGitStatus struct {
@@ -198,6 +199,7 @@ type CodespaceConnection struct {
198199

199200
// CodespaceFields is the list of exportable fields for a codespace.
200201
var CodespaceFields = []string{
202+
"displayName",
201203
"name",
202204
"owner",
203205
"repository",

pkg/cmd/codespace/common.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ type codespace struct {
250250
}
251251

252252
// displayName returns the repository nwo and branch.
253-
// If includeName is true, the name of the codespace is included.
253+
// If includeName is true, the name of the codespace (including displayName) is included.
254254
// If includeGitStatus is true, the branch will include a star if
255255
// the codespace has unsaved changes.
256256
func (c codespace) displayName(includeName, includeGitStatus bool) string {
@@ -260,11 +260,18 @@ func (c codespace) displayName(includeName, includeGitStatus bool) string {
260260
}
261261

262262
if includeName {
263+
var displayName = c.Name
264+
if c.DisplayName != "" {
265+
displayName = c.DisplayName
266+
}
263267
return fmt.Sprintf(
264-
"%s: %s [%s]", c.Repository.FullName, branch, c.Name,
268+
"%s: %s (%s)", c.Repository.FullName, displayName, branch,
265269
)
266270
}
267-
return c.Repository.FullName + ": " + branch
271+
return fmt.Sprintf(
272+
"%s: %s", c.Repository.FullName, branch,
273+
)
274+
268275
}
269276

270277
// gitStatusDirty represents an unsaved changes status.

pkg/cmd/codespace/common_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package codespace
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cli/cli/v2/internal/codespaces/api"
7+
)
8+
9+
func Test_codespace_displayName(t *testing.T) {
10+
type fields struct {
11+
Codespace *api.Codespace
12+
}
13+
type args struct {
14+
includeName bool
15+
includeGitStatus bool
16+
}
17+
tests := []struct {
18+
name string
19+
fields fields
20+
args args
21+
want string
22+
}{
23+
{
24+
name: "No included name or gitstatus",
25+
fields: fields{
26+
Codespace: &api.Codespace{
27+
GitStatus: api.CodespaceGitStatus{
28+
Ref: "trunk",
29+
},
30+
Repository: api.Repository{
31+
FullName: "cli/cli",
32+
},
33+
DisplayName: "scuba steve",
34+
},
35+
},
36+
args: args{
37+
includeName: false,
38+
includeGitStatus: false,
39+
},
40+
want: "cli/cli: trunk",
41+
},
42+
{
43+
name: "No included name - included gitstatus - no unsaved changes",
44+
fields: fields{
45+
Codespace: &api.Codespace{
46+
GitStatus: api.CodespaceGitStatus{
47+
Ref: "trunk",
48+
},
49+
Repository: api.Repository{
50+
FullName: "cli/cli",
51+
},
52+
DisplayName: "scuba steve",
53+
},
54+
},
55+
args: args{
56+
includeName: false,
57+
includeGitStatus: true,
58+
},
59+
want: "cli/cli: trunk",
60+
},
61+
{
62+
name: "No included name - included gitstatus - unsaved changes",
63+
fields: fields{
64+
Codespace: &api.Codespace{
65+
GitStatus: api.CodespaceGitStatus{
66+
Ref: "trunk",
67+
HasUncommitedChanges: true,
68+
},
69+
Repository: api.Repository{
70+
FullName: "cli/cli",
71+
},
72+
DisplayName: "scuba steve",
73+
},
74+
},
75+
args: args{
76+
includeName: false,
77+
includeGitStatus: true,
78+
},
79+
want: "cli/cli: trunk*",
80+
},
81+
{
82+
name: "Included name - included gitstatus - unsaved changes",
83+
fields: fields{
84+
Codespace: &api.Codespace{
85+
GitStatus: api.CodespaceGitStatus{
86+
Ref: "trunk",
87+
HasUncommitedChanges: true,
88+
},
89+
Repository: api.Repository{
90+
FullName: "cli/cli",
91+
},
92+
DisplayName: "scuba steve",
93+
},
94+
},
95+
args: args{
96+
includeName: true,
97+
includeGitStatus: true,
98+
},
99+
want: "cli/cli: scuba steve (trunk*)",
100+
},
101+
{
102+
name: "Included name - included gitstatus - no unsaved changes",
103+
fields: fields{
104+
Codespace: &api.Codespace{
105+
GitStatus: api.CodespaceGitStatus{
106+
Ref: "trunk",
107+
HasUncommitedChanges: false,
108+
},
109+
Repository: api.Repository{
110+
FullName: "cli/cli",
111+
},
112+
DisplayName: "scuba steve",
113+
},
114+
},
115+
args: args{
116+
includeName: true,
117+
includeGitStatus: true,
118+
},
119+
want: "cli/cli: scuba steve (trunk)",
120+
},
121+
}
122+
for _, tt := range tests {
123+
t.Run(tt.name, func(t *testing.T) {
124+
c := codespace{
125+
Codespace: tt.fields.Codespace,
126+
}
127+
if got := c.displayName(tt.args.includeName, tt.args.includeGitStatus); got != tt.want {
128+
t.Errorf("codespace.displayName() = %v, want %v", got, tt.want)
129+
}
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)