Skip to content

Commit 0068f4d

Browse files
authored
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 <[email protected]>
1 parent 9c86370 commit 0068f4d

File tree

1 file changed

+24
-4
lines changed
  • internal/engine/ingester/deps

1 file changed

+24
-4
lines changed

internal/engine/ingester/deps/deps.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ type PullRequestConfig struct {
5151
Filter string `json:"filter" yaml:"filter" mapstructure:"filter"`
5252
}
5353

54+
const (
55+
// PullRequestIngestTypeNew is a filter that exposes only new dependencies in the pull request
56+
PullRequestIngestTypeNew = "new"
57+
// PullRequestIngestTypeNewAndUpdated is a filter that exposes new and updated
58+
// dependencies in the pull request
59+
PullRequestIngestTypeNewAndUpdated = "new_and_updated"
60+
// PullRequestIngestTypeAll is a filter that exposes all dependencies in the pull request
61+
PullRequestIngestTypeAll = "all"
62+
)
63+
5464
// NewDepsIngester creates a new deps rule data ingest engine
5565
func NewDepsIngester(cfg *pb.DepsType, gitprov provifv1.Git) (*Deps, error) {
5666
if gitprov == nil {
@@ -151,13 +161,13 @@ func (gi *Deps) getBranch(repo *pb.Repository, userConfigBranch string) string {
151161
// ingestTypes returns a sorter function for the given filter type.
152162
// items which compare equal are skipped in output.
153163
var ingestTypes = map[string]func(*sbom.Node, *sbom.Node) int{
154-
"new": func(base *sbom.Node, updated *sbom.Node) int {
164+
PullRequestIngestTypeNew: func(base *sbom.Node, updated *sbom.Node) int {
155165
return cmp.Compare(base.GetName(), updated.GetName())
156166
},
157-
"new_and_updated": func(base *sbom.Node, updated *sbom.Node) int {
167+
PullRequestIngestTypeNewAndUpdated: func(base *sbom.Node, updated *sbom.Node) int {
158168
return nodeSorter(base, updated)
159169
},
160-
"all": func(_ *sbom.Node, _ *sbom.Node) int {
170+
PullRequestIngestTypeAll: func(_ *sbom.Node, _ *sbom.Node) int {
161171
return -1
162172
},
163173
}
@@ -223,10 +233,20 @@ func filterNodes(base []*sbom.Node, updated []*sbom.Node, compare func(*sbom.Nod
223233

224234
func (gi *Deps) ingestPullRequest(
225235
ctx context.Context, pr *pbinternal.PullRequest, params map[string]any) (*interfaces.Result, error) {
226-
userCfg := &PullRequestConfig{}
236+
userCfg := &PullRequestConfig{
237+
// We default to new_and_updated for user convenience.
238+
Filter: PullRequestIngestTypeNewAndUpdated,
239+
}
227240
if err := mapstructure.Decode(params, userCfg); err != nil {
228241
return nil, fmt.Errorf("failed to read dependency ingester configuration from params: %w", err)
229242
}
243+
244+
// Enforce that the filter is valid if left empty.
245+
if userCfg.Filter == "" {
246+
userCfg.Filter = PullRequestIngestTypeNewAndUpdated
247+
}
248+
249+
// At this point the user really set a wrong configuration. So, let's error out.
230250
if _, ok := ingestTypes[userCfg.Filter]; !ok {
231251
return nil, fmt.Errorf("invalid filter type: %s", userCfg.Filter)
232252
}

0 commit comments

Comments
 (0)