ListCodespaces returns a list of codespaces for the user. Pass a negative limit to request all pages from the API until all codespaces have been fetched.
(ctx context.Context, opts ListCodespacesOptions)
| 354 | // ListCodespaces returns a list of codespaces for the user. Pass a negative limit to request all pages from |
| 355 | // the API until all codespaces have been fetched. |
| 356 | func (a *API) ListCodespaces(ctx context.Context, opts ListCodespacesOptions) (codespaces []*Codespace, err error) { |
| 357 | var ( |
| 358 | perPage = 100 |
| 359 | limit = opts.Limit |
| 360 | ) |
| 361 | |
| 362 | if limit > 0 && limit < 100 { |
| 363 | perPage = limit |
| 364 | } |
| 365 | |
| 366 | var ( |
| 367 | listURL string |
| 368 | spanName string |
| 369 | ) |
| 370 | |
| 371 | if opts.RepoName != "" { |
| 372 | listURL = fmt.Sprintf("%s/repos/%s/codespaces?per_page=%d", a.githubAPI, opts.RepoName, perPage) |
| 373 | spanName = "/repos/*/codespaces" |
| 374 | } else if opts.OrgName != "" { |
| 375 | // the endpoints below can only be called by the organization admins |
| 376 | orgName := opts.OrgName |
| 377 | if opts.UserName != "" { |
| 378 | userName := opts.UserName |
| 379 | listURL = fmt.Sprintf("%s/orgs/%s/members/%s/codespaces?per_page=%d", a.githubAPI, orgName, userName, perPage) |
| 380 | spanName = "/orgs/*/members/*/codespaces" |
| 381 | } else { |
| 382 | listURL = fmt.Sprintf("%s/orgs/%s/codespaces?per_page=%d", a.githubAPI, orgName, perPage) |
| 383 | spanName = "/orgs/*/codespaces" |
| 384 | } |
| 385 | } else { |
| 386 | listURL = fmt.Sprintf("%s/user/codespaces?per_page=%d", a.githubAPI, perPage) |
| 387 | spanName = "/user/codespaces" |
| 388 | } |
| 389 | |
| 390 | for { |
| 391 | req, err := http.NewRequest(http.MethodGet, listURL, nil) |
| 392 | if err != nil { |
| 393 | return nil, fmt.Errorf("error creating request: %w", err) |
| 394 | } |
| 395 | a.setHeaders(req) |
| 396 | |
| 397 | resp, err := a.do(ctx, req, spanName) |
| 398 | if err != nil { |
| 399 | return nil, fmt.Errorf("error making request: %w", err) |
| 400 | } |
| 401 | defer resp.Body.Close() |
| 402 | |
| 403 | if resp.StatusCode != http.StatusOK { |
| 404 | return nil, api.HandleHTTPError(resp) |
| 405 | } |
| 406 | |
| 407 | var response struct { |
| 408 | Codespaces []*Codespace `json:"codespaces"` |
| 409 | } |
| 410 | |
| 411 | dec := json.NewDecoder(resp.Body) |
| 412 | if err := dec.Decode(&response); err != nil { |
| 413 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |