Skip to content

Commit

Permalink
Response an error message when there is incomplete sbom generate job (g…
Browse files Browse the repository at this point in the history
…oharbor#20526)

Response an error message when there is uncomplete sbom generate job

Signed-off-by: stonezdj <[email protected]>
Co-authored-by: Wang Yan <[email protected]>
  • Loading branch information
stonezdj and wy65701436 authored May 31, 2024
1 parent 7645ec7 commit 30767f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ func (h *scanHandler) delete(ctx context.Context, art *artifact.Artifact, mimeTy
if err != nil {
return err
}
// check if any report has running task associate with it
taskMgr := h.TaskMgrFunc()
for _, rpt := range sbomReports {
if !taskMgr.IsTaskFinished(ctx, rpt.UUID) {
return errors.ConflictError(nil).WithMessage("a previous sbom generate process is running")
}
}

for _, rpt := range sbomReports {
if rpt.MimeType != v1.MimeTypeSBOMReport {
continue
Expand Down
1 change: 1 addition & 0 deletions src/pkg/scan/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func (suite *SBOMTestSuite) TestMakeReportPlaceHolder() {
mock.OnAnything(suite.sbomManager, "Create").Return("uuid", nil).Once()
mock.OnAnything(suite.sbomManager, "Delete").Return(nil).Once()
mock.OnAnything(suite.taskMgr, "ListScanTasksByReportUUID").Return([]*task.Task{{Status: "Success"}}, nil)
mock.OnAnything(suite.taskMgr, "IsTaskFinished").Return(true).Once()
mock.OnAnything(suite.artifactCtl, "Get").Return(art, nil)
mock.OnAnything(suite.artifactCtl, "Delete").Return(nil)
rps, err := suite.handler.MakePlaceHolder(ctx, art, r)
Expand Down
18 changes: 18 additions & 0 deletions src/pkg/task/mock_task_manager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type Manager interface {
ListScanTasksByReportUUID(ctx context.Context, uuid string) (tasks []*Task, err error)
// RetrieveStatusFromTask retrieve status from task
RetrieveStatusFromTask(ctx context.Context, reportID string) string
// IsTaskFinished checks if the scan task is finished by report UUID
IsTaskFinished(ctx context.Context, reportID string) bool
}

// NewManager creates an instance of the default task manager
Expand Down Expand Up @@ -299,3 +301,11 @@ func (m *manager) RetrieveStatusFromTask(ctx context.Context, reportID string) s
}
return ""
}

func (m *manager) IsTaskFinished(ctx context.Context, reportID string) bool {
status := m.RetrieveStatusFromTask(ctx, reportID)
if len(status) == 0 {
return true
}
return job.Status(status).Final()
}
18 changes: 18 additions & 0 deletions src/testing/pkg/task/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30767f6

Please sign in to comment.