MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / applyWorkflowFilters

Function applyWorkflowFilters

app/controlplane/pkg/data/workflow.go:345–408  ·  view source on GitHub ↗

applyWorkflowFilters applies filters to the Workflow query based on the provided options

(wfQuery *ent.WorkflowQuery, opts *biz.WorkflowListOpts)

Source from the content-addressed store, hash-verified

343
344// applyWorkflowFilters applies filters to the Workflow query based on the provided options
345func applyWorkflowFilters(wfQuery *ent.WorkflowQuery, opts *biz.WorkflowListOpts) (*ent.WorkflowQuery, error) {
346 if opts != nil {
347 if opts.WorkflowPublic != nil {
348 wfQuery = wfQuery.Where(workflow.Public(*opts.WorkflowPublic))
349 }
350
351 if opts.ProjectIDs != nil {
352 wfQuery = wfQuery.Where(workflow.ProjectIDIn(opts.ProjectIDs...))
353 }
354
355 // Updated at on Workflows is only updated when a new workflow run is referenced meaning
356 // a workflow run is started
357 if opts.WorkflowActiveWindow != nil {
358 wfQuery = wfQuery.Where(
359 workflow.UpdatedAtGTE(opts.WorkflowActiveWindow.From),
360 workflow.UpdatedAtLTE(opts.WorkflowActiveWindow.To),
361 )
362 }
363
364 if len(opts.WorkflowProjectNames) != 0 {
365 wfQuery = wfQuery.Where(workflow.HasProjectWith(project.NameIn(opts.WorkflowProjectNames...)))
366 }
367
368 // Append the JSON Filters to the query
369 if len(opts.JSONFilters) != 0 {
370 // Build the predicates for each JSON filter up front so that any
371 // validation error (e.g. an unsafe field path) is surfaced instead
372 // of being silently ignored inside the query builder closure.
373 predicates := make([]*sql.Predicate, 0, len(opts.JSONFilters))
374 for _, filter := range opts.JSONFilters {
375 // Include the column where the filter is applied
376 filter.Column = workflow.FieldMetadata
377 jsonPredicate, err := jsonfilter.BuildEntSelectorFromJSONFilter(filter)
378 if err != nil {
379 return nil, biz.NewErrValidation(fmt.Errorf("invalid JSON filter: %w", err))
380 }
381 predicates = append(predicates, jsonPredicate)
382 }
383 wfQuery = wfQuery.Where(func(selector *sql.Selector) {
384 // Combine the predicates using AND logic
385 selector.Where(sql.And(predicates...))
386 })
387 }
388
389 // Combine WorkflowTeam and WorkflowName filters using OR logic
390 var orConditions []predicate.Workflow
391 if opts.WorkflowTeam != "" {
392 orConditions = append(orConditions, workflow.TeamContains(opts.WorkflowTeam))
393 }
394 if opts.WorkflowName != "" {
395 orConditions = append(orConditions, workflow.NameContains(opts.WorkflowName))
396 }
397
398 if opts.WorkflowDescription != "" {
399 orConditions = append(orConditions, workflow.DescriptionContains(opts.WorkflowDescription))
400 }
401
402 if len(orConditions) > 0 {

Callers 1

ListMethod · 0.85

Calls 13

PublicFunction · 0.92
ProjectIDInFunction · 0.92
UpdatedAtGTEFunction · 0.92
UpdatedAtLTEFunction · 0.92
HasProjectWithFunction · 0.92
NameInFunction · 0.92
NewErrValidationFunction · 0.92
TeamContainsFunction · 0.92
NameContainsFunction · 0.92
DescriptionContainsFunction · 0.92
OrFunction · 0.92

Tested by

no test coverage detected