Skip to content

fix(remediation): use PATCH for review requirements on protected branches (fixes #546) - #810

Open
Daniel-park10 wants to merge 1 commit into
darnitdevorg:mainfrom
Daniel-park10:fix/546-patch-pull-request-reviews
Open

Daniel-park10 wants to merge 1 commit into
darnitdevorg:mainfrom
Daniel-park10:fix/546-patch-pull-request-reviews

Conversation

@Daniel-park10

@Daniel-park10 Daniel-park10 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Verified against the live GitHub REST API that PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews enables pull request review requirements on a classic-protected branch without them (returning HTTP 200 and preserving existing protection settings, unlike required_status_checks which returns 404).

Updated _plan_branch in packages/darnit/src/darnit/remediation/platform/targets.py so missing review requirements use PATCH .../required_pull_request_reviews directly when no other requirement needs a full PUT. When another requirement already emits a full PUT, fold_reviews folds the review change and its FieldChange into that PUT so only a single write is sent. Updated docs/architecture/framework-design.md (Section 4.5, 1.0.0-alpha.34), CHANGELOG.md, SimulatedGitHub in conftest.py, and the branch protection tests.

Fixes #546.

Type of Change

  • Bug fix (non-breaking change fixing an issue)
  • New feature (non-breaking change adding functionality)
  • Breaking change (fix or feature causing existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)

Framework Changes Checklist

If this PR modifies the darnit framework (packages/darnit/):

  • Updated framework spec (docs/architecture/framework-design.md) if behavior changed
  • Ran uv run python scripts/validate_sync.py --verbose and it passes

Control/TOML Changes Checklist

If this PR modifies controls or TOML configuration:

  • Control metadata defined in TOML (not Python code)
  • SARIF fields (description, severity, help_url) included where appropriate
  • Ran validation to confirm TOML schema compliance

Testing

  • Tests pass locally (uv run pytest tests/darnit/remediation/platform/ tests/darnit_baseline/test_branch_protection_handler.py tests/darnit_baseline/test_enable_branch_protection.py -v)
  • Added tests for new functionality (if applicable)
  • Linting passes (uv run ruff check .)

Verified PATCH .../required_pull_request_reviews on a disposable repository with classic branch protection (required_approving_review_count set to 1 and 0).

AI assistance

  • No AI assistance was used
  • AI assistance was used (Gemini was used for review/verification assistance only; no code was written by AI, and all changes, tests, and live API verification results were reviewed and verified by me)

Additional Notes

None.

@mlieberman85 mlieberman85 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Daniel-park10. This PR hinges on one fact about GitHub: that PATCH .../required_pull_request_reviews turns reviews on for a protected branch that has none. That's exactly what #546 asks to check, and the recorded tests can't show it, because the simulated GitHub in conftest.py is changed here to accept the PATCH. GitHub's documentation doesn't say either way, and the status-checks endpoints of the same API answer 404 when their sub-protection is off.

If the PATCH is rejected, nothing is weakened. The read-back catches it, as #546 says. But AC-03.01 and QA-07.01 would then stop being fixed on those branches, where main fixes them today through the full PUT. So I'd like the evidence before approving. Could you add the transcript from your live check, with tokens and the repository name redacted as you like?

  1. GET .../branches/{b}/protection before, showing no required_pull_request_reviews.
  2. The PATCH .../required_pull_request_reviews request body and the response status and body.
  3. GET .../protection afterwards, showing required_pull_request_reviews with required_approving_review_count set and every other setting unchanged.

A regular protected branch (classic protection, not a ruleset) on a personal or organization repository is the case that matters.

Smaller:

  • When reviews and status checks are both missing, the change set now sends the reviews PATCH and then the full PUT, which also carries the reviews. Nothing is lost, but it's two writes where main sent one, and the PUT's listed changes don't mention reviews. Folding reviews into the PUT whenever there is one would keep it to one write.
  • The spec bump to 1.0.0-alpha.33 collides with #799 and the #802-#808 batch, which claim the same number. Whichever merges later renumbers.

Drafted with AI assistance (Claude), reviewed and filed by me.

@Daniel-park10
Daniel-park10 force-pushed the fix/546-patch-pull-request-reviews branch 2 times, most recently from e1fbd4e to d578d23 Compare December 3, 2026 06:53
@mlieberman85

Copy link
Copy Markdown
Member

Thanks @Daniel-park10. Folding reviews into the full PUT when one is sent anyway is what I suggested, and the changes list now names reviews in both shapes.

What I still need before approving is the transcript from the live check you mention in the commit message: the protection GET before, the PATCH .../required_pull_request_reviews request and response, and the GET after (redact the token and the repository name as you like). This PR rests on that one GitHub behavior, and the tests can't show it because the simulated GitHub accepts the PATCH.

Also, the PR description still says "Removed fold_reviews", but the code now folds reviews into the PUT when there is one. Please update it to match.


Drafted with AI assistance (Claude), reviewed and filed by me.

@Daniel-park10
Daniel-park10 force-pushed the fix/546-patch-pull-request-reviews branch from d578d23 to 9143fa2 Compare December 3, 2026 17:17
…ches (fixes darnitdevorg#546)

Enable or raise pull request review requirements with
PATCH .../required_pull_request_reviews even when reviews are not yet
configured on a protected branch, instead of triggering a full PUT.
When another requirement already triggers a full PUT, fold the review
change and its FieldChange into that PUT so only one write is sent.

Verified against a disposable GitHub repository (classic branch
protection) that PATCH .../required_pull_request_reviews returns 200
and preserves existing protection settings. Updated Section 4.5 of
docs/architecture/framework-design.md, CHANGELOG.md, and the branch
protection tests.

Signed-off-by: Daniel-park10 <dev10.tennyson@gmail.com>
@Daniel-park10
Daniel-park10 force-pushed the fix/546-patch-pull-request-reviews branch from 9143fa2 to cf91e82 Compare December 4, 2026 08:22
@Daniel-park10

Copy link
Copy Markdown
Contributor Author

@mlieberman85 Updated the PR description to reflect the fold_reviews behavior (PATCH when standalone, folded into PUT when another requirement already emits one).

Here is the redacted transcript from verifying PATCH .../required_pull_request_reviews against a classic-protected branch on a disposable repository (repository name and branch redacted):

1. Protection GET before (required_pull_request_reviews absent)

gh api -X GET /repos/{owner}/{repo}/branches/{branch}/protection --include
HTTP/2.0 200 OK
{
  "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection",
  "required_status_checks": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks",
    "strict": true,
    "contexts": [
      "ci/test"
    ],
    "contexts_url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks/contexts",
    "checks": [
      {
        "context": "ci/test",
        "app_id": null
      }
    ]
  },
  "required_signatures": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_signatures",
    "enabled": false
  },
  "enforce_admins": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/enforce_admins",
    "enabled": true
  },
  "required_linear_history": {
    "enabled": true
  },
  "allow_force_pushes": {
    "enabled": false
  },
  "allow_deletions": {
    "enabled": false
  },
  "block_creations": {
    "enabled": false
  },
  "required_conversation_resolution": {
    "enabled": true
  },
  "lock_branch": {
    "enabled": false
  },
  "allow_fork_syncing": {
    "enabled": false
  }
}

