GraphQL API Protected branch

I am using a GITHUB API V3 in one of my project now we are migrating to GraraphQL API V4. I want to list all the branches of the repo and i want to check wether it is protected branch. In GITHUB API V3 it has a branches api that will list all the branches along with it is protected key so it was each to check. Now in GraphQL i can get all the branches list but i can’t check wether the branch is protected or not.

refs(first: 100, refPrefix: "refs/heads/") {
        	nodes {
        		name
        	}
        }

Someone could assist on this how to proceed?

If that could not be achieved by the refs then is there a way to list all the protected branches alone

Regards

Parithiban

You can use the following query to get the list of branch protection rules (and what branches they match) and the list of all branches for a repository:

{
  repository(owner: "atom", name: "atom") {
    branchProtectionRules(first: 100) {
      nodes {
        pattern
        matchingRefs(first: 100) {
          nodes {
            name
          }
        }
      }
    }
    refs(first: 100, refPrefix: "refs/heads/") {
      nodes {
        name
      }
    }
  }
}

I hope that helps! Let us know if you have more questions.

2 Likes

Hello, 

how can i use the same kind of pattern to protect my branch with wildcard rules like: release*

I am trying. to use API call for this

@maloochpich

See the below example to create a branch protection rule. You can change *pari* with release*

You can check the output in the settings tab and clicking branches options in your repo.

2 Likes

@parithiban   thanks a lot that’s what I was looking for

Does github have api to see who can push to the protected branch? I am looking at pushAllowances  but couldn’t figure it out.

The fix is to use fragment:

something like 

pushAllowances(first: 50) {
          nodes {
            actor {
              ... on App {
                app_name: name
              }

@makeapp007 

If I get your point correctly. You need to get the list of users who can push to the protected branch

You can follow the below steps:

  1. Create a Branch protection rule in your repo. You need to set the branch protections to the user/team/app Reference

  1. You can call the branch protection query for graphql to list out the user who has access to the protected branch.

Hope this answers your query.

1 Like

@parithiban 

question regarding to https://developer.github.com/v4/input_object/deletebranchprotectionruleinput/

what is the branchProtectionRuleId is?

I tried to add the repository node_id did not work,

any suggestion?

mutation {
    deleteBranchProtectionRule(input: {
        branchProtectionRuleId: "node_id"
    })
}

@maloochpich 

First, you need to get the branch protection id then you can pass that to the mutation input parameter

To get the branch protection id see below

Then you need to pass that branch protection id.

mutation {
	deleteBranchProtectionRule(input: {
		branchProtectionRuleId: "YOUR_BRANCH_PROTECTION_ID"
	})
}

By applying the above query your branch protection will be deleted.