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

Commit

Permalink
fix and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lmphil committed Nov 20, 2024
1 parent 9ccac05 commit 35f6959
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
22 changes: 13 additions & 9 deletions dependency_list_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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.
//
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
20 changes: 17 additions & 3 deletions dependency_list_export_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package gitlab

import (
"encoding/json"
"io"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -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{
Expand Down Expand Up @@ -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)
}

0 comments on commit 35f6959

Please sign in to comment.