X Tutup
Skip to content

Commit 3544275

Browse files
committed
Implement new API payload for a Codespace
1 parent a033b85 commit 3544275

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
lines changed

internal/codespaces/api/api.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func jsonErrorResponse(b []byte) error {
116116

117117
// Repository represents a GitHub repository.
118118
type Repository struct {
119-
ID int `json:"id"`
119+
ID int `json:"id"`
120+
FullName string `json:"full_name"`
120121
}
121122

122123
// GetRepository returns the repository associated with the given owner and name.
@@ -152,37 +153,27 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error
152153

153154
// Codespace represents a codespace.
154155
type Codespace struct {
155-
Name string `json:"name"`
156-
CreatedAt string `json:"created_at"`
157-
LastUsedAt string `json:"last_used_at"`
158-
State string `json:"state"`
159-
Branch string `json:"branch"`
160-
RepositoryName string `json:"repository_name"`
161-
RepositoryNWO string `json:"repository_nwo"`
162-
OwnerLogin string `json:"owner_login"`
163-
Environment CodespaceEnvironment `json:"environment"`
164-
Connection CodespaceConnection `json:"connection"`
156+
Name string `json:"name"`
157+
CreatedAt string `json:"created_at"`
158+
LastUsedAt string `json:"last_used_at"`
159+
Owner User `json:"owner"`
160+
Repository Repository `json:"repository"`
161+
State string `json:"state"`
162+
GitStatus CodespaceGitStatus `json:"git_status"`
163+
Connection CodespaceConnection `json:"connection"`
165164
}
166165

167-
const CodespaceStateProvisioned = "provisioned"
168-
169-
type CodespaceEnvironment struct {
170-
State string `json:"state"`
171-
GitStatus CodespaceEnvironmentGitStatus `json:"gitStatus"`
172-
}
173-
174-
type CodespaceEnvironmentGitStatus struct {
166+
type CodespaceGitStatus struct {
175167
Ahead int `json:"ahead"`
176168
Behind int `json:"behind"`
177-
Branch string `json:"branch"`
178-
Commit string `json:"commit"`
169+
Ref string `json:"ref"`
179170
HasUnpushedChanges bool `json:"hasUnpushedChanges"`
180171
HasUncommitedChanges bool `json:"hasUncommitedChanges"`
181172
}
182173

183174
const (
184-
// CodespaceEnvironmentStateAvailable is the state for a running codespace environment.
185-
CodespaceEnvironmentStateAvailable = "Available"
175+
// CodespaceStateAvailable is the state for a running codespace environment.
176+
CodespaceStateAvailable = "Available"
186177
)
187178

188179
type CodespaceConnection struct {
@@ -459,7 +450,7 @@ func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams
459450
}
460451

461452
// we continue to poll until the codespace shows as provisioned
462-
if codespace.State != CodespaceStateProvisioned {
453+
if codespace.State != CodespaceStateAvailable {
463454
continue
464455
}
465456

@@ -549,13 +540,13 @@ type getCodespaceRepositoryContentsResponse struct {
549540
}
550541

551542
func (a *API) GetCodespaceRepositoryContents(ctx context.Context, codespace *Codespace, path string) ([]byte, error) {
552-
req, err := http.NewRequest(http.MethodGet, a.githubAPI+"/repos/"+codespace.RepositoryNWO+"/contents/"+path, nil)
543+
req, err := http.NewRequest(http.MethodGet, a.githubAPI+"/repos/"+codespace.Repository.FullName+"/contents/"+path, nil)
553544
if err != nil {
554545
return nil, fmt.Errorf("error creating request: %w", err)
555546
}
556547

557548
q := req.URL.Query()
558-
q.Add("ref", codespace.Branch)
549+
q.Add("ref", codespace.GitStatus.Ref)
559550
req.URL.RawQuery = q.Encode()
560551

561552
a.setHeaders(req)

internal/codespaces/codespaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func connectionReady(codespace *api.Codespace) bool {
2727
codespace.Connection.SessionToken != "" &&
2828
codespace.Connection.RelayEndpoint != "" &&
2929
codespace.Connection.RelaySAS != "" &&
30-
codespace.Environment.State == api.CodespaceEnvironmentStateAvailable
30+
codespace.State == api.CodespaceStateAvailable
3131
}
3232

3333
type apiClient interface {
@@ -39,7 +39,7 @@ type apiClient interface {
3939
// and connects to it using a Live Share session.
4040
func ConnectToLiveshare(ctx context.Context, log logger, sessionLogger liveshareLogger, apiClient apiClient, codespace *api.Codespace) (*liveshare.Session, error) {
4141
var startedCodespace bool
42-
if codespace.Environment.State != api.CodespaceEnvironmentStateAvailable {
42+
if codespace.State != api.CodespaceStateAvailable {
4343
startedCodespace = true
4444
log.Print("Starting your codespace...")
4545
if err := apiClient.StartCodespace(ctx, codespace.Name); err != nil {

pkg/cmd/codespace/common.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,17 @@ type codespace struct {
226226
// If includeGitStatus is true, the branch will include a star if
227227
// the codespace has unsaved changes.
228228
func (c codespace) displayName(includeName, includeGitStatus bool) string {
229-
branch := c.Branch
229+
branch := c.GitStatus.Ref
230230
if includeGitStatus {
231231
branch = c.branchWithGitStatus()
232232
}
233233

234234
if includeName {
235235
return fmt.Sprintf(
236-
"%s: %s [%s]", c.RepositoryNWO, branch, c.Name,
236+
"%s: %s [%s]", c.Repository.FullName, branch, c.Name,
237237
)
238238
}
239-
return c.RepositoryNWO + ": " + branch
239+
return c.Repository.FullName + ": " + branch
240240
}
241241

242242
// gitStatusDirty represents an unsaved changes status.
@@ -246,14 +246,14 @@ const gitStatusDirty = "*"
246246
// if the branch is currently being worked on.
247247
func (c codespace) branchWithGitStatus() string {
248248
if c.hasUnsavedChanges() {
249-
return c.Branch + gitStatusDirty
249+
return c.GitStatus.Ref + gitStatusDirty
250250
}
251251

252-
return c.Branch
252+
return c.GitStatus.Ref
253253
}
254254

255255
// hasUnsavedChanges returns whether the environment has
256256
// unsaved changes.
257257
func (c codespace) hasUnsavedChanges() bool {
258-
return c.Environment.GitStatus.HasUncommitedChanges || c.Environment.GitStatus.HasUnpushedChanges
258+
return c.GitStatus.HasUncommitedChanges || c.GitStatus.HasUnpushedChanges
259259
}

pkg/cmd/codespace/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) (err error) {
8989
if nameFilter != "" && c.Name != nameFilter {
9090
continue
9191
}
92-
if opts.repoFilter != "" && !strings.EqualFold(c.RepositoryNWO, opts.repoFilter) {
92+
if opts.repoFilter != "" && !strings.EqualFold(c.Repository.FullName, opts.repoFilter) {
9393
continue
9494
}
9595
if opts.keepDays > 0 {

pkg/cmd/codespace/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func (a *App) List(ctx context.Context, asJSON bool, limit int) error {
4545
cs := codespace{apiCodespace}
4646
table.Append([]string{
4747
cs.Name,
48-
cs.RepositoryNWO,
48+
cs.Repository.FullName,
4949
cs.branchWithGitStatus(),
50-
cs.Environment.State,
50+
cs.State,
5151
cs.CreatedAt,
5252
})
5353
}

0 commit comments

Comments
 (0)
X Tutup