GH API to update branch permissions

I am trying to update a branch permission by using gh apis. I authenticated my gh terminal using gh auth login and am able to execute certain gh commands as well.

However, when I am trying to update branch permissions, I am seeing Invalid request errors. I tried running the same using curl and this request is successful. How do I replicate the same using gh api?

curl --location --request PUT 'https://api.github.com/repos/<orgName>/<repoName>/main/protection' \
--header 'Authorization: Bearer <token> \
--header 'Content-Type: application/json' \
--data-raw '{"required_status_checks":{"strict":true,"contexts":["contexts"]},"enforce_admins":true,"required_pull_request_reviews": {},"restrictions":{"users":["users"],"teams":["teams"],"apps":["apps"]}}'

This is what I have come up with thus far (I tried putting in the same body as above but that was failing as well):

gh api -X PUT repos/<orgName>/<repoName>/branches/main/protection -f enforce_admins='true' -f required_pull_request_reviews='{}' -f restrictions='{}' -f required_status_checks='{}'` 

and the error I am seeing looks like this.

{
  "message": "Invalid request.\n\nNo subschema in \"anyOf\" matched.\nNo subschema in \"anyOf\" matched.\nNo subschema in \"anyOf\" matched.\nNo subschema in \"anyOf\" matched.\nFor 'allOf/0', \"{}\" is not an object.\nNot all subschemas of \"allOf\" matched.\nFor 'anyOf/1', \"{}\" is not a null.\nFor 'allOf/0', \"{}\" is not an object.\nNot all subschemas of \"allOf\" matched.\nFor 'anyOf/1', \"{}\" is not a null.\nFor 'allOf/0', \"true\" is not a boolean.\nNot all subschemas of \"allOf\" matched.\nFor 'anyOf/1', \"true\" is not a null.\nFor 'allOf/0', \"{}\" is not an object.\nNot all subschemas of \"allOf\" matched.\nFor 'anyOf/1', \"{}\" is not a null.",
  "documentation_url": "https://docs.github.com/rest/reference/repos#update-branch-protection"
}
gh: Invalid request.

Any help on how to send the right request through gh api would be really helpful. Thanks!

This post helped me answer my question. So, I had to pass in the array object as input as opposed to the field value (as that cannot take array values currently)

Something like this worked for me:

jq -n '{"required_status_checks":{"strict":true,"contexts":["contexts"]},"enforce_admins":true,"required_pull_request_reviews": {},"restrictions":{"users":["users"],"teams":["teams"],"apps":["apps"]}}' | gh api -X PUT repos/<orgName>/<repoName>/branches/main/protection --input -
1 Like