Don't create policy violations for mismatching rules
What does this MR do and why?
Merge request approval policies currently create policy violations for rules (for example, because a security scan is missing), even if their policy's branch specification doesn't apply to some merge request. Merge request approval policies can affect the approval settings of merge requests they apply to as through their approval_settings properties. To determine the approval setting policy overrides, we accumulate and reduce all policy violations. As a result, we currently apply policy approval setting overrides for rules even if their policy's branch specification doesn't match a merge request target branch. This MR avoids creating policy violations for rules that don't match the target branch.
References
How to set up and validate locally
Approach A
- Enable the feature flag:
echo "Feature.enable(:merge_request_approval_policies_inapplicable_rule_evaluation)" | rails c
- Create a new project
- Navigate to
Settings > Merge requests > Merge request approvalsand:- uncheck the
Prevent approval by authorcheckbox andSave changes - set the
Approvals requiredof the default ruleMinimum required approvalsto1
- uncheck the
- Navigate to
Security > Policiesand create the following merge request approval policy:
approval_policy:
- name: Test policy
enabled: true
actions:
- type: require_approval
approvals_required: 1
role_approvers:
- owner
- type: send_bot_message
enabled: true
rules:
- type: scan_finding
scanners:
- dependency_scanning
vulnerabilities_allowed: 0
severity_levels:
- high
vulnerability_states:
- new_needs_triage
branch_type: protected
approval_settings:
prevent_approval_by_commit_author: true
- Navigate to
Code > Branchesand create a new branch - Open a merge request targeting the newly created branch and verify you can approve it
- Disable the feature flag:
echo "Feature.disable(:merge_request_approval_policies_inapplicable_rule_evaluation)" | rails c
- Open another merge request targeting the newly created branch and verify you can't approve it
Approach B
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Related to #538938 (closed)