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)
| 1091 | // ListDevContainers returns a list of valid devcontainer.json files for the repo. Pass a negative limit to request all pages from |
| 1092 | // the API until all devcontainer.json files have been fetched. |
| 1093 | func (a *API) ListDevContainers(ctx context.Context, repoID int64, branch string, limit int) (devcontainers []DevContainerEntry, err error) { |
| 1094 | perPage := 100 |
| 1095 | if limit > 0 && limit < 100 { |
| 1096 | perPage = limit |
| 1097 | } |
| 1098 | |
| 1099 | u, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "repositories", strconv.FormatInt(repoID, 10), "codespaces", "devcontainers") |
| 1100 | if err != nil { |
| 1101 | return nil, err |
| 1102 | } |
| 1103 | u.SetQuery("per_page", strconv.Itoa(perPage)) |
| 1104 | if branch != "" { |
| 1105 | u.SetQuery("ref", branch) |
| 1106 | } |
| 1107 | var listURL safeurl.SafeURL = u |
| 1108 | |
| 1109 | for { |
| 1110 | req, err := http.NewRequest(http.MethodGet, listURL.String(), nil) |
| 1111 | if err != nil { |
| 1112 | return nil, fmt.Errorf("error creating request: %w", err) |
| 1113 | } |
| 1114 | a.setHeaders(req) |
| 1115 | |
| 1116 | resp, err := a.do(ctx, req, fmt.Sprintf("/repositories/%d/codespaces/devcontainers", repoID)) |
| 1117 | if err != nil { |
| 1118 | return nil, fmt.Errorf("error making request: %w", err) |
| 1119 | } |
| 1120 | defer resp.Body.Close() |
| 1121 | |
| 1122 | if resp.StatusCode != http.StatusOK { |
| 1123 | return nil, api.HandleHTTPError(resp) |
| 1124 | } |
| 1125 | |
| 1126 | var response struct { |
| 1127 | Devcontainers []DevContainerEntry `json:"devcontainers"` |
| 1128 | } |
| 1129 | |
| 1130 | dec := json.NewDecoder(resp.Body) |
| 1131 | if err := dec.Decode(&response); err != nil { |
| 1132 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |
| 1133 | } |
| 1134 | |
| 1135 | nextURL := findNextPage(resp.Header.Get("Link")) |
| 1136 | devcontainers = append(devcontainers, response.Devcontainers...) |
| 1137 | |
| 1138 | if nextURL == "" || (limit > 0 && len(devcontainers) >= limit) { |
| 1139 | break |
| 1140 | } |
| 1141 | |
| 1142 | if newPerPage := limit - len(devcontainers); limit > 0 && newPerPage < 100 { |
| 1143 | u, _ := url.Parse(nextURL) |
| 1144 | q := u.Query() |
| 1145 | q.Set("per_page", strconv.Itoa(newPerPage)) |
| 1146 | u.RawQuery = q.Encode() |
| 1147 | listURL = safeurl.NewImmutableSafeURL(u.String()) |
| 1148 | } else { |
| 1149 | listURL = safeurl.NewImmutableSafeURL(nextURL) |
| 1150 | } |
nothing calls this directly
no test coverage detected