CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it fails to create.
(ctx context.Context, params *CreateCodespaceParams)
| 822 | // CreateCodespace creates a codespace with the given parameters and returns a non-nil error if it |
| 823 | // fails to create. |
| 824 | func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams) (*Codespace, error) { |
| 825 | codespace, err := a.startCreate(ctx, params) |
| 826 | if !errors.Is(err, errProvisioningInProgress) { |
| 827 | return codespace, err |
| 828 | } |
| 829 | |
| 830 | // errProvisioningInProgress indicates that codespace creation did not complete |
| 831 | // within the GitHub API RPC time limit (10s), so it continues asynchronously. |
| 832 | // We must poll the server to discover the outcome. |
| 833 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 834 | defer cancel() |
| 835 | |
| 836 | ticker := time.NewTicker(1 * time.Second) |
| 837 | defer ticker.Stop() |
| 838 | |
| 839 | for { |
| 840 | select { |
| 841 | case <-ctx.Done(): |
| 842 | return nil, ctx.Err() |
| 843 | case <-ticker.C: |
| 844 | codespace, err = a.GetCodespace(ctx, codespace.Name, false) |
| 845 | if err != nil { |
| 846 | return nil, fmt.Errorf("failed to get codespace: %w", err) |
| 847 | } |
| 848 | |
| 849 | // we continue to poll until the codespace shows as provisioned |
| 850 | if codespace.State != CodespaceStateAvailable { |
| 851 | continue |
| 852 | } |
| 853 | |
| 854 | return codespace, nil |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | type startCreateRequest struct { |
| 860 | RepositoryID int `json:"repository_id"` |