Fix error when fetching custom roles when used in policies
Issue Summary
This MR fixes a bug where admin users can't view the Roles and Permissions page in Admin settings when a project has an MR approval policy that:
- Has a custom role assigned as an approver
- Uses a compliance frameworks policy scope
The issue occurs because when trying to fetch roles in this specific scenario, we attempt to access root_ancestor when it's nil, resulting in a NoMethodError: undefined method 'root_ancestor' for nil:NilClass error.
Root Cause
The error happens in SecurityOrchestrationPolicies::PolicyScopeFetcher when:
- The page tries to fetch all custom roles (without a specific container context)
- The fetcher attempts to resolve compliance frameworks from the policy scope
- The code tries to access
root_ancestorwithout first checking if it's nil
Solution
Two key changes:
- Add a guard clause in
root_ancestormethod to return early if container is nil - Modify
compliance_frameworksmethod to query all frameworks when root_ancestor is nil
These changes ensure that even when the container is nil (like when viewing the Admin Roles page), we can still fetch roles that are referenced in security policies with compliance framework scopes.
Testing
Added new test case that covers the scenario where container is nil, verifying the fix works properly.
Related Issues
Fixes Error fetching custom roles when used in projec... (#523100 - closed)