|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +type DependencyListExportService struct { |
| 11 | + client *Client |
| 12 | +} |
| 13 | + |
| 14 | +// CreateDependencyListExportOptions represents the available CreateDependencyListExport() |
| 15 | +// options. |
| 16 | +// |
| 17 | +// GitLab API docs: |
| 18 | +// https://docs.gitlab.com/ee/api/dependency_list_export.html#create-a-pipeline-level-dependency-list-export |
| 19 | +type CreateDependencyListExportOptions struct { |
| 20 | + ExportType *string `url:"export_type" json:"export_type"` |
| 21 | +} |
| 22 | + |
| 23 | +// DependencyListExport represents a request for a GitLab project's dependency list. |
| 24 | +// |
| 25 | +// GitLab API docs: |
| 26 | +// https://docs.gitlab.com/ee/api/dependency_list_export.html#create-a-pipeline-level-dependency-list-export |
| 27 | +type DependencyListExport struct { |
| 28 | + ID int `json:"id"` |
| 29 | + HasFinished bool `json:"has_finished"` |
| 30 | + Self string `json:"self"` |
| 31 | + Download string `json:"download"` |
| 32 | +} |
| 33 | + |
| 34 | +const defaultExportType = "sbom" |
| 35 | + |
| 36 | +// CreateDependencyListExport creates a new CycloneDX JSON export for all the project dependencies |
| 37 | +// detected in a pipeline. |
| 38 | +// |
| 39 | +// If an authenticated user does not have permission to read_dependency, this request returns a 403 |
| 40 | +// Forbidden status code. |
| 41 | +// |
| 42 | +// SBOM exports can be only accessed by the export’s author. |
| 43 | +// |
| 44 | +// GitLab docs: |
| 45 | +// https://docs.gitlab.com/ee/api/dependency_list_export.html#create-a-pipeline-level-dependency-list-export |
| 46 | +func (s *DependencyListExportService) CreateDependencyListExport(pipelineID int, opt *CreateDependencyListExportOptions, options ...RequestOptionFunc) (*DependencyListExport, *Response, error) { |
| 47 | + // POST /pipelines/:id/dependency_list_exports |
| 48 | + createExportPath := fmt.Sprintf("pipelines/%d/dependency_list_exports", pipelineID) |
| 49 | + |
| 50 | + if opt == nil { |
| 51 | + opt = &CreateDependencyListExportOptions{} |
| 52 | + } |
| 53 | + if opt.ExportType == nil { |
| 54 | + opt.ExportType = Ptr(defaultExportType) |
| 55 | + } |
| 56 | + |
| 57 | + req, err := s.client.NewRequest(http.MethodPost, createExportPath, opt, options) |
| 58 | + if err != nil { |
| 59 | + return nil, nil, err |
| 60 | + } |
| 61 | + |
| 62 | + export := new(DependencyListExport) |
| 63 | + resp, err := s.client.Do(req, &export) |
| 64 | + if err != nil { |
| 65 | + return nil, resp, err |
| 66 | + } |
| 67 | + |
| 68 | + return export, resp, nil |
| 69 | +} |
| 70 | + |
| 71 | +// GetDependencyListExport gets metadata about a single dependency list export. |
| 72 | +// |
| 73 | +// GitLab docs: |
| 74 | +// https://docs.gitlab.com/ee/api/dependency_list_export.html#get-single-dependency-list-export |
| 75 | +func (s *DependencyListExportService) GetDependencyListExport(id int, options ...RequestOptionFunc) (*DependencyListExport, *Response, error) { |
| 76 | + // GET /dependency_list_exports/:id |
| 77 | + getExportPath := fmt.Sprintf("dependency_list_exports/%d", id) |
| 78 | + |
| 79 | + req, err := s.client.NewRequest(http.MethodGet, getExportPath, nil, options) |
| 80 | + if err != nil { |
| 81 | + return nil, nil, err |
| 82 | + } |
| 83 | + |
| 84 | + export := new(DependencyListExport) |
| 85 | + resp, err := s.client.Do(req, &export) |
| 86 | + if err != nil { |
| 87 | + return nil, resp, err |
| 88 | + } |
| 89 | + |
| 90 | + return export, resp, nil |
| 91 | +} |
| 92 | + |
| 93 | +// DownloadDependencyListExport downloads a single dependency list export. |
| 94 | +// |
| 95 | +// The github.com/CycloneDX/cyclonedx-go package can be used to parse the data from the returned io.Reader. |
| 96 | +// |
| 97 | +// sbom := new(cdx.BOM) |
| 98 | +// decoder := cdx.NewBOMDecoder(reader, cdx.BOMFileFormatJSON) |
| 99 | +// |
| 100 | +// if err = decoder.Decode(sbom); err != nil { |
| 101 | +// panic(err) |
| 102 | +// } |
| 103 | +// |
| 104 | +// GitLab docs: |
| 105 | +// https://docs.gitlab.com/ee/api/dependency_list_export.html#download-dependency-list-export |
| 106 | +func (s *DependencyListExportService) DownloadDependencyListExport(id int, options ...RequestOptionFunc) (io.Reader, *Response, error) { |
| 107 | + // GET /dependency_list_exports/:id/download |
| 108 | + downloadExportPath := fmt.Sprintf("dependency_list_exports/%d/download", id) |
| 109 | + |
| 110 | + req, err := s.client.NewRequest(http.MethodGet, downloadExportPath, nil, options) |
| 111 | + if err != nil { |
| 112 | + return nil, nil, err |
| 113 | + } |
| 114 | + |
| 115 | + var sbomBuffer bytes.Buffer |
| 116 | + resp, err := s.client.Do(req, &sbomBuffer) |
| 117 | + if err != nil { |
| 118 | + return nil, resp, err |
| 119 | + } |
| 120 | + |
| 121 | + return &sbomBuffer, resp, nil |
| 122 | +} |
0 commit comments