@@ -180,77 +180,28 @@ func (a *API) ListCodespaces(ctx context.Context) ([]*codespace.Codespace, error
180180 return response .Codespaces , nil
181181}
182182
183- // getCodespaceTokenRequest is the request body for the get codespace token endpoint.
184- type getCodespaceTokenRequest struct {
185- MintRepositoryToken bool `json:"mint_repository_token"`
186- }
187-
188- type getCodespaceTokenResponse struct {
189- RepositoryToken string `json:"repository_token"`
190- }
191-
192- // ErrNotProvisioned is returned by GetCodespacesToken to indicate that the
193- // creation of a codespace is not yet complete and that the caller should try again.
194- var ErrNotProvisioned = errors .New ("codespace not provisioned" )
195-
196- // GetCodespaceToken returns a codespace token for the user.
197- func (a * API ) GetCodespaceToken (ctx context.Context , ownerLogin , codespaceName string ) (string , error ) {
198- reqBody , err := json .Marshal (getCodespaceTokenRequest {true })
199- if err != nil {
200- return "" , fmt .Errorf ("error preparing request body: %w" , err )
201- }
202-
203- req , err := http .NewRequest (
204- http .MethodPost ,
205- a .githubAPI + "/vscs_internal/user/" + ownerLogin + "/codespaces/" + codespaceName + "/token" ,
206- bytes .NewBuffer (reqBody ),
207- )
208- if err != nil {
209- return "" , fmt .Errorf ("error creating request: %w" , err )
210- }
211-
212- a .setHeaders (req )
213- resp , err := a .do (ctx , req , "/vscs_internal/user/*/codespaces/*/token" )
214- if err != nil {
215- return "" , fmt .Errorf ("error making request: %w" , err )
216- }
217- defer resp .Body .Close ()
218-
219- b , err := ioutil .ReadAll (resp .Body )
220- if err != nil {
221- return "" , fmt .Errorf ("error reading response body: %w" , err )
222- }
223-
224- if resp .StatusCode != http .StatusOK {
225- if resp .StatusCode == http .StatusUnprocessableEntity {
226- return "" , ErrNotProvisioned
227- }
228-
229- return "" , jsonErrorResponse (b )
230- }
231-
232- var response getCodespaceTokenResponse
233- if err := json .Unmarshal (b , & response ); err != nil {
234- return "" , fmt .Errorf ("error unmarshaling response: %w" , err )
235- }
236-
237- return response .RepositoryToken , nil
238- }
239-
240- // GetCodespace returns a codespace for the user.
241- func (a * API ) GetCodespace (ctx context.Context , token , owner , codespaceName string ) (* codespace.Codespace , error ) {
183+ // GetCodespace returns the user codespace based on the provided name.
184+ // If the codespace is not found, an error is returned.
185+ // If includeConnection is true, it will return the connection information for the codespace.
186+ func (a * API ) GetCodespace (ctx context.Context , codespaceName string , includeConnection bool ) (* codespace.Codespace , error ) {
242187 req , err := http .NewRequest (
243188 http .MethodGet ,
244- a .githubAPI + "/vscs_internal/ user/" + owner + " /codespaces/"+ codespaceName ,
189+ a .githubAPI + "/user/codespaces/" + codespaceName ,
245190 nil ,
246191 )
247192 if err != nil {
248193 return nil , fmt .Errorf ("error creating request: %w" , err )
249194 }
250195
251- // TODO: use a.setHeaders()
252- req .Header .Set ("Authorization" , "Bearer " + token )
253- resp , err := a .do (ctx , req , "/vscs_internal/user/*/codespaces/*" )
196+ if includeConnection {
197+ q := req .URL .Query ()
198+ q .Add ("internal" , "true" )
199+ q .Add ("refresh" , "true" )
200+ req .URL .RawQuery = q .Encode ()
201+ }
202+
203+ a .setHeaders (req )
204+ resp , err := a .do (ctx , req , "/user/codespaces/*" )
254205 if err != nil {
255206 return nil , fmt .Errorf ("error making request: %w" , err )
256207 }
@@ -397,17 +348,16 @@ func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, loc
397348
398349// CreateCodespaceParams are the required parameters for provisioning a Codespace.
399350type CreateCodespaceParams struct {
400- User string
401351 RepositoryID int
402352 Branch , Machine , Location string
403353}
404354
405355// CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it
406356// fails to create.
407357func (a * API ) CreateCodespace (ctx context.Context , params * CreateCodespaceParams ) (* codespace.Codespace , error ) {
408- codespace , err := a .startCreate (ctx , params .RepositoryID , params .Machine , params .Branch , params .Location )
358+ cs , err := a .startCreate (ctx , params .RepositoryID , params .Machine , params .Branch , params .Location )
409359 if err != errProvisioningInProgress {
410- return codespace , err
360+ return nil , err
411361 }
412362
413363 // errProvisioningInProgress indicates that codespace creation did not complete
@@ -424,22 +374,17 @@ func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams
424374 case <- ctx .Done ():
425375 return nil , ctx .Err ()
426376 case <- ticker .C :
427- token , err : = a .GetCodespaceToken (ctx , params . User , codespace . Name )
377+ cs , err = a .GetCodespace (ctx , cs . Name , false )
428378 if err != nil {
429- if err == ErrNotProvisioned {
430- // Do nothing. We expect this to fail until the codespace is provisioned
431- continue
432- }
433-
434- return nil , fmt .Errorf ("failed to get codespace token: %w" , err )
379+ return nil , fmt .Errorf ("failed to get codespace: %w" , err )
435380 }
436381
437- codespace , err = a . GetCodespace ( ctx , token , params . User , codespace . Name )
438- if err != nil {
439- return nil , fmt . Errorf ( "failed to get codespace: %w" , err )
382+ // we continue to poll until the codespace shows as provisioned
383+ if cs . State != codespace . StateProvisioned {
384+ continue
440385 }
441386
442- return codespace , nil
387+ return cs , nil
443388 }
444389 }
445390}
0 commit comments