Skip to content

Commit

Permalink
small codescene fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Sep 29, 2024
1 parent db30f94 commit ea68110
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
33 changes: 14 additions & 19 deletions internal/report/sbom/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ func (r Reporter) Order(args report.IOrderArgs) error {
return ErrHandleArgs
}

uuid, err := r.generate(
orderArgs.CommitID,
orderArgs.RepositoryID,
orderArgs.Branch,
orderArgs.Vulnerabilities,
orderArgs.Licenses,
)
uuid, err := r.generate(orderArgs)
if err != nil {
return err
}
Expand All @@ -80,17 +74,17 @@ func (r Reporter) Order(args report.IOrderArgs) error {

}

func (r Reporter) generate(commitID, repositoryID, branch string, vulnerabilities, licenses bool) (string, error) {
func (r Reporter) generate(orderArgs OrderArgs) (string, error) {
// Tries to start generating an SBOM and returns the UUID for the report
body, err := json.Marshal(generateSbom{
Format: "CycloneDX",
RepositoryID: repositoryID,
CommitID: commitID,
RepositoryID: orderArgs.RepositoryID,
CommitID: orderArgs.CommitID,
Email: "",
Branch: branch,
Branch: orderArgs.Branch,
Locale: "en",
Vulnerabilities: vulnerabilities,
Licenses: licenses,
Vulnerabilities: orderArgs.Vulnerabilities,
Licenses: orderArgs.Licenses,
SendEmail: false,
VulnerabilityStatuses: []string{"vulnerable", "unexamined", "paused", "snoozed"},
})
Expand All @@ -116,18 +110,19 @@ func (r Reporter) generate(commitID, repositoryID, branch string, vulnerabilitie
} else {
fmt.Println("Successfully initialized SBOM generation")
}
generateSbomResponseJSON, err := io.ReadAll(response.Body)

return r.parseUUID(response.Body)
}

func (r Reporter) parseUUID(body io.Reader) (string, error) {
generateSbomResponseJSON, err := io.ReadAll(body)
if err != nil {
return "", err
}

var generateSbomResponse generateSbomResponse
err = json.Unmarshal(generateSbomResponseJSON, &generateSbomResponse)
if err != nil {
return "", err
}

return generateSbomResponse.ReportUUID, nil
return generateSbomResponse.ReportUUID, err
}

func (r Reporter) download(uuid string) ([]byte, error) {
Expand Down
27 changes: 17 additions & 10 deletions internal/report/sbom/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func TestOrderError(t *testing.T) {
debClientMock := testdata.NewDebClientMock()
debClientMock.AddMockResponse(testdata.MockResponse{StatusCode: http.StatusOK})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
args := OrderArgs{CommitID: "", RepositoryID: ""}
err := reporter.Order(args)
err := reporter.Order(OrderArgs{CommitID: "", RepositoryID: ""})
assert.Error(t, err)
}

Expand All @@ -32,8 +31,7 @@ func TestOrder(t *testing.T) {
ResponseBody: io.NopCloser(strings.NewReader("{}")),
})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
args := OrderArgs{CommitID: "", RepositoryID: ""}
err := reporter.Order(args)
err := reporter.Order(OrderArgs{CommitID: "", RepositoryID: ""})
assert.NoError(t, err)
}

Expand All @@ -47,8 +45,7 @@ func TestOrderDownloadErr(t *testing.T) {
StatusCode: http.StatusForbidden,
})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
args := OrderArgs{CommitID: "", RepositoryID: ""}
err := reporter.Order(args)
err := reporter.Order(OrderArgs{CommitID: "", RepositoryID: ""})
assert.Error(t, err)
}

Expand All @@ -67,7 +64,7 @@ func TestGenerateOK(t *testing.T) {
ResponseBody: io.NopCloser(strings.NewReader("{}")),
})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
uuid, err := reporter.generate("", "", "", false, false)
uuid, err := reporter.generate(orderArgs())
assert.NoError(t, err)
assert.NotNil(t, uuid)
}
Expand All @@ -79,7 +76,7 @@ func TestGenerateSubscriptionError(t *testing.T) {
ResponseBody: io.NopCloser(strings.NewReader("{}")),
})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
uuid, err := reporter.generate("", "", "", false, false)
uuid, err := reporter.generate(orderArgs())
assert.Error(t, err)
assert.NotNil(t, uuid)
}
Expand All @@ -91,7 +88,7 @@ func TestGenerateError(t *testing.T) {
ResponseBody: io.NopCloser(strings.NewReader("{}")),
})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
uuid, err := reporter.generate("", "", "", false, false)
uuid, err := reporter.generate(orderArgs())
assert.Error(t, err)
assert.NotNil(t, uuid)
}
Expand All @@ -100,7 +97,7 @@ func TestGenerateDefaultGetError(t *testing.T) {
debClientMock := testdata.NewDebClientMock()
debClientMock.AddMockResponse(testdata.MockResponse{Error: errors.New("")})
reporter := Reporter{DebClient: debClientMock, FileWriter: &ioTestData.FileWriterMock{}}
res, err := reporter.generate("", "", "", false, false)
res, err := reporter.generate(orderArgs())
assert.Error(t, err)
assert.Equal(t, "", res)
}
Expand Down Expand Up @@ -170,3 +167,13 @@ func TestWriteSBOM(t *testing.T) {
err := reporter.writeSBOM("", "", nil)
assert.Error(t, err)
}

func orderArgs() OrderArgs {
return OrderArgs{
Vulnerabilities: false,
Licenses: false,
Branch: "",
CommitID: "",
RepositoryID: "",
}
}

0 comments on commit ea68110

Please sign in to comment.