@@ -239,7 +239,6 @@ func (a *API) GetCodespaceToken(ctx context.Context, ownerLogin, codespaceName s
239239 }
240240
241241 if resp .StatusCode != http .StatusOK {
242-
243242 if resp .StatusCode == http .StatusUnprocessableEntity {
244243 return "" , ErrNotProvisioned
245244 }
@@ -405,8 +404,8 @@ func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Rep
405404
406405// ProvisionCodespaceParams are the required parameters for provisioning a Codespace.
407406type ProvisionCodespaceParams struct {
408- User * User
409- Repository * Repository
407+ User string
408+ RepositoryID int
410409 Branch , Machine , Location string
411410}
412411
@@ -415,21 +414,21 @@ type logger interface {
415414 Println (v ... interface {}) (int , error )
416415}
417416
418- // ProvisionCodespace creates a codespace with the given parameters and handles polling in the case
419- // of initial creation failures .
420- func (a * API ) ProvisionCodespace (ctx context.Context , log logger , params * ProvisionCodespaceParams ) (* Codespace , error ) {
421- codespace , err := a .createCodespace (
422- ctx , params .User , params .Repository , params .Machine , params .Branch , params .Location ,
417+ // CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it
418+ // fails to create .
419+ func (a * API ) CreateCodespace (ctx context.Context , log logger , params * ProvisionCodespaceParams ) (* Codespace , error ) {
420+ codespace , err := a .startCreate (
421+ ctx , params .User , params .RepositoryID , params .Machine , params .Branch , params .Location ,
423422 )
424423 if err != nil {
425- // This error is returned by the API when the initial creation fails with a retryable error.
426- // A retryable error means that GitHub will retry to re-create Codespace and clients should poll
427- // the API and attempt to fetch the Codespace for the next two minutes .
424+ // errProvisioningInProgress indicates that codespace creation did not complete
425+ // within the GitHub API RPC time limit (10s), so it continues asynchronously.
426+ // We must poll the server to discover the outcome .
428427 if err == errProvisioningInProgress {
429428 pollTimeout := 2 * time .Minute
430429 pollInterval := 1 * time .Second
431430 log .Print ("." )
432- codespace , err = pollForCodespace (ctx , a , log , pollTimeout , pollInterval , params .User . Login , codespace .Name )
431+ codespace , err = pollForCodespace (ctx , a , log , pollTimeout , pollInterval , params .User , codespace .Name )
433432 log .Print ("\n " )
434433
435434 if err != nil {
@@ -487,13 +486,17 @@ type createCodespaceRequest struct {
487486
488487var errProvisioningInProgress = errors .New ("provisioning in progress" )
489488
490- func (a * API ) createCodespace (ctx context.Context , user * User , repository * Repository , sku , branch , location string ) (* Codespace , error ) {
491- requestBody , err := json .Marshal (createCodespaceRequest {repository .ID , branch , location , sku })
489+ // startCreate starts the creation of a codespace.
490+ // It may return success or an error, or errProvisioningInProgress indicating that the operation
491+ // did not complete before the GitHub API's time limit for RPCs (10s), in which case the caller
492+ // must poll the server to learn the outcome.
493+ func (a * API ) startCreate (ctx context.Context , user string , repository int , sku , branch , location string ) (* Codespace , error ) {
494+ requestBody , err := json .Marshal (createCodespaceRequest {repository , branch , location , sku })
492495 if err != nil {
493496 return nil , fmt .Errorf ("error marshaling request: %w" , err )
494497 }
495498
496- req , err := http .NewRequest (http .MethodPost , githubAPI + "/vscs_internal/user/" + user . Login + "/codespaces" , bytes .NewBuffer (requestBody ))
499+ req , err := http .NewRequest (http .MethodPost , githubAPI + "/vscs_internal/user/" + user + "/codespaces" , bytes .NewBuffer (requestBody ))
497500 if err != nil {
498501 return nil , fmt .Errorf ("error creating request: %w" , err )
499502 }
@@ -514,11 +517,7 @@ func (a *API) createCodespace(ctx context.Context, user *User, repository *Repos
514517 case resp .StatusCode > http .StatusAccepted :
515518 return nil , jsonErrorResponse (b )
516519 case resp .StatusCode == http .StatusAccepted :
517- // When the API returns a 202, it means that the initial creation failed but it is
518- // being retried. For clients this means that they must implement a polling strategy
519- // to check for the codespace existence for the next two minutes. We return an error
520- // here so callers can detect and handle this condition.
521- return nil , errProvisioningInProgress
520+ return nil , errProvisioningInProgress // RPC finished before result of creation known
522521 }
523522
524523 var response Codespace
0 commit comments