Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAlvo1 committed Jul 14, 2024
1 parent a0589f7 commit fef3bcc
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions internal/services/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,74 @@ const (
completedStatus = "completed"
)

func ExportSbomResults(exportWrapper wrappers.ExportWrapper,
func ExportSbomResults(
exportWrapper wrappers.ExportWrapper,
targetFile string,
results *wrappers.ResultSummary,
formatSbomOptions string) error {
formatSbomOptions string,
) error {
payload, err := preparePayload(results.ScanID, formatSbomOptions)
if err != nil {
return err
}

log.Println("Generating SBOM report with " + payload.FileFormat + " file format")
sbomresp, err := exportWrapper.GenerateSbomReport(payload)
if err != nil {
return err
}

pollingResp, err := pollForCompletion(exportWrapper, sbomresp.ExportID)
if err != nil {
return err
}

if err := exportWrapper.DownloadSbomReport(pollingResp.ExportID, targetFile); err != nil {
return errors.Wrapf(err, "Failed downloading SBOM report")
}
return nil
}

func preparePayload(scanID, formatSbomOptions string) (*wrappers.ExportRequestPayload, error) {
payload := &wrappers.ExportRequestPayload{
ScanID: results.ScanID,
ScanID: scanID,
FileFormat: DefaultSbomOption,
}

if formatSbomOptions != "" && formatSbomOptions != DefaultSbomOption {
format, err := validateSbomOptions(formatSbomOptions)
if err != nil {
return err
return nil, err
}
payload.FileFormat = format
}
pollingResp := &wrappers.ExportPollingResponse{}

sbomresp, err := exportWrapper.GenerateSbomReport(payload)
if err != nil {
return err
}
return payload, nil
}

func pollForCompletion(exportWrapper wrappers.ExportWrapper, exportID string) (*wrappers.ExportPollingResponse, error) {
timeout := time.After(5 * time.Minute)

Check failure on line 66 in internal/services/export.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 5, in <argument> detected (gomnd)
pollingResp := &wrappers.ExportPollingResponse{ExportStatus: exportingStatus}

log.Println("Generating SBOM report with " + payload.FileFormat + " file format")
pollingResp.ExportStatus = exportingStatus
for pollingResp.ExportStatus == exportingStatus || pollingResp.ExportStatus == pendingStatus {
pollingResp, err = exportWrapper.GetSbomReportStatus(sbomresp.ExportID)
if err != nil {
return errors.Wrapf(err, "%s", "failed getting SBOM report status")
select {
case <-timeout:
return nil, errors.Errorf("SBOM generating failed - Timed out after 5 minutes")
default:
resp, err := exportWrapper.GetSbomReportStatus(exportID)
if err != nil {
return nil, errors.Wrapf(err, "failed getting SBOM report status")
}
pollingResp = resp
time.Sleep(delayValueForReport * time.Second)
}
time.Sleep(delayValueForReport * time.Second)
}

if !strings.EqualFold(pollingResp.ExportStatus, completedStatus) {
return errors.Errorf("SBOM generating failed - Current status: %s", pollingResp.ExportStatus)
return nil, errors.Errorf("SBOM generating failed - Current status: %s", pollingResp.ExportStatus)
}
err = exportWrapper.DownloadSbomReport(pollingResp.ExportID, targetFile)
if err != nil {
return errors.Wrapf(err, "%s", "Failed downloading SBOM report")
}
return nil

return pollingResp, nil
}

func validateSbomOptions(sbomOption string) (string, error) {
Expand Down

0 comments on commit fef3bcc

Please sign in to comment.