List the runs in an organization, optionally filtered out by workflow
(ctx context.Context, orgID uuid.UUID, filters *biz.RunListFilters, p *pagination.CursorOptions)
| 433 | |
| 434 | // List the runs in an organization, optionally filtered out by workflow |
| 435 | func (r *WorkflowRunRepo) List(ctx context.Context, orgID uuid.UUID, filters *biz.RunListFilters, p *pagination.CursorOptions) (result []*biz.WorkflowRun, cursor string, err error) { |
| 436 | ctx, span := otelx.Start(ctx, workflowRunRepoTracer, "WorkflowRunRepo.List") |
| 437 | defer span.End() |
| 438 | |
| 439 | if p == nil { |
| 440 | return nil, "", errors.New("pagination options is required") |
| 441 | } |
| 442 | |
| 443 | // Org-scoped query uses the denormalized organization_id column for a |
| 444 | // sargable range scan via the (organization_id, created_at DESC) index. |
| 445 | // The prior HasWorkflowWith form compiled to a correlated EXISTS that |
| 446 | // let the planner pick a bad ORDER BY created_at DESC plan. The |
| 447 | // deleted_at filter still goes via the workflows edge: it's a per-row |
| 448 | // PK lookup applied after the index narrows the candidate set, so it |
| 449 | // doesn't reintroduce the planner ambiguity. |
| 450 | wfPredicates := []predicate.Workflow{workflow.DeletedAtIsNil()} |
| 451 | if filters != nil && filters.ProjectIDs != nil { |
| 452 | wfPredicates = append(wfPredicates, workflow.ProjectIDIn(filters.ProjectIDs...)) |
| 453 | } |
| 454 | |
| 455 | q := r.data.DB.WorkflowRun.Query(). |
| 456 | Where( |
| 457 | workflowrun.OrganizationID(orgID), |
| 458 | workflowrun.HasWorkflowWith(wfPredicates...), |
| 459 | ). |
| 460 | Order(ent.Desc(workflowrun.FieldCreatedAt)). |
| 461 | WithWorkflowAndProject().WithVersion(). |
| 462 | Limit(p.Limit + 1) |
| 463 | |
| 464 | // Append the workflow filter if present |
| 465 | if filters != nil && filters.WorkflowID != nil { |
| 466 | q = q.Where(workflowrun.HasWorkflowWith(workflow.ID(*filters.WorkflowID))) |
| 467 | } |
| 468 | |
| 469 | // Append the state filter if present, i.e only running |
| 470 | if filters != nil && filters.Status != "" { |
| 471 | q = q.Where(workflowrun.StateEQ(filters.Status)) |
| 472 | } |
| 473 | |
| 474 | // or the project version |
| 475 | if filters != nil && filters.VersionID != nil { |
| 476 | q = q.Where(workflowrun.VersionID(*filters.VersionID)) |
| 477 | } |
| 478 | |
| 479 | // Canonical policy status filter takes precedence over the legacy |
| 480 | // violations-only filter. Only one is applied to avoid contradictory |
| 481 | // predicates when a client sends both. |
| 482 | switch { |
| 483 | case filters != nil && filters.PolicyStatus != nil: |
| 484 | entStatus, err := policyStatusToEnt(*filters.PolicyStatus) |
| 485 | if err != nil { |
| 486 | return nil, "", err |
| 487 | } |
| 488 | q = q.Where(workflowrun.PolicyStatusEQ(entStatus)) |
| 489 | case filters != nil && filters.PolicyViolationsFilter != nil: |
| 490 | if *filters.PolicyViolationsFilter { |
| 491 | q = q.Where(workflowrun.HasPolicyViolations(true)) |
| 492 | } else { |
nothing calls this directly
no test coverage detected