Adding Branch Protection Rules through endpoint API

I am trying to add a Pull request review protection rule to one of the repositories I am working on, and using the following API call to add it:

curl -X PATCH -H "Accept: application/vnd.github.v3+json"$REPOORG/$REPO/branches/main/protection/required_pull_request_reviews -d '{"require_code_owner_reviews":true}'

I keep getting this error message. When I try to check the repository branch, I get a 404 for found error message:
i have admin permission to change the repository, but I was wondering what API query I should use to add the branch protection rule with. I have tried using the following API call, but that didn’t seem to work either:

hub api -X PUT -H "Accept: application/vnd.github.v3+json" /repos/$REPOORG/$REPO/branches/main/protection --raw-field '{"dismissal_restrictions": {},"dismiss_stale_reviews": false,"require_code_owner_reviews": true,"required_approving_review_count": 1}'

And I also tried this command and got this error message instead:

hub api -t -X PUT -H 'Accept: application/vnd.github.v3+json' /repos/splunk/splunk-add-on-for-ucc-example/branches/main/protection/required_pull_request_reviews --field require_code_owner_reviews=true
.message	Branch not protected

I would really appreciate the help!

Looking at other posts, I am getting the same issue asked about here:

I personally do not have the required access though I am using a Github Token that should have admin permission to that repository.

Hey @seanmalloy-crest how are you going, aside from these API troubles? Well, I hope!

You mentioned:

I am using a Github Token that should have admin permission to that repository.

But in your example curl, there was no token being applied to your calls, that I could see. At least, not from your screenshot above. Our REST Authentication docs are here:

Because a 404 not found could easily mean permissions issues and if you aren’t passing a token with valid permission, would explain what you see.

Let us know if you continue to have trouble after properly authenticating your calls! :bow:

1 Like

Thanks, that seemed to help a lot!

Here is the final curl call I made, in case if anyone else has a similar issue:

curl -X PUT -u $GITHUB_USER:$GITHUB_TOKEN -H "Accept: application/vnd.github.luke-cage-preview+json"$REPOORG/$REPO/branches/main/protection -d '{"required_status_checks": null,"enforce_admins": null,"required_pull_request_reviews" : {"dismissal_restrictions": {},"dismiss_stale_reviews": false,"require_code_owner_reviews": true,"required_approving_review_count": 1},"restrictions":null}'
1 Like