feat: Block if branch deletion is enabled #644
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Block PR Merges When Auto Branch Deletion is Enabled
Summary
This PR adds support for detecting and blocking pull request merges when SCM providers have automatic branch deletion enabled. This prevents conflicts with gitops-promoter's branch lifecycle management.
Problem
When SCM providers are configured to automatically delete branches after PR merge, it interferes with gitops-promoter's ability to manage the promotion workflow. The promoter needs to control when branches are deleted to maintain the integrity of the GitOps promotion chain.
Solution
Added a new
HasAutoBranchDeletionEnabled()method to thePullRequestProviderinterface that checks if automatic branch deletion is configured. When enabled, the controller refuses to merge PRs and emits a clear error message and Kubernetes event.Provider-Specific Implementation
GitHub:
delete_branch_on_mergerepository setting via the Repositories APIallow_deletions: false) are safe from auto-deletionGitLab:
remove_source_branch_after_mergeproject setting via the Projects APIForgejo: Controls branch deletion directly via
DeleteBranchAfterMerge: falsein merge options (always safe)Fake: Returns
falsefor testing purposesChanges
Modified Files
internal/scms/pullrequest.go- AddedHasAutoBranchDeletionEnabledmethod to interfaceinternal/scms/github/pullrequest.go- Implemented GitHub provider checkinternal/scms/gitlab/pullrequest.go- Implemented GitLab provider checkinternal/scms/forgejo/pullrequest.go- Implemented Forgejo provider (returns false with explanation)internal/scms/fake/pullrequest.go- Implemented fake provider for testinginternal/controller/pullrequest_controller.go- Added merge blocking logicinternal/types/constants/events.go- Added new event constants for blocked mergesinternal/scms/mock/mock_PullRequestProvider.go- Regenerated mocksBehavior
When auto-deletion is disabled (default):
When auto-deletion is enabled:
"Pull Request {name} merge blocked: repository has automatic branch deletion enabled. This setting must be disabled to allow gitops-promoter to manage branch lifecycle"PullRequestMergeBlockedTesting
Permissions
No new permissions required ✅
Contents: Read and writepermission already covers:delete_branch_on_merge)apiscope already covers:remove_source_branch_after_merge)Breaking Changes
None. This is a backward-compatible enhancement that adds safety checks.
Migration Notes
None required. Users with automatic branch deletion enabled will see merge failures with clear error messages instructing them to disable the setting.