Skip to content

Commit

Permalink
Enforce defaults in deps ingest pull request configuration (#5226)
Browse files Browse the repository at this point in the history
If the user would forget to set the parameters, the deps ingest would
fail saying that the filter is invalid. This is not ideal for the user
since we should just set reasonable defaults.

This fixes that!

We now default to `new_and_updated`  if no filter configuration is set.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Dec 19, 2024
1 parent 9c86370 commit 0068f4d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions internal/engine/ingester/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ type PullRequestConfig struct {
Filter string `json:"filter" yaml:"filter" mapstructure:"filter"`
}

const (
// PullRequestIngestTypeNew is a filter that exposes only new dependencies in the pull request
PullRequestIngestTypeNew = "new"
// PullRequestIngestTypeNewAndUpdated is a filter that exposes new and updated
// dependencies in the pull request
PullRequestIngestTypeNewAndUpdated = "new_and_updated"
// PullRequestIngestTypeAll is a filter that exposes all dependencies in the pull request
PullRequestIngestTypeAll = "all"
)

// NewDepsIngester creates a new deps rule data ingest engine
func NewDepsIngester(cfg *pb.DepsType, gitprov provifv1.Git) (*Deps, error) {
if gitprov == nil {
Expand Down Expand Up @@ -151,13 +161,13 @@ func (gi *Deps) getBranch(repo *pb.Repository, userConfigBranch string) string {
// ingestTypes returns a sorter function for the given filter type.
// items which compare equal are skipped in output.
var ingestTypes = map[string]func(*sbom.Node, *sbom.Node) int{
"new": func(base *sbom.Node, updated *sbom.Node) int {
PullRequestIngestTypeNew: func(base *sbom.Node, updated *sbom.Node) int {
return cmp.Compare(base.GetName(), updated.GetName())
},
"new_and_updated": func(base *sbom.Node, updated *sbom.Node) int {
PullRequestIngestTypeNewAndUpdated: func(base *sbom.Node, updated *sbom.Node) int {
return nodeSorter(base, updated)
},
"all": func(_ *sbom.Node, _ *sbom.Node) int {
PullRequestIngestTypeAll: func(_ *sbom.Node, _ *sbom.Node) int {
return -1
},
}
Expand Down Expand Up @@ -223,10 +233,20 @@ func filterNodes(base []*sbom.Node, updated []*sbom.Node, compare func(*sbom.Nod

func (gi *Deps) ingestPullRequest(
ctx context.Context, pr *pbinternal.PullRequest, params map[string]any) (*interfaces.Result, error) {
userCfg := &PullRequestConfig{}
userCfg := &PullRequestConfig{
// We default to new_and_updated for user convenience.
Filter: PullRequestIngestTypeNewAndUpdated,
}
if err := mapstructure.Decode(params, userCfg); err != nil {
return nil, fmt.Errorf("failed to read dependency ingester configuration from params: %w", err)
}

// Enforce that the filter is valid if left empty.
if userCfg.Filter == "" {
userCfg.Filter = PullRequestIngestTypeNewAndUpdated
}

// At this point the user really set a wrong configuration. So, let's error out.
if _, ok := ingestTypes[userCfg.Filter]; !ok {
return nil, fmt.Errorf("invalid filter type: %s", userCfg.Filter)
}
Expand Down

0 comments on commit 0068f4d

Please sign in to comment.