Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub GraphQL API - Branch Protection Rule - How do I get "Require a pull request before merging"?

Tags:

github-api

I'd like to get the value set for the Require a pull request before merging via the GraphQL API. I'm not finding or am missing the information for the docs on the BranchProtectionRule. I'd imagine this should be obvious but isn't for me at the moment.

enter image description here

And for the purposes of helping out anyone else on a related path, here's my code for getting the branch protection rules using PowerShell and GitHub GraphQL API:

$JsonContent = & "gh" api graphql -H 'X-Github-Next-Global-ID: 1' -F owner="$repoowner" -F name="$reponame" -f query='
    query($name: String!, $owner: String!) {
        repository(owner: $owner, name: $name) {
            branchProtectionRules(first: 100) {
                nodes {
                    matchingRefs(first: 100) {
                    nodes {
                        name
                    }
                }
                pattern
                allowsDeletions
                allowsForcePushes
                dismissesStaleReviews
                isAdminEnforced
                pushAllowances(first: 100) {
                    nodes {
                      actor {
                        ... on App {
                          id
                          name
                          slug
                        }
                        ... on Team {
                          id
                          name
                          slug
                        }
                        ... on User {
                          id
                          name
                          login
                        }
                      }
                    }
                  }
                requiresApprovingReviews
                requiredApprovingReviewCount
                requiresCodeOwnerReviews
                requiresCommitSignatures
                requiresConversationResolution
                requiresLinearHistory
                requiresStatusChecks
                requiredStatusCheckContexts
                requiresStrictStatusChecks
                restrictsPushes
                restrictsReviewDismissals
                }
            }
        }
    }
'
like image 758
Michael Mainer Avatar asked Oct 16 '25 09:10

Michael Mainer


1 Answers

I think I've found the answer; confusingly, the Require a pull request before merging setting seems to correspond with the requiresApprovingReviews value in conjunction with the requiredApprovingReviewCount value—despite the setting in the GitHub UI not mentioning reviews at all.

To try and figure this out, I selected all available properties in my GraphQL query for a single repository, then toggled the setting on and off to see what was different.

query MyQuery {
  repository(name: "MY_REPO_NAME", owner: "MY_ORGANISATION") {
    branchProtectionRules(first: 10) {
      edges {
        node {
          allowsDeletions
          allowsForcePushes
          blocksCreations
          dismissesStaleReviews
          isAdminEnforced
          lockAllowsFetchAndMerge
          lockBranch
          pattern
          requireLastPushApproval
          requiredApprovingReviewCount
          requiredDeploymentEnvironments
          requiresApprovingReviews
          requiresCodeOwnerReviews
          requiresCommitSignatures
          requiresConversationResolution
          requiresDeployments
          requiresLinearHistory
          requiresStatusChecks
          requiresStrictStatusChecks
          restrictsPushes
          restrictsReviewDismissals
        }
      }
    }
  }
}

So the results of that were that with the setting unchecked:

"Require a pull request before merging" not checked

We get:

{
  "data": {
    "repository": {
      "branchProtectionRules": {
        "edges": [
          {
            "node": {
              "allowsDeletions": false,
              "allowsForcePushes": false,
              "blocksCreations": false,
              "dismissesStaleReviews": false,
              "isAdminEnforced": false,
              "lockAllowsFetchAndMerge": false,
              "lockBranch": false,
              "pattern": "master",
              "requireLastPushApproval": false,
              "requiredApprovingReviewCount": null,
              "requiredDeploymentEnvironments": [],
              "requiresApprovingReviews": false,
              "requiresCodeOwnerReviews": false,
              "requiresCommitSignatures": false,
              "requiresConversationResolution": false,
              "requiresDeployments": false,
              "requiresLinearHistory": false,
              "requiresStatusChecks": false,
              "requiresStrictStatusChecks": true,
              "restrictsPushes": false,
              "restrictsReviewDismissals": false
            }
          }
        ]
      }
    }
  }
}

And with the setting checked:

"Require a pull request before merging" checked

We get:

{
  "data": {
    "repository": {
      "branchProtectionRules": {
        "edges": [
          {
            "node": {
              "allowsDeletions": false,
              "allowsForcePushes": false,
              "blocksCreations": false,
              "dismissesStaleReviews": false,
              "isAdminEnforced": false,
              "lockAllowsFetchAndMerge": false,
              "lockBranch": false,
              "pattern": "master",
              "requireLastPushApproval": false,
              "requiredApprovingReviewCount": 0,
              "requiredDeploymentEnvironments": [],
              "requiresApprovingReviews": true,
              "requiresCodeOwnerReviews": false,
              "requiresCommitSignatures": false,
              "requiresConversationResolution": false,
              "requiresDeployments": false,
              "requiresLinearHistory": false,
              "requiresStatusChecks": false,
              "requiresStrictStatusChecks": true,
              "restrictsPushes": false,
              "restrictsReviewDismissals": false
            }
          }
        ]
      }
    }
  }
}

So the difference is:

Before/after diff showing that requiresApprovingReviews and requiredApprovingReviewCount are true/0 after checking the setting

So it seems you can say that if requiresApprovingReviews is true then the Require a pull request before merging setting is enabled for that repository. The requiredApprovingReviewCount will be 0 if no PR approvals are required to merge.

like image 50
Mark Bell Avatar answered Oct 18 '25 15:10

Mark Bell