Add service to revert automatic approval policy bypasses
What does this MR do and why?
Adds Security::ScanResultPolicies::RevertAutomaticBypassMergeRequestService, which reverts the automatic approval policy bypasses of a merge request. It is the counterpart of AutomaticBypassMergeRequestService from !273914 (merged): an automatic bypass is recorded against a merge request diff, so when the merge request changes the bypass must stop applying.
Nothing calls the service yet. Hooking it into pushes follows in a later MR. It only runs when the merge_request_approval_policy_auto_bypass feature flag (wip, default off) is enabled and the project has the security_orchestration_policies license. Otherwise it returns an error with reason :not_available.
The service:
- Deletes every automatic bypass event of the merge request, one per bypassed policy, in a single
DELETE ... RETURNINGstatement (delete_all_returning). Automatic events are the ones with amerge_request_diff_id. Manual bypasses (merge_request_diff_idisNULL) are kept. - Builds the audit events and the response from the returned rows, so a concurrent revert cannot audit the same bypass twice.
- Logs a new
security_policy_merge_request_automatic_bypass_revertedaudit event per reverted policy, authored by the current user. On a push this is the pusher. - Triggers the
mergeRequestApprovalStateUpdatedandmergeRequestMergeStatusUpdatedsubscriptions. - Returns an error with reason
:no_automatic_bypasseswhen there is nothing to revert. The caller decides how to handle that.
It also adds an automatic scope to Security::ApprovalPolicyMergeRequestBypassEvent, so callers do not need to know that a NULL diff marks a manual bypass.
References
- Feature issue: #579429
- Sibling MR adding the automatic bypass: !273914 (merged)
- Depends on the
merge_request_diff_idcolumn: !273613 (merged). Until it merges, this MR's diff also contains that MR's commits. Review only the last commits, starting at "Add service to revert automatic approval policy bypasses".
Screenshots or screen recordings
Not applicable. This MR has no UI changes.
How to set up and validate locally
This MR has no user-facing change: nothing calls the service yet. I validated it in GDK on a merge request authored by a service account, with one approval policy whose bypass_settings lists that service account, and an automatic bypass event recorded against the latest merge request diff. Run these in a Rails console:
mr = Project.find_by_full_path('your/project').merge_requests.find_by(iid: 1)
bot = User.find_by_username('your-service-account')
revert = -> { Security::ScanResultPolicies::RevertAutomaticBypassMergeRequestService.new(merge_request: mr, current_user: bot).execute }- With the feature flag disabled,
revert.call.reasonreturns:not_availableand the bypass event remains. - Enable the feature flag with
Feature.enable(:merge_request_approval_policy_auto_bypass, mr.project).revert.callreturns a success listing the policy, and the bypass event is deleted. - Reload the merge request page. The policy requires approval again.
- The security policy project's audit events page shows "Automatic bypass of security policy
<policy>in merge request (<project>!<iid>) has been reverted", with themerge_request_diff_idof the reverted bypass in its details. - Run
revert.callagain. It returns an error with reason:no_automatic_bypasses. - Record a manual bypass for the policy (
merge_request_diff_id: nil) and runrevert.call. It returns:no_automatic_bypassesand the manual bypass remains.
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.