Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
fix default export type and make DownloadDependencyListExport return …
Browse files Browse the repository at this point in the history
…an io.Reader
  • Loading branch information
lmphil committed Nov 21, 2024
1 parent 0be4373 commit d6d274c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
26 changes: 19 additions & 7 deletions dependency_list_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitlab
import (
"bytes"
"fmt"
"io"
"net/http"
)

Expand All @@ -16,7 +17,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 {
ExportType string `url:"export_type" json:"export_type"`
ExportType *string `url:"export_type" json:"export_type"`
}

// DependencyListExport represents a request for a GitLab project's dependency list.
Expand Down Expand Up @@ -47,9 +48,10 @@ func (s *DependencyListExportService) CreateDependencyListExport(pipelineID int,
createExportPath := fmt.Sprintf("pipelines/%d/dependency_list_exports", pipelineID)

if opt == nil {
opt = &CreateDependencyListExportOptions{
ExportType: *Ptr(defaultExportType),
}
opt = &CreateDependencyListExportOptions{}
}
if opt.ExportType == nil {
opt.ExportType = Ptr(defaultExportType)
}

req, err := s.client.NewRequest(http.MethodPost, createExportPath, opt, options)
Expand Down Expand Up @@ -90,11 +92,21 @@ func (s *DependencyListExportService) GetDependencyListExport(id int, options ..

// DownloadDependencyListExport downloads a single dependency list export.
//
// The github.com/CycloneDX/cyclonedx-go package can be used to parse the returned bytes.
// The github.com/CycloneDX/cyclonedx-go package can be used to parse the data from the returned io.Reader.
// -----------------[ Example ]------------------
//
// sbom := new(cdx.BOM)
// decoder := cdx.NewBOMDecoder(reader, cdx.BOMFileFormatJSON)
//
// if err = decoder.Decode(sbom); err != nil {
// panic(err)
// }
//
// ----------------------------------------------
//
// GitLab docs:
// https://docs.gitlab.com/ee/api/dependency_list_export.html#download-dependency-list-export
func (s *DependencyListExportService) DownloadDependencyListExport(id int, options ...RequestOptionFunc) ([]byte, *Response, error) {
func (s *DependencyListExportService) DownloadDependencyListExport(id int, options ...RequestOptionFunc) (io.Reader, *Response, error) {
// GET /dependency_list_exports/:id/download
downloadExportPath := fmt.Sprintf("dependency_list_exports/%d/download", id)

Expand All @@ -109,5 +121,5 @@ func (s *DependencyListExportService) DownloadDependencyListExport(id int, optio
return nil, resp, err
}

return sbomBuffer.Bytes(), resp, nil
return &sbomBuffer, resp, nil
}
14 changes: 9 additions & 5 deletions dependency_list_export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitlab

import (
"bytes"
"encoding/json"
"io"
"net/http"
Expand All @@ -23,12 +24,12 @@ func TestCreateDependencyListExport(t *testing.T) {
err = json.Unmarshal(body, &content)
require.NoError(t, err)

assert.Equal(t, "sbom", content.ExportType)
assert.Equal(t, "sbom", *content.ExportType)
mustWriteHTTPResponse(t, w, "testdata/create_dependency_list_export.json")
})

d := &CreateDependencyListExportOptions{
ExportType: *Ptr("sbom"),
ExportType: Ptr("sbom"),
}

export, _, err := client.DependencyListExport.CreateDependencyListExport(1234, d)
Expand Down Expand Up @@ -71,11 +72,14 @@ func TestDownloadDependencyListExport(t *testing.T) {
mustWriteHTTPResponse(t, w, "testdata/download_dependency_list_export.json")
})

sbom, _, err := client.DependencyListExport.DownloadDependencyListExport(5678)
sbomReader, _, err := client.DependencyListExport.DownloadDependencyListExport(5678)
require.NoError(t, err)

want, err := os.ReadFile("testdata/download_dependency_list_export.json")
expectedSbom, err := os.ReadFile("testdata/download_dependency_list_export.json")
require.NoError(t, err)

require.Equal(t, want, sbom)
var want bytes.Buffer
want.Write(expectedSbom)

require.Equal(t, &want, sbomReader)
}

0 comments on commit d6d274c

Please sign in to comment.