2. PATCH .../required_pull_request_reviews request and response

gh api -X PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews --include --input - <<< '{"required_approving_review_count": 1}'
HTTP/2.0 200 OK
{
  "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews",
  "dismiss_stale_reviews": false,
  "require_code_owner_reviews": false,
  "require_last_push_approval": false,
  "required_approving_review_count": 1
}

(Also verified {"required_approving_review_count": 0} on a second protected branch without reviews configured; it returned HTTP/2.0 200 OK with "required_approving_review_count": 0.)

3. Protection GET after (required_pull_request_reviews enabled, all other settings unchanged)

gh api -X GET /repos/{owner}/{repo}/branches/{branch}/protection --include
HTTP/2.0 200 OK
{
  "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection",
  "required_status_checks": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks",
    "strict": true,
    "contexts": [
      "ci/test"
    ],
    "contexts_url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks/contexts",
    "checks": [
      {
        "context": "ci/test",
        "app_id": null
      }
    ]
  },
  "required_pull_request_reviews": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews",
    "dismiss_stale_reviews": false,
    "require_code_owner_reviews": false,
    "require_last_push_approval": false,
    "required_approving_review_count": 1
  },
  "required_signatures": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/required_signatures",
    "enabled": false
  },
  "enforce_admins": {
    "url": "https://api.github.com/repos/{owner}/{repo}/branches/{branch}/protection/enforce_admins",
    "enabled": true
  },
  "required_linear_history": {
    "enabled": true
  },
  "allow_force_pushes": {
    "enabled": false
  },
  "allow_deletions": {
    "enabled": false
  },
  "block_creations": {
    "enabled": false
  },
  "required_conversation_resolution": {
    "enabled": true
  },
  "lock_branch": {
    "enabled": false
  },
  "allow_fork_syncing": {
    "enabled": false
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Verify that PATCH required_pull_request_reviews enables reviews on a protected branch without them

2 participants