Skip to content

Commit

Permalink
feat: SP-1571 Add capability to add comments when filtering a file/co…
Browse files Browse the repository at this point in the history
…mponent (#53)

* feat: SP-1571 Add InputPromptDialog component, ask for comments before filtering components

* feat: SP-1571 Reset input value on confirm/cancel

* feat: SP-1571 Use 'comment' instead of 'comments' to align with json schema

* feat: SP-1571 Add keyboard shortcut, display comment tooltip

* feat: SP-1571 Show comment in match info card
  • Loading branch information
matiasdaloia authored Oct 3, 2024
1 parent 825bbb0 commit 8905267
Show file tree
Hide file tree
Showing 23 changed files with 1,068 additions and 937 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const (
)

type ComponentFilter struct {
Path string `json:"path,omitempty"`
Purl string `json:"purl"`
Usage ComponentFilterUsage `json:"usage,omitempty"`
Path string `json:"path,omitempty"`
Purl string `json:"purl"`
Usage ComponentFilterUsage `json:"usage,omitempty"`
Comment string `json:"comment,omitempty"`
}

func (sf *SettingsFile) Equal(other *SettingsFile) bool {
Expand All @@ -52,48 +53,58 @@ func (sf *SettingsFile) GetResultWorkflowState(result entities.Result) entities.
return entities.Pending
}

func (sf *SettingsFile) IsResultIncluded(result entities.Result) (bool, entities.FilterType) {
func (sf *SettingsFile) IsResultIncluded(result entities.Result) (bool, int) {
return sf.IsResultInList(result, sf.Bom.Include)
}

func (sf *SettingsFile) IsResultRemoved(result entities.Result) (bool, entities.FilterType) {
func (sf *SettingsFile) IsResultRemoved(result entities.Result) (bool, int) {
return sf.IsResultInList(result, sf.Bom.Remove)
}

func (sf *SettingsFile) IsResultInList(result entities.Result, list []ComponentFilter) (bool, entities.FilterType) {
func (sf *SettingsFile) IsResultInList(result entities.Result, list []ComponentFilter) (bool, int) {
i := slices.IndexFunc(list, func(cf ComponentFilter) bool {
if cf.Path != "" && cf.Purl != "" {
return cf.Path == result.Path && slices.Contains(result.Purl, cf.Purl)
}
return slices.Contains(result.Purl, cf.Purl)
})

if i != -1 {
cf := list[i]
if cf.Path != "" && cf.Purl != "" {
return true, entities.ByFile
}
return true, entities.ByPurl
}

return false, entities.ByPurl
return i != -1, i
}

func (sf *SettingsFile) GetResultFilterConfig(result entities.Result) entities.FilterConfig {
var filterAction entities.FilterAction
var filterType entities.FilterType

if included, t := sf.IsResultIncluded(result); included {
if included, i := sf.IsResultIncluded(result); included {
filterAction = entities.Include
filterType = t
} else if removed, t := sf.IsResultRemoved(result); removed {
filterType = getResultFilterType(sf.Bom.Include[i])
} else if removed, i := sf.IsResultRemoved(result); removed {
filterAction = entities.Remove
filterType = t

filterType = getResultFilterType(sf.Bom.Remove[i])
}

return entities.FilterConfig{
Action: filterAction,
Type: filterType,
}
}

func getResultFilterType(cf ComponentFilter) entities.FilterType {
if cf.Path != "" && cf.Purl != "" {
return entities.ByFile
}
return entities.ByPurl
}

func (sf *SettingsFile) GetResultComment(result entities.Result) string {
if included, i := sf.IsResultIncluded(result); included {
return sf.Bom.Include[i].Comment
}

if removed, i := sf.IsResultRemoved(result); removed {
return sf.Bom.Remove[i].Comment
}

return ""
}
11 changes: 6 additions & 5 deletions backend/main/pkg/component/entities/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ComponentDTO struct {
FileHash string `json:"file_hash,omitempty"`
SourceHash string `json:"source_hash,omitempty"`
FileURL string `json:"file_url,omitempty"`
Purl []string `json:"purl,omitempty"`
Purl []string `json:"purl"`
Vendor string `json:"vendor,omitempty"`
Component string `json:"component,omitempty"`
Version string `json:"version,omitempty"`
Expand Down Expand Up @@ -68,8 +68,9 @@ const (
)

type ComponentFilterDTO struct {
Path string `json:"path,omitempty"`
Purl string `json:"purl" validate:"required"`
Usage string `json:"usage,omitempty"`
Action FilterAction `json:"action" validate:"required,eq=include|eq=remove"`
Path string `json:"path,omitempty"`
Purl string `json:"purl" validate:"required"`
Usage string `json:"usage,omitempty"`
Action FilterAction `json:"action" validate:"required,eq=include|eq=remove"`
Comment string `json:"comment,omitempty"`
}
7 changes: 4 additions & 3 deletions backend/main/pkg/component/service/component_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func (s *ComponentServiceImpl) GetComponentByFilePath(filePath string) (entities

func (s *ComponentServiceImpl) FilterComponent(dto entities.ComponentFilterDTO) error {
newFilter := &scanossSettingsEntities.ComponentFilter{
Path: dto.Path,
Purl: dto.Purl,
Usage: scanossSettingsEntities.ComponentFilterUsage(dto.Usage),
Path: dto.Path,
Purl: dto.Purl,
Usage: scanossSettingsEntities.ComponentFilterUsage(dto.Usage),
Comment: dto.Comment,
}
if err := s.scanossSettingsRepo.AddBomEntry(*newFilter, string(dto.Action)); err != nil {
fmt.Printf("error adding bom entry: %s", err)
Expand Down
1 change: 1 addition & 0 deletions backend/main/pkg/result/entities/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ResultDTO struct {
MatchType MatchType `json:"match_type"`
WorkflowState WorkflowState `json:"workflow_state,omitempty"`
FilterConfig FilterConfig `json:"filter_config,omitempty"`
Comment string `json:"comment,omitempty"`
}

type RequestResultDTO struct {
Expand Down
5 changes: 5 additions & 0 deletions backend/main/pkg/result/mappers/result_mapper_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ func (m ResultMapperImpl) MapToResultDTO(result entities.Result) entities.Result
Path: result.Path,
WorkflowState: m.mapWorkflowState(result),
FilterConfig: m.mapFilterConfig(result),
Comment: m.mapComment(result),
}
}

func (m ResultMapperImpl) mapComment(result entities.Result) string {
return m.scanossSettings.SettingsFile.GetResultComment(result)
}

func (m ResultMapperImpl) MapToResultDTOList(results []entities.Result) []entities.ResultDTO {
output := make([]entities.ResultDTO, len(results))

Expand Down
Loading

0 comments on commit 8905267

Please sign in to comment.