startCreate starts the creation of a codespace. It may return success or an error, or errProvisioningInProgress indicating that the operation did not complete before the GitHub API's time limit for RPCs (10s), in which case the caller must poll the server to learn the outcome.
(ctx context.Context, params *CreateCodespaceParams)
| 886 | // did not complete before the GitHub API's time limit for RPCs (10s), in which case the caller |
| 887 | // must poll the server to learn the outcome. |
| 888 | func (a *API) startCreate(ctx context.Context, params *CreateCodespaceParams) (*Codespace, error) { |
| 889 | if params == nil { |
| 890 | return nil, errors.New("startCreate missing parameters") |
| 891 | } |
| 892 | |
| 893 | requestBody, err := json.Marshal(startCreateRequest{ |
| 894 | RepositoryID: params.RepositoryID, |
| 895 | IdleTimeoutMinutes: params.IdleTimeoutMinutes, |
| 896 | RetentionPeriodMinutes: params.RetentionPeriodMinutes, |
| 897 | Ref: params.Branch, |
| 898 | Location: params.Location, |
| 899 | Machine: params.Machine, |
| 900 | DevContainerPath: params.DevContainerPath, |
| 901 | VSCSTarget: params.VSCSTarget, |
| 902 | VSCSTargetURL: params.VSCSTargetURL, |
| 903 | PermissionsOptOut: params.PermissionsOptOut, |
| 904 | DisplayName: params.DisplayName, |
| 905 | }) |
| 906 | |
| 907 | if err != nil { |
| 908 | return nil, fmt.Errorf("error marshaling request: %w", err) |
| 909 | } |
| 910 | |
| 911 | req, err := http.NewRequest(http.MethodPost, a.githubAPI+"/user/codespaces", bytes.NewBuffer(requestBody)) |
| 912 | if err != nil { |
| 913 | return nil, fmt.Errorf("error creating request: %w", err) |
| 914 | } |
| 915 | |
| 916 | a.setHeaders(req) |
| 917 | resp, err := a.do(ctx, req, "/user/codespaces") |
| 918 | if err != nil { |
| 919 | return nil, fmt.Errorf("error making request: %w", err) |
| 920 | } |
| 921 | defer resp.Body.Close() |
| 922 | |
| 923 | if resp.StatusCode == http.StatusAccepted { |
| 924 | b, err := io.ReadAll(resp.Body) |
| 925 | if err != nil { |
| 926 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 927 | } |
| 928 | |
| 929 | var response Codespace |
| 930 | if err := json.Unmarshal(b, &response); err != nil { |
| 931 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |
| 932 | } |
| 933 | |
| 934 | return &response, errProvisioningInProgress // RPC finished before result of creation known |
| 935 | } else if resp.StatusCode == http.StatusUnauthorized { |
| 936 | var ( |
| 937 | ue AcceptPermissionsRequiredError |
| 938 | bodyCopy = &bytes.Buffer{} |
| 939 | r = io.TeeReader(resp.Body, bodyCopy) |
| 940 | ) |
| 941 | |
| 942 | b, err := io.ReadAll(r) |
| 943 | if err != nil { |
| 944 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 945 | } |
no test coverage detected