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

Method startCreate

internal/codespaces/api/api.go:959–1048  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

957// did not complete before the GitHub API's time limit for RPCs (10s), in which case the caller
958// must poll the server to learn the outcome.
959func (a *API) startCreate(ctx context.Context, params *CreateCodespaceParams) (*Codespace, error) {
960 if params == nil {
961 return nil, errors.New("startCreate missing parameters")
962 }
963
964 requestBody, err := json.Marshal(startCreateRequest{
965 RepositoryID: params.RepositoryID,
966 IdleTimeoutMinutes: params.IdleTimeoutMinutes,
967 RetentionPeriodMinutes: params.RetentionPeriodMinutes,
968 Ref: params.Branch,
969 Location: params.Location,
970 Machine: params.Machine,
971 DevContainerPath: params.DevContainerPath,
972 VSCSTarget: params.VSCSTarget,
973 VSCSTargetURL: params.VSCSTargetURL,
974 PermissionsOptOut: params.PermissionsOptOut,
975 DisplayName: params.DisplayName,
976 })
977
978 if err != nil {
979 return nil, fmt.Errorf("error marshaling request: %w", err)
980 }
981
982 u, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "user", "codespaces")
983 if err != nil {
984 return nil, err
985 }
986 req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(requestBody))
987 if err != nil {
988 return nil, fmt.Errorf("error creating request: %w", err)
989 }
990
991 a.setHeaders(req)
992 resp, err := a.do(ctx, req, "/user/codespaces")
993 if err != nil {
994 return nil, fmt.Errorf("error making request: %w", err)
995 }
996 defer resp.Body.Close()
997
998 if resp.StatusCode == http.StatusAccepted {
999 b, err := io.ReadAll(resp.Body)
1000 if err != nil {
1001 return nil, fmt.Errorf("error reading response body: %w", err)
1002 }
1003
1004 var response Codespace
1005 if err := json.Unmarshal(b, &response); err != nil {
1006 return nil, fmt.Errorf("error unmarshalling response: %w", err)
1007 }
1008
1009 return &response, errProvisioningInProgress // RPC finished before result of creation known
1010 } else if resp.StatusCode == http.StatusUnauthorized {
1011 var (
1012 ue AcceptPermissionsRequiredError
1013 bodyCopy = &bytes.Buffer{}
1014 r = io.TeeReader(resp.Body, bodyCopy)
1015 )
1016

Callers 1

CreateCodespaceMethod · 0.95

Calls 7

setHeadersMethod · 0.95
doMethod · 0.95
JoinPathWithHostPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
ErrorfMethod · 0.65
StringMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected