How to read wildcard branch protection rules using the API?

It seems that you cannot read wildcard branch protection rules via the API. Instead you can only look up the protections for branches that exist.

Is this feature on the roadmap for Github?

The GraphQL API branchprotectionrule endpoint may be helpful. It will output the patterns settings:

{
  "data": {
    "repository": {
      "branchProtectionRules": {
        "nodes": [
          {
            "pattern": "main",
            "isAdminEnforced": true,
            "requiresApprovingReviews": true,
            "pushAllowances": {
              "edges": [
                {
                  "node": {
                    "actor": {},
                    "id": "xxx"
                  }
                }
              ]
            }
          },
          {
            "pattern": "testbranch*",
            "isAdminEnforced": false,
            "requiresApprovingReviews": false,
            "pushAllowances": {
              "edges": []
            }
          }
        ]
      }
    }
  }
}

To test with the GraphQL Explorer, paste something like this with repo and org replaced with appropriate values for your account:

{ repository(name:"<repo>", owner:"<org>") { branchProtectionRules(first:10){ nodes{ pattern isAdminEnforced requiresApprovingReviews pushAllowances(first:10){ edges{ node{ actor id } } } } } } }

Here’s a shell script using curl to do the same thing:

read -r -d '' graphql_script <<- EOF
{  repository(name:"$repo", owner:"$org") {
    branchProtectionRules(first:10){
      nodes{
        pattern
        isAdminEnforced
        requiresApprovingReviews
        pushAllowances(first:10){
          edges{
              node{
            actor
            id
          }
        }
        }
      }
    }
  }
}
EOF

graphql_script="$(echo ${graphql_script//\"/\\\"})"

curl \
     -H "Accept: application/vnd.github.v3+json" \
     -H "Authorization: token ${GITHUB_TOKEN}" \
        https://api.github.com/graphql -d "{ \"query\": \"$graphql_script\"}"