Repository navigation
fix(branch): keep enforcement going when branch protection is unavailable - #893
Conversation
|
The red
Pinning the tool to Everything else is green, and |
|
Sent as offered above: #894 puts |
|
This pull request has been marked stale because it has been open for 10 days with no activity |
There was a problem hiding this comment.
🟡 Changes recommended
The 404 handler for updating existing branch protection lacks a regression test confirming that enforcement continues to the next branch after an update returns 404.
1 open issue
What changed in this PR
Addresses #562 by preventing unavailable classic branch protection from stopping enforcement for subsequent branches and repositories.
Changes:
- Handles 404 responses at both protection-update paths and the signed-commits endpoint by warning and continuing to the next branch.
- Preserves existing 403 handling and propagation of other errors.
- Adds regression tests for branch continuation and server-error propagation.
| File | Description |
|---|---|
pkg/policies/branch/branch.go |
Adds shared 404 detection and warning-and-continue handling at three enforcement call sites. |
pkg/policies/branch/branch_rulesets_test.go |
Tests continuation after protection and signature failures, plus propagation of 500 errors. |
Review effort: Balanced
b37702d to
fd2f2b1
Compare
|
|
||
| // ensureSignedCommits reports whether enforcement can continue for the next branch. | ||
| func ensureSignedCommits(ctx context.Context, rep repositories, owner, repo, b string, required bool) (bool, error) { | ||
| func ensureSignedCommits(ctx context.Context, rep repositories, owner, repo, b string, required bool) (bool, bool, error) { |
| Str("repo", repo). | ||
| Str("area", polName). | ||
| Str("branch", b). | ||
| Msg("Action set to fix, but branch protection is not available for this branch, so signed commits cannot be required on it.") |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package branch |
…able GitHub answers 404 on the classic branch protection endpoints when the setting is not there to be written - a branch that does not exist, or a repository whose protection has moved to rulesets. The Fix action returned that error, which ends the enforcement run for every repository queued behind this one in the same App installation. Treat 404 the way a missing signature setting is already treated: log a warning naming the branch and move on to the next one. 403 handling is unchanged, and any other status is still returned as an error. Fixes ossf#562 Signed-off-by: Eljees <57435526+Eljees@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jeff Mendoza <jlm@jlm.name>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jeff Mendoza <jlm@jlm.name>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jeff Mendoza <jlm@jlm.name>
1a2742b to
992f96a
Compare

Fixes #562.
The failure
PUT /repos/{owner}/{repo}/branches/{branch}/protectionanswers404when the setting is not there to be written. Two ordinary cases produce it: a branch listed inenforceBranchesthat does not exist, and a repository whose protection has been migrated to rulesets, where the classic endpoint is gone.GetBranchProtectionalso returns404for such a branch, sofix()takes the "no existing protection, create from config" path and then hits the404on the write.Only
403was handled there, so the404was returned as an error. That error leavesfix(), and with it the enforcement run: every repository queued behind this one in the same App installation is skipped, which is what #562 reports. A single repository that moved to rulesets stops enforcement for the whole installation.The change
404is now treated the way the same status is already treated for commit signing (getSignatureProtectionEnabled, which has returned "not enabled" on404since it was written): log a warning naming the branch and carry on.403handling is untouched, and any other status is still returned as an error.Three call sites reach the classic protection endpoints per branch, and all three had the same gap:
UpdateBranchProtectionon the create-from-config path,UpdateBranchProtectionon the update-existing path,RequireSignaturesOnProtectedBranch, which is called per branch right after, on protection that is not there either.The warning says the protection is not available for that branch rather than naming rulesets as the cause, since
404does not tell us which of the two cases it was.continuerather thanreturn nil: a branch that cannot take protection says nothing about the next branch inenforceBranches. A repository on rulesets will simply skip all of them in turn.Tests
pkg/policies/branch/branch_rulesets_test.go:TestFixContinuesWhenBranchProtectionIsNotAvailable— the first branch answers404on the write, and the test asserts the run reached the following branch. Asserting only thatFixreturned no error would also pass if the loop had quietly stopped after the first branch, so the assertion is on the call that has to happen, not on the absence of an error.TestFixContinuesWhenSignedCommitsCannotBeRequired— the same for the signed-commits call.TestFixStillFailsOnOtherErrors— a500is still reported, so a broken installation does not start looking healthy.I checked the failure direction by reverting only
branch.goand keeping the tests: both continuation tests fail with404 Not Found, the500test still passes, and the rest of the package is unaffected.gofmt,go vetandgo test ./pkg/policies/branch/are clean on Go 1.26.7.Relation to #864
@pujitha24 opened #864 for the same issue in July; it was closed by the stale bot last week without a human review. This change differs in three ways, two of them from the automated review left on that PR: it continues to the next branch instead of returning, it does not state rulesets as the cause in the log message, and it also covers
RequireSignaturesOnProtectedBranch, which is the third place the same404arrives.