CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it fails to create.
(ctx context.Context, params *CreateCodespaceParams)
| 893 | // CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it |
| 894 | // fails to create. |
| 895 | func (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 | |
| 930 | type startCreateRequest struct { |
| 931 | RepositoryID int64 `json:"repository_id"` |