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

  1. Enable the feature flag:
echo "Feature.enable(:merge_request_approval_policies_inapplicable_rule_evaluation)" | rails c
  1. Create a new project
  2. Navigate to Settings > Merge requests > Merge request approvals and:
    • uncheck the Prevent approval by author checkbox and Save changes
    • set the Approvals required of the default rule Minimum required approvals to 1
  3. Navigate to Security > Policies and 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
  1. Navigate to Code > Branches and create a new branch
  2. Open a merge request targeting the newly created branch and verify you can approve it
  3. Disable the feature flag:
echo "Feature.disable(:merge_request_approval_policies_inapplicable_rule_evaluation)" | rails c
  1. Open another merge request targeting the newly created branch and verify you can't approve it

Approach B

!197162 (comment 2626043111)

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)

Edited by Dominic Bauer

Merge request reports

Loading