diff --git a/dependency_list_export.go b/dependency_list_export.go index cf0ab40ca..2a14547ea 100644 --- a/dependency_list_export.go +++ b/dependency_list_export.go @@ -15,7 +15,7 @@ type DependencyListExportService struct { // // GitLab API docs: // https://docs.gitlab.com/ee/api/dependency_list_export.html#create-a-pipeline-level-dependency-list-export -type createDependencyListExportOptions struct { +type CreateDependencyListExportOptions struct { ExportType string `url:"export_type" json:"export_type"` } @@ -30,6 +30,8 @@ type DependencyListExport struct { Download string `json:"download"` } +const defaultExportType = "sbom" + // CreateDependencyListExport creates a new CycloneDX JSON export for all the project dependencies // detected in a pipeline. // @@ -40,12 +42,14 @@ type DependencyListExport struct { // // GitLab docs: // https://docs.gitlab.com/ee/api/dependency_list_export.html#create-a-pipeline-level-dependency-list-export -func (s *DependencyListExportService) CreateDependencyListExport(id int, options ...RequestOptionFunc) (*DependencyListExport, *Response, error) { +func (s *DependencyListExportService) CreateDependencyListExport(pipelineID int, opt *CreateDependencyListExportOptions, options ...RequestOptionFunc) (*DependencyListExport, *Response, error) { // POST /pipelines/:id/dependency_list_exports - createExportPath := fmt.Sprintf("pipelines/%d/dependency_list_exports", id) + createExportPath := fmt.Sprintf("pipelines/%d/dependency_list_exports", pipelineID) - opt := &createDependencyListExportOptions{ - ExportType: *Ptr("sbom"), + if opt == nil { + opt = &CreateDependencyListExportOptions{ + ExportType: *Ptr(defaultExportType), + } } req, err := s.client.NewRequest(http.MethodPost, createExportPath, opt, options) @@ -88,20 +92,20 @@ func (s *DependencyListExportService) GetDependencyListExport(id int, options .. // // GitLab docs: // https://docs.gitlab.com/ee/api/dependency_list_export.html#download-dependency-list-export -func (s *DependencyListExportService) DownloadDependencyListExport(id int, options ...RequestOptionFunc) (string, *Response, error) { +func (s *DependencyListExportService) DownloadDependencyListExport(id int, options ...RequestOptionFunc) ([]byte, *Response, error) { // GET /dependency_list_exports/:id/download downloadExportPath := fmt.Sprintf("dependency_list_exports/%d/download", id) req, err := s.client.NewRequest(http.MethodGet, downloadExportPath, nil, options) if err != nil { - return "", nil, err + return nil, nil, err } var sbomBuffer bytes.Buffer resp, err := s.client.Do(req, &sbomBuffer) if err != nil { - return "", resp, err + return nil, resp, err } - return sbomBuffer.String(), resp, nil + return sbomBuffer.Bytes(), resp, nil } diff --git a/dependency_list_export_test.go b/dependency_list_export_test.go index 7a7700bb8..08f58c32b 100644 --- a/dependency_list_export_test.go +++ b/dependency_list_export_test.go @@ -1,10 +1,13 @@ package gitlab import ( + "encoding/json" + "io" "net/http" "os" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -13,10 +16,22 @@ func TestCreateDependencyListExport(t *testing.T) { mux.HandleFunc("/api/v4/pipelines/1234/dependency_list_exports", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) + body, err := io.ReadAll(r.Body) + require.NoError(t, err) + + var content CreateDependencyListExportOptions + err = json.Unmarshal(body, &content) + require.NoError(t, err) + + assert.Equal(t, "sbom", content.ExportType) mustWriteHTTPResponse(t, w, "testdata/create_dependency_list_export.json") }) - export, _, err := client.DependencyListExport.CreateDependencyListExport(1234) + d := &CreateDependencyListExportOptions{ + ExportType: *Ptr("sbom"), + } + + export, _, err := client.DependencyListExport.CreateDependencyListExport(1234, d) require.NoError(t, err) want := &DependencyListExport{ @@ -59,9 +74,8 @@ func TestDownloadDependencyListExport(t *testing.T) { sbom, _, err := client.DependencyListExport.DownloadDependencyListExport(5678) require.NoError(t, err) - wantBytes, err := os.ReadFile("testdata/download_dependency_list_export.json") + want, err := os.ReadFile("testdata/download_dependency_list_export.json") require.NoError(t, err) - want := string(wantBytes) require.Equal(t, want, sbom) }