From 0068f4d7452e8db6d5e4fcea9442cf7e58377e3d Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 19 Dec 2024 13:34:50 +0200 Subject: [PATCH] Enforce defaults in `deps` ingest pull request configuration (#5226) 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 --- internal/engine/ingester/deps/deps.go | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/engine/ingester/deps/deps.go b/internal/engine/ingester/deps/deps.go index 50b019f958..7ab05e46b1 100644 --- a/internal/engine/ingester/deps/deps.go +++ b/internal/engine/ingester/deps/deps.go @@ -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 { @@ -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 }, } @@ -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) }