@@ -151,13 +151,16 @@ type Codespace struct {
151151 Name string `json:"name"`
152152 CreatedAt string `json:"created_at"`
153153 LastUsedAt string `json:"last_used_at"`
154+ State string `json:"state"`
154155 Branch string `json:"branch"`
155156 RepositoryName string `json:"repository_name"`
156157 RepositoryNWO string `json:"repository_nwo"`
157158 OwnerLogin string `json:"owner_login"`
158159 Environment CodespaceEnvironment `json:"environment"`
159160}
160161
162+ const CodespaceStateProvisioned = "provisioned"
163+
161164type CodespaceEnvironment struct {
162165 State string `json:"state"`
163166 Connection CodespaceEnvironmentConnection `json:"connection"`
@@ -220,77 +223,28 @@ func (a *API) ListCodespaces(ctx context.Context) ([]*Codespace, error) {
220223 return response .Codespaces , nil
221224}
222225
223- // getCodespaceTokenRequest is the request body for the get codespace token endpoint.
224- type getCodespaceTokenRequest struct {
225- MintRepositoryToken bool `json:"mint_repository_token"`
226- }
227-
228- type getCodespaceTokenResponse struct {
229- RepositoryToken string `json:"repository_token"`
230- }
231-
232- // ErrNotProvisioned is returned by GetCodespacesToken to indicate that the
233- // creation of a codespace is not yet complete and that the caller should try again.
234- var ErrNotProvisioned = errors .New ("codespace not provisioned" )
235-
236- // GetCodespaceToken returns a codespace token for the user.
237- func (a * API ) GetCodespaceToken (ctx context.Context , ownerLogin , codespaceName string ) (string , error ) {
238- reqBody , err := json .Marshal (getCodespaceTokenRequest {true })
239- if err != nil {
240- return "" , fmt .Errorf ("error preparing request body: %w" , err )
241- }
242-
243- req , err := http .NewRequest (
244- http .MethodPost ,
245- a .githubAPI + "/vscs_internal/user/" + ownerLogin + "/codespaces/" + codespaceName + "/token" ,
246- bytes .NewBuffer (reqBody ),
247- )
248- if err != nil {
249- return "" , fmt .Errorf ("error creating request: %w" , err )
250- }
251-
252- a .setHeaders (req )
253- resp , err := a .do (ctx , req , "/vscs_internal/user/*/codespaces/*/token" )
254- if err != nil {
255- return "" , fmt .Errorf ("error making request: %w" , err )
256- }
257- defer resp .Body .Close ()
258-
259- b , err := ioutil .ReadAll (resp .Body )
260- if err != nil {
261- return "" , fmt .Errorf ("error reading response body: %w" , err )
262- }
263-
264- if resp .StatusCode != http .StatusOK {
265- if resp .StatusCode == http .StatusUnprocessableEntity {
266- return "" , ErrNotProvisioned
267- }
268-
269- return "" , jsonErrorResponse (b )
270- }
271-
272- var response getCodespaceTokenResponse
273- if err := json .Unmarshal (b , & response ); err != nil {
274- return "" , fmt .Errorf ("error unmarshaling response: %w" , err )
275- }
276-
277- return response .RepositoryToken , nil
278- }
279-
280- // GetCodespace returns a codespace for the user.
281- func (a * API ) GetCodespace (ctx context.Context , token , owner , codespace string ) (* Codespace , error ) {
226+ // GetCodespace returns the user codespace based on the provided name.
227+ // If the codespace is not found, an error is returned.
228+ // If includeConnection is true, it will return the connection information for the codespace.
229+ func (a * API ) GetCodespace (ctx context.Context , codespaceName string , includeConnection bool ) (* Codespace , error ) {
282230 req , err := http .NewRequest (
283231 http .MethodGet ,
284- a .githubAPI + "/vscs_internal/ user/" + owner + "/ codespaces/"+ codespace ,
232+ a .githubAPI + "/user/codespaces/" + codespaceName ,
285233 nil ,
286234 )
287235 if err != nil {
288236 return nil , fmt .Errorf ("error creating request: %w" , err )
289237 }
290238
291- // TODO: use a.setHeaders()
292- req .Header .Set ("Authorization" , "Bearer " + token )
293- resp , err := a .do (ctx , req , "/vscs_internal/user/*/codespaces/*" )
239+ if includeConnection {
240+ q := req .URL .Query ()
241+ q .Add ("internal" , "true" )
242+ q .Add ("refresh" , "true" )
243+ req .URL .RawQuery = q .Encode ()
244+ }
245+
246+ a .setHeaders (req )
247+ resp , err := a .do (ctx , req , "/user/codespaces/*" )
294248 if err != nil {
295249 return nil , fmt .Errorf ("error making request: %w" , err )
296250 }
@@ -437,7 +391,6 @@ func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, loc
437391
438392// CreateCodespaceParams are the required parameters for provisioning a Codespace.
439393type CreateCodespaceParams struct {
440- User string
441394 RepositoryID int
442395 Branch , Machine , Location string
443396}
@@ -464,19 +417,14 @@ func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams
464417 case <- ctx .Done ():
465418 return nil , ctx .Err ()
466419 case <- ticker .C :
467- token , err : = a .GetCodespaceToken (ctx , params . User , codespace .Name )
420+ codespace , err = a .GetCodespace (ctx , codespace .Name , false )
468421 if err != nil {
469- if err == ErrNotProvisioned {
470- // Do nothing. We expect this to fail until the codespace is provisioned
471- continue
472- }
473-
474- return nil , fmt .Errorf ("failed to get codespace token: %w" , err )
422+ return nil , fmt .Errorf ("failed to get codespace: %w" , err )
475423 }
476424
477- codespace , err = a . GetCodespace ( ctx , token , params . User , codespace . Name )
478- if err != nil {
479- return nil , fmt . Errorf ( "failed to get codespace: %w" , err )
425+ // we continue to poll until the codespace shows as provisioned
426+ if codespace . State != CodespaceStateProvisioned {
427+ continue
480428 }
481429
482430 return codespace , nil
0 commit comments