Serve group policy evaluations through Group.policyStore.policyEvaluations

What does this MR do and why?

Summary

Previously, the policy evaluations feature only worked for organizations — groups would always return null. This change extends support so that groups can also query their own policy evaluations through the same GraphQL field. When a group requests its evaluations, it only sees evaluations tied to policies it directly owns (not organization-wide policies or those from sibling groups). The underlying data-fetching logic was generalized to accept any "container" (organization, group, or other), returning the appropriate evaluations for each case. New tests were added to verify the correct behavior for groups, including filtering, pagination, and access control — confirming that a group owner can read their group's evaluations even without organization-level permissions.

Closes #630065

References

Database

Both govern_policy_evaluations and govern_policies are in the gitlab_sec schema, so the join is same-database. No new tables or indexes are added.

The tables are experiment-only, gated behind the policy_store_experiment_enabled setting, and hold zero rows in production today.

First page query (default GraphQL page size 20):

SELECT "govern_policy_evaluations".* FROM "govern_policy_evaluations" INNER JOIN "govern_policies" ON "govern_policies"."id" = "govern_policy_evaluations"."govern_policy_id" WHERE "govern_policies"."namespace_id" = 243 ORDER BY "govern_policy_evaluations"."evaluated_at" DESC, "govern_policy_evaluations"."id" DESC LIMIT 20

count field query:

SELECT COUNT(*) FROM "govern_policy_evaluations" INNER JOIN "govern_policies" ON "govern_policies"."id" = "govern_policy_evaluations"."govern_policy_id" WHERE "govern_policies"."namespace_id" = 243

Keyset pagination adds a row comparison on (evaluated_at, id), and the optional filters (policy, mode, verdict, evaluated range) add predicates on govern_policy_evaluations. Both use the same join and indexes.

Production plan

postgres.ai, gitlab-production-main clone. Returned 0 rows. The planner chose sequential scans because the tables are empty (cost 0.00), so this plan confirms the tables are empty rather than showing index selection. Shared buffers hit: 6. Execution 0.135 ms.

Local plan with seeded data

Local plan on GDK with seeded data (8 policies, 14 evaluations for group 243) shows the intended indexes: index_govern_policies_on_namespace_id for the outer index scan and index_govern_policy_evaluations_on_policy_and_evaluated_at for the inner one.

EXPLAIN (ANALYZE, BUFFERS), GDK
 Limit  (cost=7.45..7.46 rows=3 width=82) (actual time=0.026..0.028 rows=14 loops=1)
   Buffers: shared hit=12
   ->  Sort  (cost=7.45..7.46 rows=3 width=82) (actual time=0.026..0.027 rows=14 loops=1)
         Sort Key: govern_policy_evaluations.evaluated_at DESC, govern_policy_evaluations.id DESC
         Sort Method: quicksort  Memory: 26kB
         ->  Nested Loop  (cost=0.30..7.43 rows=3 width=82) (actual time=0.015..0.019 rows=14 loops=1)
               ->  Index Scan using index_govern_policies_on_namespace_id on govern_policies  (cost=0.15..2.17 rows=1 width=8) (actual time=0.008..0.010 rows=8 loops=1)
                     Index Cond: (namespace_id = 243)
               ->  Index Scan using index_govern_policy_evaluations_on_policy_and_evaluated_at on govern_policy_evaluations  (cost=0.15..5.22 rows=4 width=82) (actual time=0.001..0.001 rows=2 loops=8)
                     Index Cond: (govern_policy_id = govern_policies.id)
 Planning Time: 0.108 ms
 Execution Time: 0.091 ms

The count query has no limit and walks every evaluation of the namespace through the same policy-and-evaluated_at index. With production volume still zero, a seeded Database Lab run can be added on request.

Screenshots or screen recordings

No UI changes.

How to set up and validate locally

  1. Enable the experiment and opt a root group in, as an instance admin in rails console:

    Gitlab::CurrentSettings.update!(policy_store_experiment_enabled: true)
    group = Group.find_by_full_path('<root-group-path>')
    group.namespace_settings.update!(policy_store_experiment_enabled: true)
  2. Create a policy for the group and an evaluation for it:

    policy = Govern::Policy.create!(organization: group.organization, namespace: group, name: 'group policy', trigger_type: :deployment_requested)
    Govern::PolicyEvaluation.create!(policy: policy, organization: group.organization, trigger_type: :deployment_requested, mode: :enforce, verdict: :allow, policy_version: 1, evaluated_at: Time.current)
  3. As an Owner of the group, run in /-/graphql-explorer:

    { group(fullPath: "<root-group-path>") { policyStore { policyEvaluations { count nodes { id policyId verdict evaluatedAt } } } } }

    Expected: the evaluation from step 2, and count: 1. Before this MR the field is null.

  4. Run the same query for an organization-wide policy's evaluation (a policy with namespace: nil): it does not appear under the group but still appears under organization(id:) { policyStore { policyEvaluations } }.

Verification

bundle exec rspec ee/spec/models/govern/policy_evaluation_spec.rb \
  ee/spec/finders/govern/policy_evaluations_finder_spec.rb \
  ee/spec/graphql/resolvers/govern/policy_evaluations_resolver_spec.rb \
  ee/spec/graphql/types/govern/policy_store_type_spec.rb \
  ee/spec/requests/api/graphql/group/policy_store_policy_evaluations_spec.rb \
  ee/spec/requests/api/graphql/organizations/policy_store_policy_evaluations_spec.rb
# 86 examples, 0 failures, 2 pending (the shared granular-token example's instance-boundary branch)

Schema docs regenerated with rake gitlab:graphql:update_all; the diff is the description change only.

Adversarial review (fresh-context reviewer) verdict: pass with findings. Applied: granular-token example now uses the instance boundary the type declares, two stale "authorizes against the organization" comments corrected, group count coverage mirrors the organization spec, sibling-group Owner and not-opted-in group cases added, for_namespace takes an id like Govern::Policy.for_namespace.

Pre-existing, not changed here: GovernPolicyEvaluationType and GovernPolicyType declare an instance-only granular-token boundary, so a group-scoped fine-grained token gets an empty list from both policies and policyEvaluations on a group.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

🤖 Generated with Claude Code

Edited by Artur Fedorov

Merge request reports

Loading
Loading