(ctx context.Context, codespace *Codespace, path string)
| 1225 | } |
| 1226 | |
| 1227 | func (a *API) GetCodespaceRepositoryContents(ctx context.Context, codespace *Codespace, path string) ([]byte, error) { |
| 1228 | owner, name, err := safeurl.RepoPartsFromNWO(codespace.Repository.FullName) |
| 1229 | if err != nil { |
| 1230 | return nil, err |
| 1231 | } |
| 1232 | u, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "repos", owner, name, "contents", path) |
| 1233 | if err != nil { |
| 1234 | return nil, err |
| 1235 | } |
| 1236 | req, err := http.NewRequest(http.MethodGet, u.String(), nil) |
| 1237 | if err != nil { |
| 1238 | return nil, fmt.Errorf("error creating request: %w", err) |
| 1239 | } |
| 1240 | |
| 1241 | q := req.URL.Query() |
| 1242 | q.Add("ref", codespace.GitStatus.Ref) |
| 1243 | req.URL.RawQuery = q.Encode() |
| 1244 | |
| 1245 | a.setHeaders(req) |
| 1246 | resp, err := a.do(ctx, req, "/repos/*/contents/*") |
| 1247 | if err != nil { |
| 1248 | return nil, fmt.Errorf("error making request: %w", err) |
| 1249 | } |
| 1250 | defer resp.Body.Close() |
| 1251 | |
| 1252 | if resp.StatusCode == http.StatusNotFound { |
| 1253 | return nil, nil |
| 1254 | } else if resp.StatusCode != http.StatusOK { |
| 1255 | return nil, api.HandleHTTPError(resp) |
| 1256 | } |
| 1257 | |
| 1258 | b, err := io.ReadAll(resp.Body) |
| 1259 | if err != nil { |
| 1260 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 1261 | } |
| 1262 | |
| 1263 | var response getCodespaceRepositoryContentsResponse |
| 1264 | if err := json.Unmarshal(b, &response); err != nil { |
| 1265 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |
| 1266 | } |
| 1267 | |
| 1268 | decoded, err := base64.StdEncoding.DecodeString(response.Content) |
| 1269 | if err != nil { |
| 1270 | return nil, fmt.Errorf("error decoding content: %w", err) |
| 1271 | } |
| 1272 | |
| 1273 | return decoded, nil |
| 1274 | } |
| 1275 | |
| 1276 | // do executes the given request and returns the response. It creates an |
| 1277 | // opentracing span to track the length of the request. |
nothing calls this directly
no test coverage detected