MCPcopy Create free account
hub / github.com/cli/cli / CreateCodespace

Method CreateCodespace

internal/codespaces/api/api.go:895–928  ·  view source on GitHub ↗

CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it fails to create.

(ctx context.Context, params *CreateCodespaceParams)

Source from the content-addressed store, hash-verified

893// CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it
894// fails to create.
895func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams) (*Codespace, error) {
896 codespace, err := a.startCreate(ctx, params)
897 if !errors.Is(err, errProvisioningInProgress) {
898 return codespace, err
899 }
900
901 // errProvisioningInProgress indicates that codespace creation did not complete
902 // within the GitHub API RPC time limit (10s), so it continues asynchronously.
903 // We must poll the server to discover the outcome.
904 ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
905 defer cancel()
906
907 ticker := time.NewTicker(1 * time.Second)
908 defer ticker.Stop()
909
910 for {
911 select {
912 case <-ctx.Done():
913 return nil, ctx.Err()
914 case <-ticker.C:
915 codespace, err = a.GetCodespace(ctx, codespace.Name, false)
916 if err != nil {
917 return nil, fmt.Errorf("failed to get codespace: %w", err)
918 }
919
920 // we continue to poll until the codespace shows as provisioned
921 if codespace.State != CodespaceStateAvailable {
922 continue
923 }
924
925 return codespace, nil
926 }
927 }
928}
929
930type startCreateRequest struct {
931 RepositoryID int64 `json:"repository_id"`

Callers 3

TestCreateCodespacesFunction · 0.95

Calls 5

startCreateMethod · 0.95
GetCodespaceMethod · 0.95
ErrMethod · 0.80
DoneMethod · 0.65
ErrorfMethod · 0.65

Tested by 3

TestCreateCodespacesFunction · 0.76