-
Notifications
You must be signed in to change notification settings - Fork 132
branchprotector: remove protection from excluded branches #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
branchprotector: remove protection from excluded branches #478
Conversation
✅ Deploy Preview for k8s-prow ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Fix issue where excluded branches retain existing protection instead of being removed. When a branch is added to the exclude list in branchprotector configuration, the tool correctly stops applying new protection rules but does not remove existing protection from branches that were previously protected. This change adds logic to detect excluded branches that are currently protected and queue them for removal by sending requirements with Request: nil, which triggers RemoveBranchProtection() in the configureBranches() function. The fix prevents push failures like: remote: error: GH006: Protected branch update failed for refs/heads/konflux-branch remote: - Changes must be made through a pull request. Changes: - Add detection logic for excluded protected branches in UpdateRepo() - Send removal requests (Request: nil) for such branches - Update tests to expect removal requests for excluded protected branches - Add code comment explaining the call flow to RemoveBranchProtection Fixes kubernetes-sigs#477 Signed-off-by: Tiger Kaovilai <passawit.kaovilai@gmail.com>
2f7aa72
to
f1b83a2
Compare
Hi @kaovilai. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: kaovilai The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/ok-to-test |
@@ -346,6 +348,27 @@ func (p *protector) UpdateRepo(orgName string, repoName string, repo config.Repo | |||
} | |||
} | |||
|
|||
// Handle excluded branches that are currently protected and need removal | |||
if branchExclusions != nil { | |||
seen := make(map[string]bool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this map needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Problem
The allBranches slice can contain duplicate entries for the same branch because it's populated from two separate API calls:
for _, onlyProtected := range []bool{false, true} { // runs twice
bs, err := p.client.GetBranches(orgName, repoName, onlyProtected)
allBranches = append(allBranches, bs...) // can add same branch twice
}
Without Deduplication
If a branch like konflux-test appears in both API responses, we would send two removal requests:
- First iteration: {Org: "org", Repo: "repo", Branch: "konflux-test", Request: nil}
- Second iteration: {Org: "org", Repo: "repo", Branch: "konflux-test", Request: nil}
With seen Map
if b.Protected && branchExclusions.MatchString(b.Name) && !seen[b.Name] {
seen[b.Name] = true // Mark as processed
// Send removal request only once
}
The seen map ensures each excluded protected branch gets exactly one removal request, regardless of how many times it appears in allBranches.
This prevents:
- Duplicate API calls to GitHub
- Confusing log messages
- Potential race conditions in the removal process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps if p.client.GetBranches(orgName, repoName, false) return all branches we can initialize allBranches once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, get it. Thanks!
/lgtm |
Fix issue where excluded branches retain existing protection instead of being removed.
When a branch is added to the exclude list in branchprotector configuration, the tool
correctly stops applying new protection rules but does not remove existing protection
from branches that were previously protected.
This change adds logic to detect excluded branches that are currently protected and
queue them for removal by sending requirements with Request: nil, which triggers
RemoveBranchProtection() in the configureBranches() function.
The fix prevents push failures like:
remote: error: GH006: Protected branch update failed for refs/heads/konflux-branch
remote: - Changes must be made through a pull request.
Changes:
Fixes #477