2

I am using a GITHUB API V3 in one of my projects now we are migrating to GraraphQL API V4. I want to list all the branches of the repo and I want to check whether it is a 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 easy to check. Now in GraphQL, I can get all the branches list but I can't check whether the branch is protected or not.

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

Someone could assist with this on how to proceed?

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

Parithiban
  • 1,656
  • 11
  • 16

2 Answers2

1

I found out the logic to get the protected branch

{
    viewer {
        repository(owner: "parithiban", name: "git-slack-bot") {
            branchProtectionRules(first: 5) {
                nodes {
                    pattern
                    requiredApprovingReviewCount
                    matchingRefs(first: 100) {
                        nodes {
                            name
                        }
                    }
                }
            }
        }
    }
}
Parithiban
  • 1,656
  • 11
  • 16
0

Accepted answer does not work, because it's calling repository() with an owner argument. It's already scoped to the logged-in user, so you get this error:

"errors": [
   ...
   "message": "Field 'repository' doesn't accept argument 'owner'"
]

Remove the outermost curly-braces and the word viewer, and it will work.

Daryl
  • 3,654
  • 1
  • 14
  • 14