Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] fix 20518 #20522

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,15 @@
}

func (h *scanHandler) MakePlaceHolder(ctx context.Context, art *artifact.Artifact, r *scanner.Registration) (rps []*scanModel.Report, err error) {
var reports []*scanModel.Report
mgr := h.SBOMMgrFunc()
mimeTypes := r.GetProducesMimeTypes(art.ManifestMediaType, v1.ScanTypeSbom)
if len(mimeTypes) == 0 {
return nil, errors.New("no mime types to make report placeholders")
}
sbomReports, err := mgr.GetBy(h.cloneCtx(ctx), art.ID, r.UUID, mimeTypes[0], sbomMediaTypeSpdx)
if err != nil {
return nil, err
}
if err := h.deleteSBOMAccessories(ctx, sbomReports); err != nil {
if err := h.delete(ctx, art, mimeTypes[0], r); err != nil {
return nil, err
}
var reports []*scanModel.Report
for _, mt := range mimeTypes {
report := &sbom.Report{
ArtifactID: art.ID,
Expand Down Expand Up @@ -241,20 +237,24 @@
return reports, nil
}

// deleteSBOMAccessories delete the sbom accessory in reports
func (h *scanHandler) deleteSBOMAccessories(ctx context.Context, reports []*sbom.Report) error {
// delete deletes the sbom report and accessory
func (h *scanHandler) delete(ctx context.Context, art *artifact.Artifact, mimeTypes string, r *scanner.Registration) error {
mgr := h.SBOMMgrFunc()
for _, rpt := range reports {
sbomReports, err := mgr.GetBy(h.cloneCtx(ctx), art.ID, r.UUID, mimeTypes, sbomMediaTypeSpdx)
if err != nil {
return err
}

Check warning on line 246 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L245-L246

Added lines #L245 - L246 were not covered by tests
for _, rpt := range sbomReports {
if rpt.MimeType != v1.MimeTypeSBOMReport {
continue
}
if err := h.deleteSBOMAccessory(ctx, rpt.ArtifactID); err != nil {
return err
}
if err := mgr.Delete(ctx, rpt.UUID); err != nil {
return err
}
}
if err := h.deleteSBOMAccessory(ctx, art.ID); err != nil {
return err
}

Check warning on line 257 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L256-L257

Added lines #L256 - L257 were not covered by tests
return nil
}

Expand Down
Loading