|
1 | 1 | //
|
2 |
| -// Copyright 2023 The Chainloop Authors. |
| 2 | +// Copyright 2023-2025 The Chainloop Authors. |
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | // you may not use this file except in compliance with the License.
|
@@ -136,20 +136,9 @@ func (d *SBOMUploader) Validate(ctx context.Context) error {
|
136 | 136 | }
|
137 | 137 |
|
138 | 138 | // Check if the project or parent project exists
|
139 |
| - var projectFound bool |
140 |
| - projects, err := listProjects(d.host, d.apiKey) |
141 |
| - if err != nil { |
| 139 | + if projectFound, err := projectExists(d.host, d.apiKey, existingProjectID); err != nil { |
142 | 140 | return fmt.Errorf("checking that the project exists: %w", err)
|
143 |
| - } |
144 |
| - |
145 |
| - for _, p := range projects { |
146 |
| - if p.ID == existingProjectID { |
147 |
| - projectFound = true |
148 |
| - break |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - if !projectFound { |
| 141 | + } else if !projectFound { |
153 | 142 | return fmt.Errorf("project with ID %q not found", existingProjectID)
|
154 | 143 | }
|
155 | 144 |
|
@@ -310,45 +299,30 @@ func uploadSBOMRequest(host *url.URL, apiKey string, values map[string]io.Reader
|
310 | 299 | return resp, nil
|
311 | 300 | }
|
312 | 301 |
|
313 |
| -type listProjectsResponseItem struct { |
314 |
| - ID string `json:"uuid"` |
315 |
| - Name string |
316 |
| -} |
317 |
| - |
318 |
| -type listProjectsResponse []listProjectsResponseItem |
319 |
| - |
320 | 302 | // We are listing projects instead of accessing a specific one to enable
|
321 | 303 | // son in the future listing and selection in the UI
|
322 |
| -func listProjects(host *url.URL, apiKey string) (listProjectsResponse, error) { |
323 |
| - apiEndpoint := host.JoinPath("/api/v1/project") |
| 304 | +func projectExists(host *url.URL, apiKey string, projectID string) (bool, error) { |
| 305 | + apiEndpoint := host.JoinPath(fmt.Sprintf("/api/v1/project/%s", projectID)) |
324 | 306 |
|
325 | 307 | req, err := http.NewRequest(http.MethodGet, apiEndpoint.String(), nil)
|
326 | 308 | if err != nil {
|
327 |
| - return nil, err |
| 309 | + return false, err |
328 | 310 | }
|
329 | 311 |
|
330 | 312 | req.Header.Set("X-Api-Key", apiKey)
|
331 | 313 | // Submit the request
|
332 | 314 | res, err := http.DefaultClient.Do(req)
|
333 | 315 | if err != nil {
|
334 |
| - return nil, err |
| 316 | + return false, err |
335 | 317 | }
|
336 | 318 |
|
337 | 319 | // Check the response
|
338 |
| - if res.StatusCode != http.StatusOK { |
339 |
| - err = fmt.Errorf("bad status: %s", res.Status) |
340 |
| - return nil, err |
341 |
| - } |
342 |
| - |
343 |
| - resBody, err := io.ReadAll(res.Body) |
344 |
| - if err != nil { |
345 |
| - return nil, err |
346 |
| - } |
347 |
| - |
348 |
| - resp := make([]listProjectsResponseItem, 0) |
349 |
| - if err := json.Unmarshal(resBody, &resp); err != nil { |
350 |
| - return nil, err |
| 320 | + switch res.StatusCode { |
| 321 | + case http.StatusOK: |
| 322 | + return true, nil |
| 323 | + case http.StatusNotFound: |
| 324 | + return false, nil |
| 325 | + default: |
| 326 | + return false, fmt.Errorf("bad status: %s", res.Status) |
351 | 327 | }
|
352 |
| - |
353 |
| - return resp, nil |
354 | 328 | }
|
0 commit comments