MCPcopy
hub / github.com/cli/cli / ListDevContainers

Method ListDevContainers

internal/codespaces/api/api.go:1014–1072  ·  view source on GitHub ↗

ListDevContainers returns a list of valid devcontainer.json files for the repo. Pass a negative limit to request all pages from the API until all devcontainer.json files have been fetched.

(ctx context.Context, repoID int64, branch string, limit int)

Source from the content-addressed store, hash-verified

1012// ListDevContainers returns a list of valid devcontainer.json files for the repo. Pass a negative limit to request all pages from
1013// the API until all devcontainer.json files have been fetched.
1014func (a *API) ListDevContainers(ctx context.Context, repoID int64, branch string, limit int) (devcontainers []DevContainerEntry, err error) {
1015 perPage := 100
1016 if limit > 0 && limit < 100 {
1017 perPage = limit
1018 }
1019
1020 v := url.Values{}
1021 v.Set("per_page", strconv.Itoa(perPage))
1022 if branch != "" {
1023 v.Set("ref", branch)
1024 }
1025 listURL := fmt.Sprintf("%s/repositories/%d/codespaces/devcontainers?%s", a.githubAPI, repoID, v.Encode())
1026
1027 for {
1028 req, err := http.NewRequest(http.MethodGet, listURL, nil)
1029 if err != nil {
1030 return nil, fmt.Errorf("error creating request: %w", err)
1031 }
1032 a.setHeaders(req)
1033
1034 resp, err := a.do(ctx, req, fmt.Sprintf("/repositories/%d/codespaces/devcontainers", repoID))
1035 if err != nil {
1036 return nil, fmt.Errorf("error making request: %w", err)
1037 }
1038 defer resp.Body.Close()
1039
1040 if resp.StatusCode != http.StatusOK {
1041 return nil, api.HandleHTTPError(resp)
1042 }
1043
1044 var response struct {
1045 Devcontainers []DevContainerEntry `json:"devcontainers"`
1046 }
1047
1048 dec := json.NewDecoder(resp.Body)
1049 if err := dec.Decode(&response); err != nil {
1050 return nil, fmt.Errorf("error unmarshalling response: %w", err)
1051 }
1052
1053 nextURL := findNextPage(resp.Header.Get("Link"))
1054 devcontainers = append(devcontainers, response.Devcontainers...)
1055
1056 if nextURL == "" || (limit > 0 && len(devcontainers) >= limit) {
1057 break
1058 }
1059
1060 if newPerPage := limit - len(devcontainers); limit > 0 && newPerPage < 100 {
1061 u, _ := url.Parse(nextURL)
1062 q := u.Query()
1063 q.Set("per_page", strconv.Itoa(newPerPage))
1064 u.RawQuery = q.Encode()
1065 listURL = u.String()
1066 } else {
1067 listURL = nextURL
1068 }
1069 }
1070
1071 return devcontainers, nil

Callers

nothing calls this directly

Calls 10

setHeadersMethod · 0.95
doMethod · 0.95
HandleHTTPErrorFunction · 0.92
findNextPageFunction · 0.70
SetMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
QueryMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected