API and branch protection

Hello,

I’m working on a tool to help my team create new project using a cookiecutter template. Most of it is working fine, except that in the said tool, I want to be able to add a branch protection to master branch (the tool is creating the repository on github - when it will be done it will use my team’s organization account).

But I’m facing a problem with that simple task. When I started, i was like “ok, I don’t need specific permissions on this branch, so let’s let “restrictions” out of the original json”. But when I tested the tool, Github answered :

{“message”:“Invalid request.\n\n\“restrictions\” wasn’t supplied.”,“documentation_url”:“https://developer.github.com/v3/repos/branches/#update-branch-protection”}

Well, fair enough. So i changed my json and provided restriction :

"restrictions": {
"users": [],  
"teams": []
}

And now Github say :

{“message”:“Validation Failed”,“errors”:[“Only organization repositories can have users and team restrictions”],“documentation_url”:“https://developer.github.com/v3/repos/branches/#update-branch-protection”}

Weeeeeell… Ok but… Of course in the end it won’t be a problem as I will indeed use an organization repository, except that this cookiecutter will be in my public repositories for anyone (including me on my personnal projects) to use, and that won’t do at all if it’s restricted to organization repositories.

So i tried to just remove any content on “restrictions”, leaving it an empty object. Of course (and that’s quite logical) the answer was :

{“message”:“Invalid request.\n\nNo subschema in \“anyOf\” matched.\n\“teams\”, \“users\” weren’t supplied.\nNot all subschemas of \“allOf\” matched.\nFor ‘anyOf/1’, {} is not a null.”,“documentation_url”:“https://developer.github.com/v3/repos/branches/#update-branch-protection”}

So i though “ok, maybe a null value will do ?” and I put some None (yes, this is in python) in my pre-jsonned dict. Now Github answer is :

{“message”:“Invalid request.\n\nNo subschema in \“anyOf\” matched.\nFor ‘properties/users’, nil is not an array.\nFor ‘properties/teams’, nil is not an array.\nNot all subschemas of \“allOf\” matched.\nFor ‘anyOf/1’, {\“users\”=>nil, \“teams\”=>nil} is not a null.”,“documentation_url”:“https://developer.github.com/v3/repos/branches/#update-branch-protection”}

So now I’m a bit out of idea - well of course i’ve tryed with only “users” but no luck either. Is branch protection through API an organization-only feature ?

Hi @exanis! You might have better luck with getting help on this particular topic in our Platform-specific forum. I would recommend trying to open up a Topic there, as that community is specifically tuned into the API and you might get an answer faster.

1 Like

Were you every able to figure this out @exanis ?  Running into the same problem over a year later.  But my issue is happening on both org repos and personal repos.

1 Like